Skip to content

Commit

Permalink
Use expectEvent and scale duration for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 1, 2022
1 parent e732bda commit 0d58bba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
47 changes: 12 additions & 35 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
pb "github.com/libp2p/go-libp2p/p2p/host/autonat/pb"
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
testutils "github.com/libp2p/go-libp2p/testing"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
Expand Down Expand Up @@ -86,15 +87,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 +121,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, testutils.ScaleDuration(2*time.Second))
}

func TestAutoNATPublic(t *testing.T) {
Expand All @@ -147,14 +143,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, testutils.ScaleDuration(2*time.Second))
}

func TestAutoNATPublictoPrivate(t *testing.T) {
Expand All @@ -175,26 +164,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, testutils.ScaleDuration(2*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, testutils.ScaleDuration(2*time.Second))
}

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

expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, testutils.ScaleDuration(2*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, testutils.ScaleDuration(2*time.Second))

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

expectEvent(t, s, network.ReachabilityPublic)
expectEvent(t, s, network.ReachabilityPublic, testutils.ScaleDuration(2*time.Second))

an.recordObservation(autoNATResult{network.ReachabilityPrivate, nil})
if an.Status() != network.ReachabilityPublic {
Expand All @@ -293,5 +270,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, testutils.ScaleDuration(2*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
13 changes: 13 additions & 0 deletions testing/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package testutils

import (
"os"
"time"
)

func ScaleDuration(d time.Duration) time.Duration {
if os.Getenv("CI") != "" {
d *= 10
}
return d
}

0 comments on commit 0d58bba

Please sign in to comment.