Skip to content

Commit

Permalink
feat: plumb through contexts from peerstore (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
guseggert authored Nov 10, 2021
1 parent 8cc74d3 commit d2bf1c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
26 changes: 21 additions & 5 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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())
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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()
Expand All @@ -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{})
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions p2p/host/autonat/dialpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"))
Expand Down
31 changes: 26 additions & 5 deletions p2p/host/autonat/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -83,13 +91,17 @@ 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
c.throttlePeerMax = 1
_ = makeAutoNATService(t, c)

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

_, err := ac.DialBack(ctx, c.host.ID())
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d2bf1c6

Please sign in to comment.