Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

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 0058028 commit 96e55da
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 117 deletions.
26 changes: 21 additions & 5 deletions 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 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
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/libp2p/go-libp2p-autonat
go 1.16

require (
github.com/gogo/protobuf v1.3.1
github.com/ipfs/go-log v1.0.4
github.com/gogo/protobuf v1.3.2
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-eventbus v0.2.1
github.com/libp2p/go-libp2p-blankhost v0.2.0
github.com/libp2p/go-libp2p-core v0.7.0
github.com/libp2p/go-libp2p-swarm v0.3.0
github.com/libp2p/go-libp2p-core v0.11.0
github.com/libp2p/go-libp2p-swarm v0.8.0
github.com/libp2p/go-msgio v0.0.6
github.com/multiformats/go-multiaddr v0.3.1
github.com/multiformats/go-multiaddr v0.4.0
)
Loading

0 comments on commit 96e55da

Please sign in to comment.