Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autonat: fix flaky TestAutoNATPrivate #1581

Merged
merged 2 commits into from
Jun 2, 2022
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
46 changes: 11 additions & 35 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ func connect(t *testing.T, a, b host.Host) {
}
}

func expectEvent(t *testing.T, s event.Subscription, expected network.Reachability) {
func expectEvent(t *testing.T, s event.Subscription, expected network.Reachability, timeout time.Duration) {
t.Helper()
select {
case e := <-s.Out():
ev, ok := e.(event.EvtLocalReachabilityChanged)
if !ok || ev.Reachability != expected {
t.Fatal("got wrong event type from the bus")
}

case <-time.After(100 * time.Millisecond):
case <-time.After(timeout):
t.Fatal("failed to get the reachability event from the bus")
}
}
Expand All @@ -119,13 +120,7 @@ func TestAutoNATPrivate(t *testing.T) {
}

connect(t, hs, hc)
require.Eventually(t,
func() bool { return an.Status() == network.ReachabilityPrivate },
2*time.Second,
25*time.Millisecond,
"expected NAT status to be private",
)
expectEvent(t, s, network.ReachabilityPrivate)
expectEvent(t, s, network.ReachabilityPrivate, 3*time.Second)
}

func TestAutoNATPublic(t *testing.T) {
Expand All @@ -147,14 +142,7 @@ func TestAutoNATPublic(t *testing.T) {
}

connect(t, hs, hc)
require.Eventually(t,
func() bool { return an.Status() == network.ReachabilityPublic },
2*time.Second,
25*time.Millisecond,
"expected NAT status to be public",
)

expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, 3*time.Second)
}

func TestAutoNATPublictoPrivate(t *testing.T) {
Expand All @@ -175,26 +163,14 @@ func TestAutoNATPublictoPrivate(t *testing.T) {
}

connect(t, hs, hc)
require.Eventually(t,
func() bool { return an.Status() == network.ReachabilityPublic },
2*time.Second,
25*time.Millisecond,
"expected NAT status to be public",
)
expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, 3*time.Second)

hs.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
hps := makeAutoNATServicePrivate(t)
connect(t, hps, hc)
identifyAsServer(hps, hc)

require.Eventually(t,
func() bool { return an.Status() == network.ReachabilityPrivate },
2*time.Second,
25*time.Millisecond,
"expected NAT status to be private",
)
expectEvent(t, s, network.ReachabilityPrivate)
expectEvent(t, s, network.ReachabilityPrivate, 3*time.Second)
}

func TestAutoNATIncomingEvents(t *testing.T) {
Expand Down Expand Up @@ -252,15 +228,15 @@ func TestAutoNATObservationRecording(t *testing.T) {
t.Fatalf("failed to transition to public.")
}

expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, 3*time.Second)

// a single recording should have confidence still at 0, and transition to private quickly.
an.recordObservation(autoNATResult{network.ReachabilityPrivate, nil})
if an.Status() != network.ReachabilityPrivate {
t.Fatalf("failed to transition to private.")
}

expectEvent(t, s, network.ReachabilityPrivate)
expectEvent(t, s, network.ReachabilityPrivate, 3*time.Second)

// stronger public confidence should be harder to undo.
an.recordObservation(autoNATResult{network.ReachabilityPublic, addr})
Expand All @@ -269,7 +245,7 @@ func TestAutoNATObservationRecording(t *testing.T) {
t.Fatalf("failed to transition to public.")
}

expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, 3*time.Second)

an.recordObservation(autoNATResult{network.ReachabilityPrivate, nil})
if an.Status() != network.ReachabilityPublic {
Expand All @@ -293,5 +269,5 @@ func TestStaticNat(t *testing.T) {
if nat.Status() != network.ReachabilityPrivate {
t.Fatalf("should be private")
}
expectEvent(t, s, network.ReachabilityPrivate)
expectEvent(t, s, network.ReachabilityPrivate, 3*time.Second)
}
1 change: 0 additions & 1 deletion p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func TestSignedPeerRecordWithNoListenAddrs(t *testing.T) {
rec, err := env.Record()
require.NoError(t, err)
require.NotEmpty(t, rec.(*peer.PeerRecord).Addrs)

}

func TestProtocolHandlerEvents(t *testing.T) {
Expand Down