Skip to content

Commit

Permalink
Merge pull request #38 from aarshkshah1992/feat/autonat-evt-completion
Browse files Browse the repository at this point in the history
Take eventbus events to completion
  • Loading branch information
vyzo authored Feb 6, 2020
2 parents 373245d + 44def81 commit 0713eb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func (as *AmbientAutoNAT) autodetect() {
} else if as.confidence < 3 {
as.confidence++
}
as.updateStatus(NATStatusPublic)
as.addr = result.pubaddr
as.updateStatus(NATStatusPublic)
} else if result.private > 0 {
log.Debugf("NAT status is private")
if as.status == NATStatusPublic {
Expand All @@ -229,15 +229,15 @@ func (as *AmbientAutoNAT) autodetect() {
} else if as.confidence < 3 {
as.confidence++
}
as.updateStatus(NATStatusPrivate)
as.addr = nil
as.updateStatus(NATStatusPrivate)
} else if as.confidence > 0 {
// don't just flip to unknown, reduce confidence first
as.confidence--
} else {
log.Debugf("NAT status is unknown")
as.updateStatus(NATStatusUnknown)
as.addr = nil
as.updateStatus(NATStatusUnknown)
}
as.mx.Unlock()
}
Expand Down
38 changes: 36 additions & 2 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"time"

pb "github.com/libp2p/go-libp2p-autonat/pb"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

ggio "github.com/gogo/protobuf/io"
bhost "github.com/libp2p/go-libp2p-blankhost"
Expand Down Expand Up @@ -96,6 +96,12 @@ func TestAutoNATPrivate(t *testing.T) {
hs := makeAutoNATServicePrivate(ctx, t)
hc, an := makeAutoNAT(ctx, t, hs)

// subscribe to AutoNat events
s, err := hc.EventBus().Subscribe(&event.EvtLocalRoutabilityPrivate{})
if err != nil {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPrivate, err=%s", err)
}

status := an.Status()
if status != NATStatusUnknown {
t.Fatalf("unexpected NAT status: %d", status)
Expand All @@ -108,6 +114,17 @@ func TestAutoNATPrivate(t *testing.T) {
if status != NATStatusPrivate {
t.Fatalf("unexpected NAT status: %d", status)
}

select {
case e := <-s.Out():
_, ok := e.(event.EvtLocalRoutabilityPrivate)
if !ok {
t.Fatal("got wrong event type from the bus")
}

case <-time.After(1 * time.Second):
t.Fatal("failed to get the EvtLocalRoutabilityPrivate event from the bus")
}
}

func TestAutoNATPublic(t *testing.T) {
Expand All @@ -117,6 +134,12 @@ func TestAutoNATPublic(t *testing.T) {
hs := makeAutoNATServicePublic(ctx, t)
hc, an := makeAutoNAT(ctx, t, hs)

// subscribe to AutoNat events
s, err := hc.EventBus().Subscribe(&event.EvtLocalRoutabilityPublic{})
if err != nil {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPublic, err=%s", err)
}

status := an.Status()
if status != NATStatusUnknown {
t.Fatalf("unexpected NAT status: %d", status)
Expand All @@ -129,4 +152,15 @@ func TestAutoNATPublic(t *testing.T) {
if status != NATStatusPublic {
t.Fatalf("unexpected NAT status: %d", status)
}

select {
case e := <-s.Out():
_, ok := e.(event.EvtLocalRoutabilityPublic)
if !ok {
t.Fatal("got wrong event type from the bus")
}

case <-time.After(1 * time.Second):
t.Fatal("failed to get the EvtLocalRoutabilityPublic event from the bus")
}
}

0 comments on commit 0713eb4

Please sign in to comment.