Skip to content

Commit

Permalink
speed up the tests (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 21, 2021
1 parent 60188e3 commit 2486dd7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 84 deletions.
100 changes: 41 additions & 59 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import (
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-msgio/protoio"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)

// these are mock service implementations for testing
func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host {
func makeAutoNATServicePrivate(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
return h
}

func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host {
func makeAutoNATServicePublic(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.SetStreamHandler(AutoNATProto, sayAutoNATPublic)
return h
Expand All @@ -51,7 +52,7 @@ func sayAutoNATPublic(s network.Stream) {
w.WriteMsg(&res)
}

func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) {
func makeAutoNAT(t *testing.T, ash host.Host) (host.Host, AutoNAT) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.Peerstore().AddAddrs(ash.ID(), ash.Addrs(), time.Minute)
h.Peerstore().AddProtocols(ash.ID(), AutoNATProto)
Expand Down Expand Up @@ -90,12 +91,9 @@ func expectEvent(t *testing.T, s event.Subscription, expected network.Reachabili

// tests
func TestAutoNATPrivate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hs := makeAutoNATServicePrivate(ctx, t)
hs := makeAutoNATServicePrivate(t)
defer hs.Close()
hc, an := makeAutoNAT(ctx, t, hs)
hc, an := makeAutoNAT(t, hs)
defer hc.Close()
defer an.Close()

Expand All @@ -111,23 +109,19 @@ func TestAutoNATPrivate(t *testing.T) {
}

connect(t, hs, hc)
time.Sleep(2 * time.Second)

status = an.Status()
if status != network.ReachabilityPrivate {
t.Fatalf("unexpected NAT status: %d", status)
}

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)
}

func TestAutoNATPublic(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hs := makeAutoNATServicePublic(ctx, t)
hs := makeAutoNATServicePublic(t)
defer hs.Close()
hc, an := makeAutoNAT(ctx, t, hs)
hc, an := makeAutoNAT(t, hs)
defer hc.Close()
defer an.Close()

Expand All @@ -143,23 +137,20 @@ func TestAutoNATPublic(t *testing.T) {
}

connect(t, hs, hc)
time.Sleep(1500 * time.Millisecond)

status = an.Status()
if status != network.ReachabilityPublic {
t.Fatalf("unexpected NAT status: %d", status)
}
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)
}

func TestAutoNATPublictoPrivate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hs := makeAutoNATServicePublic(ctx, t)
hs := makeAutoNATServicePublic(t)
defer hs.Close()
hc, an := makeAutoNAT(ctx, t, hs)
hc, an := makeAutoNAT(t, hs)
defer hc.Close()
defer an.Close()

Expand All @@ -169,43 +160,37 @@ func TestAutoNATPublictoPrivate(t *testing.T) {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPublic, err=%s", err)
}

status := an.Status()
if status != network.ReachabilityUnknown {
if status := an.Status(); status != network.ReachabilityUnknown {
t.Fatalf("unexpected NAT status: %d", status)
}

connect(t, hs, hc)
time.Sleep(1500 * time.Millisecond)

status = an.Status()
if status != network.ReachabilityPublic {
t.Fatalf("unexpected NAT status: %d", status)
}

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)

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

time.Sleep(2 * time.Second)

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)

status = an.Status()
if status != network.ReachabilityPrivate {
t.Fatalf("unexpected NAT status: %d", status)
}
}

func TestAutoNATIncomingEvents(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hs := makeAutoNATServicePrivate(ctx, t)
hs := makeAutoNATServicePrivate(t)
defer hs.Close()
hc, ani := makeAutoNAT(ctx, t, hs)
hc, ani := makeAutoNAT(t, hs)
defer hc.Close()
defer ani.Close()
an := ani.(*AmbientAutoNAT)
Expand All @@ -227,12 +212,9 @@ func TestAutoNATIncomingEvents(t *testing.T) {
}

func TestAutoNATObservationRecording(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hs := makeAutoNATServicePublic(ctx, t)
hs := makeAutoNATServicePublic(t)
defer hs.Close()
hc, ani := makeAutoNAT(ctx, t, hs)
hc, ani := makeAutoNAT(t, hs)
defer hc.Close()
defer ani.Close()
an := ani.(*AmbientAutoNAT)
Expand All @@ -252,7 +234,7 @@ func TestAutoNATObservationRecording(t *testing.T) {
case <-s.Out():
t.Fatal("not expecting a public reachability event")
default:
//expected
// expected
}

addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
Expand Down
43 changes: 18 additions & 25 deletions p2p/host/autonat/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

func makeAutoNATConfig(ctx context.Context, t *testing.T) *config {
func makeAutoNATConfig(t *testing.T) *config {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
dh := bhost.NewBlankHost(swarmt.GenSwarm(t))
c := config{host: h, dialer: dh.Network()}
Expand All @@ -34,7 +34,7 @@ func makeAutoNATService(t *testing.T, c *config) *autoNATService {
return as
}

func makeAutoNATClient(ctx context.Context, t *testing.T) (host.Host, Client) {
func makeAutoNATClient(t *testing.T) (host.Host, Client) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
cli := NewAutoNATClient(h, nil)
return h, cli
Expand All @@ -45,14 +45,14 @@ func TestAutoNATServiceDialError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := makeAutoNATConfig(ctx, t)
c := makeAutoNATConfig(t)
defer c.host.Close()
defer c.dialer.Close()

c.dialTimeout = 1 * time.Second
c.dialPolicy.allowSelfDials = false
_ = makeAutoNATService(t, c)
hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
defer hc.Close()
connect(t, c.host, hc)

Expand All @@ -70,13 +70,13 @@ func TestAutoNATServiceDialSuccess(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := makeAutoNATConfig(ctx, t)
c := makeAutoNATConfig(t)
defer c.host.Close()
defer c.dialer.Close()

_ = makeAutoNATService(t, c)

hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
defer hc.Close()
connect(t, c.host, hc)

Expand All @@ -90,17 +90,17 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := makeAutoNATConfig(ctx, t)
c := makeAutoNATConfig(t)
defer c.host.Close()
defer c.dialer.Close()

c.dialTimeout = 1 * time.Second
c.throttleResetPeriod = time.Second
c.dialTimeout = 200 * time.Millisecond
c.throttleResetPeriod = 200 * time.Millisecond
c.throttleResetJitter = 0
c.throttlePeerMax = 1
_ = makeAutoNATService(t, c)

hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
defer hc.Close()
connect(t, c.host, hc)

Expand All @@ -118,7 +118,7 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) {
t.Fatal(err)
}

time.Sleep(2 * time.Second)
time.Sleep(400 * time.Millisecond)

_, err = ac.DialBack(ctx, c.host.ID())
if err != nil {
Expand All @@ -130,7 +130,7 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := makeAutoNATConfig(ctx, t)
c := makeAutoNATConfig(t)
defer c.host.Close()
defer c.dialer.Close()

Expand All @@ -144,7 +144,7 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) {
hs := c.host

for i := 0; i < 5; i++ {
hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
connect(t, hs, hc)

_, err := ac.DialBack(ctx, hs.ID())
Expand All @@ -153,7 +153,7 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) {
}
}

hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
defer hc.Close()
connect(t, hs, hc)
_, err := ac.DialBack(ctx, hs.ID())
Expand All @@ -167,9 +167,7 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) {
}

func TestAutoNATServiceRateLimitJitter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

c := makeAutoNATConfig(ctx, t)
c := makeAutoNATConfig(t)
defer c.host.Close()
defer c.dialer.Close()

Expand All @@ -187,14 +185,9 @@ func TestAutoNATServiceRateLimitJitter(t *testing.T) {
if svc.globalReqs != 0 {
t.Fatal("reset of rate limitter occured slower than expected")
}

cancel()
}

func TestAutoNATServiceStartup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

h := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h.Close()
dh := bhost.NewBlankHost(swarmt.GenSwarm(t))
Expand All @@ -205,10 +198,10 @@ func TestAutoNATServiceStartup(t *testing.T) {
t.Fatal(err)
}

hc, ac := makeAutoNATClient(ctx, t)
hc, ac := makeAutoNATClient(t)
connect(t, h, hc)

_, err = ac.DialBack(ctx, h.ID())
_, err = ac.DialBack(context.Background(), h.ID())
if err != nil {
t.Fatal("autonat service be active in unknown mode.")
}
Expand All @@ -220,7 +213,7 @@ func TestAutoNATServiceStartup(t *testing.T) {

<-sub.Out()

_, err = ac.DialBack(ctx, h.ID())
_, err = ac.DialBack(context.Background(), h.ID())
if err != nil {
t.Fatalf("autonat should be active, was %v", err)
}
Expand Down

0 comments on commit 2486dd7

Please sign in to comment.