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

Commit

Permalink
stop using goprocess to control teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 5, 2021
1 parent 29bd937 commit 2f4278a
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 225 deletions.
2 changes: 1 addition & 1 deletion dial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestDialSelf(t *testing.T) {
defer cancel()

self := peer.ID("ABC")
s := NewSwarm(ctx, self, nil, nil)
s := NewSwarm(self, nil, nil)
defer s.Close()

// this should fail
Expand Down
86 changes: 38 additions & 48 deletions dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,71 @@ import (
"testing"
"time"

addrutil "github.com/libp2p/go-addr-util"
. "github.com/libp2p/go-libp2p-swarm"

addrutil "github.com/libp2p/go-addr-util"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/transport"

testutil "github.com/libp2p/go-libp2p-core/test"
"github.com/libp2p/go-libp2p-core/transport"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-libp2p-testing/ci"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"

. "github.com/libp2p/go-libp2p-swarm"
"github.com/stretchr/testify/require"
)

func init() {
transport.DialTimeout = time.Second
}

func closeSwarms(swarms []*Swarm) {
type swarmWithBackoff interface {
network.Network
Backoff() *DialBackoff
}

func closeSwarms(swarms []network.Network) {
for _, s := range swarms {
s.Close()
}
}

func TestBasicDialPeer(t *testing.T) {
t.Parallel()
ctx := context.Background()

swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
defer closeSwarms(swarms)
s1 := swarms[0]
s2 := swarms[1]

s1.Peerstore().AddAddrs(s2.LocalPeer(), s2.ListenAddresses(), peerstore.PermanentAddrTTL)

c, err := s1.DialPeer(ctx, s2.LocalPeer())
if err != nil {
t.Fatal(err)
}

s, err := c.NewStream(ctx)
if err != nil {
t.Fatal(err)
}
c, err := s1.DialPeer(context.Background(), s2.LocalPeer())
require.NoError(t, err)

s, err := c.NewStream(context.Background())
require.NoError(t, err)
s.Close()
}

func TestDialWithNoListeners(t *testing.T) {
t.Parallel()
ctx := context.Background()

s1 := makeDialOnlySwarm(ctx, t)

swarms := makeSwarms(ctx, t, 1)
s1 := makeDialOnlySwarm(t)
swarms := makeSwarms(t, 1)
defer closeSwarms(swarms)
s2 := swarms[0]

s1.Peerstore().AddAddrs(s2.LocalPeer(), s2.ListenAddresses(), peerstore.PermanentAddrTTL)

c, err := s1.DialPeer(ctx, s2.LocalPeer())
if err != nil {
t.Fatal(err)
}

s, err := c.NewStream(ctx)
if err != nil {
t.Fatal(err)
}
c, err := s1.DialPeer(context.Background(), s2.LocalPeer())
require.NoError(t, err)

s, err := c.NewStream(context.Background())
require.NoError(t, err)
s.Close()
}

Expand All @@ -104,12 +96,12 @@ func TestSimultDials(t *testing.T) {
t.Parallel()

ctx := context.Background()
swarms := makeSwarms(ctx, t, 2, swarmt.OptDisableReuseport)
swarms := makeSwarms(t, 2, swarmt.OptDisableReuseport)

// connect everyone
{
var wg sync.WaitGroup
connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
connect := func(s network.Network, dst peer.ID, addr ma.Multiaddr) {
// copy for other peer
log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.LocalPeer(), dst, addr)
s.Peerstore().AddAddr(dst, addr, peerstore.TempAddrTTL)
Expand Down Expand Up @@ -175,7 +167,7 @@ func TestDialWait(t *testing.T) {
t.Parallel()

ctx := context.Background()
swarms := makeSwarms(ctx, t, 1)
swarms := makeSwarms(t, 1)
s1 := swarms[0]
defer s1.Close()

Expand All @@ -201,7 +193,7 @@ func TestDialWait(t *testing.T) {
t.Error("> 2*transport.DialTimeout * DialAttempts not being respected", duration, 2*transport.DialTimeout*DialAttempts)
}

if !s1.Backoff().Backoff(s2p, s2addr) {
if !s1.(swarmWithBackoff).Backoff().Backoff(s2p, s2addr) {
t.Error("s2 should now be on backoff")
}
}
Expand All @@ -215,7 +207,7 @@ func TestDialBackoff(t *testing.T) {
t.Parallel()

ctx := context.Background()
swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
s1 := swarms[0]
s2 := swarms[1]
defer s1.Close()
Expand Down Expand Up @@ -338,10 +330,10 @@ func TestDialBackoff(t *testing.T) {
}

// check backoff state
if s1.Backoff().Backoff(s2.LocalPeer(), s2addrs[0]) {
if s1.(swarmWithBackoff).Backoff().Backoff(s2.LocalPeer(), s2addrs[0]) {
t.Error("s2 should not be on backoff")
}
if !s1.Backoff().Backoff(s3p, s3addr) {
if !s1.(swarmWithBackoff).Backoff().Backoff(s3p, s3addr) {
t.Error("s3 should be on backoff")
}

Expand Down Expand Up @@ -408,10 +400,10 @@ func TestDialBackoff(t *testing.T) {
}

// check backoff state (the same)
if s1.Backoff().Backoff(s2.LocalPeer(), s2addrs[0]) {
if s1.(swarmWithBackoff).Backoff().Backoff(s2.LocalPeer(), s2addrs[0]) {
t.Error("s2 should not be on backoff")
}
if !s1.Backoff().Backoff(s3p, s3addr) {
if !s1.(swarmWithBackoff).Backoff().Backoff(s3p, s3addr) {
t.Error("s3 should be on backoff")
}
}
Expand All @@ -422,7 +414,7 @@ func TestDialBackoffClears(t *testing.T) {
t.Parallel()

ctx := context.Background()
swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
s1 := swarms[0]
s2 := swarms[1]
defer s1.Close()
Expand Down Expand Up @@ -453,7 +445,7 @@ func TestDialBackoffClears(t *testing.T) {
t.Error("> 2*transport.DialTimeout * DialAttempts not being respected", duration, 2*transport.DialTimeout*DialAttempts)
}

if !s1.Backoff().Backoff(s2.LocalPeer(), s2bad) {
if !s1.(swarmWithBackoff).Backoff().Backoff(s2.LocalPeer(), s2bad) {
t.Error("s2 should now be on backoff")
} else {
t.Log("correctly added to backoff")
Expand All @@ -480,7 +472,7 @@ func TestDialBackoffClears(t *testing.T) {
t.Log("correctly connected")
}

if s1.Backoff().Backoff(s2.LocalPeer(), s2bad) {
if s1.(swarmWithBackoff).Backoff().Backoff(s2.LocalPeer(), s2bad) {
t.Error("s2 should no longer be on backoff")
} else {
t.Log("correctly cleared backoff")
Expand All @@ -491,7 +483,7 @@ func TestDialPeerFailed(t *testing.T) {
t.Parallel()
ctx := context.Background()

swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
defer closeSwarms(swarms)
testedSwarm, targetSwarm := swarms[0], swarms[1]

Expand Down Expand Up @@ -530,7 +522,7 @@ func TestDialPeerFailed(t *testing.T) {
func TestDialExistingConnection(t *testing.T) {
ctx := context.Background()

swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
defer closeSwarms(swarms)
s1 := swarms[0]
s2 := swarms[1]
Expand Down Expand Up @@ -574,7 +566,7 @@ func TestDialSimultaneousJoin(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
s1 := swarms[0]
s2 := swarms[1]
defer s1.Close()
Expand Down Expand Up @@ -676,12 +668,10 @@ func TestDialSelf2(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
s1 := swarms[0]
defer s1.Close()

_, err := s1.DialPeer(ctx, s1.LocalPeer())
if err != ErrDialToSelf {
t.Fatal("expected error from self dial")
}
require.ErrorIs(t, err, ErrDialToSelf, "expected error from self dial")
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ go 1.16

require (
github.com/ipfs/go-log v1.0.5
github.com/jbenet/goprocess v0.1.4
github.com/libp2p/go-addr-util v0.1.0
github.com/libp2p/go-conn-security-multistream v0.2.1
github.com/libp2p/go-libp2p-core v0.8.6
github.com/libp2p/go-libp2p-core v0.9.1-0.20210905173309-045ce33f287b
github.com/libp2p/go-libp2p-peerstore v0.2.8
github.com/libp2p/go-libp2p-quic-transport v0.11.2
github.com/libp2p/go-libp2p-testing v0.4.2
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down Expand Up @@ -234,7 +233,6 @@ github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABo
github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk=
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY=
github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down Expand Up @@ -288,8 +286,9 @@ github.com/libp2p/go-libp2p-core v0.5.1/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt
github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.8.6 h1:3S8g006qG6Tjpj1JdRK2S+TWc2DJQKX/RG9fdLeiLSU=
github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM=
github.com/libp2p/go-libp2p-core v0.9.1-0.20210905173309-045ce33f287b h1:Rc/KIaoWLFumEDUm0oMkpMKG+ASa5YzpZJQaF1zfZV0=
github.com/libp2p/go-libp2p-core v0.9.1-0.20210905173309-045ce33f287b/go.mod h1:j3WKs+bvJ5a1/WEe8IFouxQQltzZQWDOKL0MWT8C0Eo=
github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc=
github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g=
github.com/libp2p/go-libp2p-peerstore v0.2.8 h1:nJghUlUkFVvyk7ccsM67oFA6kqUkwyCM1G4WPVMCWYA=
Expand Down Expand Up @@ -611,7 +610,6 @@ go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
Expand Down
8 changes: 3 additions & 5 deletions peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import (
"github.com/libp2p/go-libp2p-core/peerstore"

ma "github.com/multiformats/go-multiaddr"

. "github.com/libp2p/go-libp2p-swarm"
)

func TestPeers(t *testing.T) {
ctx := context.Background()
swarms := makeSwarms(ctx, t, 2)
swarms := makeSwarms(t, 2)
s1 := swarms[0]
s2 := swarms[1]

connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
connect := func(s network.Network, dst peer.ID, addr ma.Multiaddr) {
// TODO: make a DialAddr func.
s.Peerstore().AddAddr(dst, addr, peerstore.PermanentAddrTTL)
// t.Logf("connections from %s", s.LocalPeer())
Expand Down Expand Up @@ -55,7 +53,7 @@ func TestPeers(t *testing.T) {
log.Infof("%s swarm routing table: %s", s.LocalPeer(), s.Peers())
}

test := func(s *Swarm) {
test := func(s network.Network) {
expect := 1
actual := len(s.Peers())
if actual != expect {
Expand Down
11 changes: 4 additions & 7 deletions simul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@ import (
"testing"
"time"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"

ma "github.com/multiformats/go-multiaddr"

. "github.com/libp2p/go-libp2p-swarm"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-libp2p-testing/ci"
)

func TestSimultOpen(t *testing.T) {

t.Parallel()

ctx := context.Background()
swarms := makeSwarms(ctx, t, 2, swarmt.OptDisableReuseport)
swarms := makeSwarms(t, 2, swarmt.OptDisableReuseport)

// connect everyone
{
var wg sync.WaitGroup
connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
connect := func(s network.Network, dst peer.ID, addr ma.Multiaddr) {
defer wg.Done()
// copy for other peer
log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.LocalPeer(), dst, addr)
s.Peerstore().AddAddr(dst, addr, peerstore.PermanentAddrTTL)
if _, err := s.DialPeer(ctx, dst); err != nil {
if _, err := s.DialPeer(context.Background(), dst); err != nil {
t.Error("error swarm dialing to peer", err)
}
}
Expand Down
Loading

0 comments on commit 2f4278a

Please sign in to comment.