Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Take eventbus events to completion #38

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 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 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")
}
}