diff --git a/autonat.go b/autonat.go index 351efe3..2d6c978 100644 --- a/autonat.go +++ b/autonat.go @@ -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 { @@ -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() } diff --git a/autonat_test.go b/autonat_test.go index e5fc7ff..74a429c 100644 --- a/autonat_test.go +++ b/autonat_test.go @@ -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" @@ -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) @@ -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) { @@ -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) @@ -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") + } }