From d2bf1c65e0019ed52e1a94aae9b0f66d30093947 Mon Sep 17 00:00:00 2001 From: Gus Eggert Date: Wed, 10 Nov 2021 11:56:49 -0500 Subject: [PATCH] feat: plumb through contexts from peerstore (#111) --- p2p/host/autonat/autonat_test.go | 26 +++++++++++++++++++----- p2p/host/autonat/dialpolicy_test.go | 6 +++--- p2p/host/autonat/svc_test.go | 31 ++++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/p2p/host/autonat/autonat_test.go b/p2p/host/autonat/autonat_test.go index b000d503da..18d64b6cdb 100644 --- a/p2p/host/autonat/autonat_test.go +++ b/p2p/host/autonat/autonat_test.go @@ -20,13 +20,13 @@ import ( // these are mock service implementations for testing func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host { - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate) return h } func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host { - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) h.SetStreamHandler(AutoNATProto, sayAutoNATPublic) return h } @@ -52,7 +52,7 @@ func sayAutoNATPublic(s network.Stream) { } func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) { - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) h.Peerstore().AddAddrs(ash.ID(), ash.Addrs(), time.Minute) h.Peerstore().AddProtocols(ash.ID(), AutoNATProto) a, _ := New(h, WithSchedule(100*time.Millisecond, time.Second), WithoutStartupDelay()) @@ -94,7 +94,10 @@ func TestAutoNATPrivate(t *testing.T) { defer cancel() hs := makeAutoNATServicePrivate(ctx, t) + defer hs.Close() hc, an := makeAutoNAT(ctx, t, hs) + defer hc.Close() + defer an.Close() // subscribe to AutoNat events s, err := hc.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{}) @@ -123,7 +126,10 @@ func TestAutoNATPublic(t *testing.T) { defer cancel() hs := makeAutoNATServicePublic(ctx, t) + defer hs.Close() hc, an := makeAutoNAT(ctx, t, hs) + defer hc.Close() + defer an.Close() // subscribe to AutoNat events s, err := hc.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{}) @@ -152,7 +158,10 @@ func TestAutoNATPublictoPrivate(t *testing.T) { defer cancel() hs := makeAutoNATServicePublic(ctx, t) + defer hs.Close() hc, an := makeAutoNAT(ctx, t, hs) + defer hc.Close() + defer an.Close() // subscribe to AutoNat events s, err := hc.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{}) @@ -195,7 +204,10 @@ func TestAutoNATIncomingEvents(t *testing.T) { defer cancel() hs := makeAutoNATServicePrivate(ctx, t) + defer hs.Close() hc, ani := makeAutoNAT(ctx, t, hs) + defer hc.Close() + defer ani.Close() an := ani.(*AmbientAutoNAT) status := an.Status() @@ -219,7 +231,10 @@ func TestAutoNATObservationRecording(t *testing.T) { defer cancel() hs := makeAutoNATServicePublic(ctx, t) + defer hs.Close() hc, ani := makeAutoNAT(ctx, t, hs) + defer hc.Close() + defer ani.Close() an := ani.(*AmbientAutoNAT) s, err := hc.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{}) @@ -273,10 +288,11 @@ func TestAutoNATObservationRecording(t *testing.T) { } func TestStaticNat(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) + _, cancel := context.WithCancel(context.Background()) defer cancel() - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) + defer h.Close() s, _ := h.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{}) nat, err := New(h, WithReachability(network.ReachabilityPrivate)) diff --git a/p2p/host/autonat/dialpolicy_test.go b/p2p/host/autonat/dialpolicy_test.go index df9061cfc8..92436405ff 100644 --- a/p2p/host/autonat/dialpolicy_test.go +++ b/p2p/host/autonat/dialpolicy_test.go @@ -54,7 +54,7 @@ func TestSkipDial(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - s := swarmt.GenSwarm(t, ctx) + s := swarmt.GenSwarm(t) d := dialPolicy{host: blankhost.NewBlankHost(s)} if d.skipDial(makeMA("/ip4/8.8.8.8")) != false { t.Fatal("failed dialing a valid public addr") @@ -82,7 +82,7 @@ func TestSkipPeer(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - s := swarmt.GenSwarm(t, ctx) + s := swarmt.GenSwarm(t) d := dialPolicy{host: blankhost.NewBlankHost(s)} if d.skipPeer([]multiaddr.Multiaddr{makeMA("/ip4/8.8.8.8")}) != false { t.Fatal("failed dialing a valid public addr") @@ -115,7 +115,7 @@ func TestSkipLocalPeer(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - s := swarmt.GenSwarm(t, ctx) + s := swarmt.GenSwarm(t) d := dialPolicy{host: blankhost.NewBlankHost(s)} s.AddTransport(&mockT{ctx, makeMA("/ip4/192.168.0.1")}) err := s.AddListenAddr(makeMA("/ip4/192.168.0.1")) diff --git a/p2p/host/autonat/svc_test.go b/p2p/host/autonat/svc_test.go index 857f9e6197..f8748ff6b7 100644 --- a/p2p/host/autonat/svc_test.go +++ b/p2p/host/autonat/svc_test.go @@ -15,8 +15,8 @@ import ( ) func makeAutoNATConfig(ctx context.Context, t *testing.T) *config { - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) - dh := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) + dh := bhost.NewBlankHost(swarmt.GenSwarm(t)) c := config{host: h, dialer: dh.Network()} _ = defaults(&c) c.forceReachability = true @@ -35,7 +35,7 @@ func makeAutoNATService(t *testing.T, c *config) *autoNATService { } func makeAutoNATClient(ctx context.Context, t *testing.T) (host.Host, Client) { - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) cli := NewAutoNATClient(h, nil) return h, cli } @@ -46,10 +46,14 @@ func TestAutoNATServiceDialError(t *testing.T) { defer cancel() c := makeAutoNATConfig(ctx, 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) + defer hc.Close() connect(t, c.host, hc) _, err := ac.DialBack(ctx, c.host.ID()) @@ -67,9 +71,13 @@ func TestAutoNATServiceDialSuccess(t *testing.T) { defer cancel() c := makeAutoNATConfig(ctx, t) + defer c.host.Close() + defer c.dialer.Close() + _ = makeAutoNATService(t, c) hc, ac := makeAutoNATClient(ctx, t) + defer hc.Close() connect(t, c.host, hc) _, err := ac.DialBack(ctx, c.host.ID()) @@ -83,6 +91,9 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) { defer cancel() c := makeAutoNATConfig(ctx, t) + defer c.host.Close() + defer c.dialer.Close() + c.dialTimeout = 1 * time.Second c.throttleResetPeriod = time.Second c.throttleResetJitter = 0 @@ -90,6 +101,7 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) { _ = makeAutoNATService(t, c) hc, ac := makeAutoNATClient(ctx, t) + defer hc.Close() connect(t, c.host, hc) _, err := ac.DialBack(ctx, c.host.ID()) @@ -119,6 +131,9 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) { defer cancel() c := makeAutoNATConfig(ctx, t) + defer c.host.Close() + defer c.dialer.Close() + c.dialTimeout = time.Second c.throttleResetPeriod = 10 * time.Second c.throttleResetJitter = 0 @@ -139,6 +154,7 @@ func TestAutoNATServiceGlobalLimiter(t *testing.T) { } hc, ac := makeAutoNATClient(ctx, t) + defer hc.Close() connect(t, hs, hc) _, err := ac.DialBack(ctx, hs.ID()) if err == nil { @@ -154,6 +170,9 @@ func TestAutoNATServiceRateLimitJitter(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) c := makeAutoNATConfig(ctx, t) + defer c.host.Close() + defer c.dialer.Close() + c.throttleResetPeriod = 100 * time.Millisecond c.throttleResetJitter = 100 * time.Millisecond c.throttleGlobalMax = 1 @@ -176,8 +195,10 @@ func TestAutoNATServiceStartup(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) - dh := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h := bhost.NewBlankHost(swarmt.GenSwarm(t)) + defer h.Close() + dh := bhost.NewBlankHost(swarmt.GenSwarm(t)) + defer dh.Close() an, err := New(h, EnableService(dh.Network())) an.(*AmbientAutoNAT).config.dialPolicy.allowSelfDials = true if err != nil {