diff --git a/dial_test.go b/dial_test.go index 3bbc5e2b..5fe4bdc2 100644 --- a/dial_test.go +++ b/dial_test.go @@ -7,21 +7,21 @@ 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() { @@ -36,50 +36,37 @@ func closeSwarms(swarms []*Swarm) { 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() } @@ -104,7 +91,7 @@ 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 { @@ -175,7 +162,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() @@ -215,7 +202,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() @@ -422,7 +409,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() @@ -491,7 +478,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] @@ -530,7 +517,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] @@ -574,7 +561,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() @@ -676,12 +663,10 @@ func TestDialSelf(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") } diff --git a/go.mod b/go.mod index 7264aa52..f7593131 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,13 @@ 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.9.1-0.20210824123029-094b0d3f8ba2 + github.com/libp2p/go-conn-security-multistream v0.3.0 + github.com/libp2p/go-libp2p-core v0.10.0 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 - github.com/libp2p/go-libp2p-transport-upgrader v0.4.6 + github.com/libp2p/go-libp2p-quic-transport v0.13.0 + github.com/libp2p/go-libp2p-testing v0.5.0 + github.com/libp2p/go-libp2p-transport-upgrader v0.5.0 github.com/libp2p/go-libp2p-yamux v0.5.0 github.com/libp2p/go-maddr-filter v0.1.0 github.com/libp2p/go-stream-muxer-multistream v0.3.0 diff --git a/go.sum b/go.sum index c26f4430..9f2eff36 100644 --- a/go.sum +++ b/go.sum @@ -39,11 +39,9 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= -github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8= github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= -github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= @@ -209,7 +207,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= -github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY= @@ -233,7 +230,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= @@ -275,12 +271,11 @@ github.com/libp2p/go-addr-util v0.1.0/go.mod h1:6I3ZYuFr2O/9D+SoyM0zEw0EF3YkldtT github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ= github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= -github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= -github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= +github.com/libp2p/go-conn-security-multistream v0.3.0 h1:9UCIKlBL1hC9u7nkMXpD1nkc/T53PKMAn3/k9ivBAVc= +github.com/libp2p/go-conn-security-multistream v0.3.0/go.mod h1:EEP47t4fw/bTelVmEzIDqSe69hO/ip52xBEhZMLWAHM= github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= -github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= github.com/libp2p/go-libp2p-core v0.5.1/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= @@ -288,25 +283,27 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB 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/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= -github.com/libp2p/go-libp2p-core v0.9.1-0.20210824123029-094b0d3f8ba2 h1:bjiRhvcmpNh7CcBsQZWbvXeik/babUsrJhPaw1Jvbdw= -github.com/libp2p/go-libp2p-core v0.9.1-0.20210824123029-094b0d3f8ba2/go.mod h1:QQrJQ1IV5nm3/nEeCApfbuwlqzKDCJPRvRoS7Ob5NmQ= +github.com/libp2p/go-libp2p-core v0.10.0 h1:jFy7v5Muq58GTeYkPhGzIH8Qq4BFfziqc0ixPd/pP9k= +github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= 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= github.com/libp2p/go-libp2p-peerstore v0.2.8/go.mod h1:gGiPlXdz7mIHd2vfAsHzBNAMqSDkt2UBFwgcITgw1lA= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= -github.com/libp2p/go-libp2p-quic-transport v0.11.2 h1:p1YQDZRHH4Cv2LPtHubqlQ9ggz4CKng/REZuXZbZMhM= -github.com/libp2p/go-libp2p-quic-transport v0.11.2/go.mod h1:wlanzKtIh6pHrq+0U3p3DY9PJfGqxMgPaGKaK5LifwQ= +github.com/libp2p/go-libp2p-quic-transport v0.13.0 h1:MTVojS4AnGD/rng6rF/HXEqwMHL27rHUEf3DaqSdnUw= +github.com/libp2p/go-libp2p-quic-transport v0.13.0/go.mod h1:39/ZWJ1TW/jx1iFkKzzUg00W6tDJh73FC0xYudjr7Hc= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-testing v0.4.2 h1:IOiA5mMigi+eEjf4J+B7fepDhsjtsoWA9QbsCqbNp5U= github.com/libp2p/go-libp2p-testing v0.4.2/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-tls v0.1.3 h1:twKMhMu44jQO+HgQK9X8NHO5HkeJu2QbhLzLJpa8oNM= -github.com/libp2p/go-libp2p-tls v0.1.3/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M= -github.com/libp2p/go-libp2p-transport-upgrader v0.4.6 h1:SHt3g0FslnqIkEWF25YOB8UCOCTpGAVvHRWQYJ+veiI= +github.com/libp2p/go-libp2p-testing v0.5.0 h1:bTjC29TTQ/ODq0ld3+0KLq3irdA5cAH3OMbRi0/QsvE= +github.com/libp2p/go-libp2p-testing v0.5.0/go.mod h1:QBk8fqIL1XNcno/l3/hhaIEn4aLRijpYOR+zVjjlh+A= +github.com/libp2p/go-libp2p-tls v0.3.0 h1:8BgvUJiOTcj0Gp6XvEicF0rL5aUtRg/UzEdeZDmDlC8= +github.com/libp2p/go-libp2p-tls v0.3.0/go.mod h1:fwF5X6PWGxm6IDRwF3V8AVCCj/hOd5oFlg+wo2FxJDY= github.com/libp2p/go-libp2p-transport-upgrader v0.4.6/go.mod h1:JE0WQuQdy+uLZ5zOaI3Nw9dWGYJIA7mywEtP2lMvnyk= +github.com/libp2p/go-libp2p-transport-upgrader v0.5.0 h1:7SDl3O2+AYOgfE40Mis83ClpfGNkNA6m4FwhbOHs+iI= +github.com/libp2p/go-libp2p-transport-upgrader v0.5.0/go.mod h1:Rc+XODlB3yce7dvFV4q/RmyJGsFcCZRkeZMu/Zdg0mo= github.com/libp2p/go-libp2p-yamux v0.5.0 h1:ZzmUhbQE+X7NuYUT2naxN31JyebZfRmpZVhKtRP13ys= github.com/libp2p/go-libp2p-yamux v0.5.0/go.mod h1:AyR8k5EzyM2QN9Bbdg6X1SkVVuqLwTGf0L4DFq9g6po= github.com/libp2p/go-maddr-filter v0.1.0 h1:4ACqZKw8AqiuJfwFGq1CYDFugfXTOos+qQ3DETkhtCE= @@ -337,8 +334,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= -github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= -github.com/lucas-clemente/quic-go v0.21.2/go.mod h1:vF5M1XqhBAHgbjKcJOXY3JZz3GP0T3FQhz/uyOUS38Q= +github.com/lucas-clemente/quic-go v0.23.0 h1:5vFnKtZ6nHDFsc/F3uuiF4T3y/AXaQdxjUqiVw26GZE= +github.com/lucas-clemente/quic-go v0.23.0/go.mod h1:paZuzjXCE5mj6sikVLMvqXk8lJV2AsqtJ6bDhjEfxx0= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= @@ -346,12 +343,10 @@ github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= github.com/marten-seemann/qtls-go1-15 v0.1.4/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= -github.com/marten-seemann/qtls-go1-15 v0.1.5 h1:Ci4EIUN6Rlb+D6GmLdej/bCQ4nPYNtVXQB+xjiXE1nk= -github.com/marten-seemann/qtls-go1-15 v0.1.5/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I= github.com/marten-seemann/qtls-go1-16 v0.1.4 h1:xbHbOGGhrenVtII6Co8akhLEdrawwB2iHl5yhJRpnco= github.com/marten-seemann/qtls-go1-16 v0.1.4/go.mod h1:gNpI2Ol+lRS3WwSOtIUUtRwZEQMXjYK+dQSBFbethAk= -github.com/marten-seemann/qtls-go1-17 v0.1.0-rc.1 h1:/rpmWuGvceLwwWuaKPdjpR4JJEUH0tq64/I3hvzaNLM= -github.com/marten-seemann/qtls-go1-17 v0.1.0-rc.1/go.mod h1:fz4HIxByo+LlWcreM4CZOYNuz3taBQ8rN2X6FqvaWo8= +github.com/marten-seemann/qtls-go1-17 v0.1.0 h1:P9ggrs5xtwiqXv/FHNwntmuLMNq3KaSIG93AtAZ48xk= +github.com/marten-seemann/qtls-go1-17 v0.1.0/go.mod h1:fz4HIxByo+LlWcreM4CZOYNuz3taBQ8rN2X6FqvaWo8= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd/go.mod h1:QuCEs1Nt24+FYQEqAAncTDPJIuGs+LxK1MCiFL25pMU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -391,7 +386,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= -github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= @@ -400,7 +394,6 @@ github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= -github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo= github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4= github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z7tZ04iMk5wP4QMGGE= @@ -449,6 +442,7 @@ github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQ github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvwxz1E= @@ -456,6 +450,7 @@ github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak= github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY= @@ -561,7 +556,6 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= -github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU= github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -639,7 +633,6 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -731,7 +724,6 @@ golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190526052359-791d8a0f4d09/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -739,6 +731,7 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191206220618-eeba5f6aabab/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/peers_test.go b/peers_test.go index 8e82bf5b..3145d862 100644 --- a/peers_test.go +++ b/peers_test.go @@ -15,7 +15,7 @@ import ( func TestPeers(t *testing.T) { ctx := context.Background() - swarms := makeSwarms(ctx, t, 2) + swarms := makeSwarms(t, 2) s1 := swarms[0] s2 := swarms[1] diff --git a/simul_test.go b/simul_test.go index 0373e37d..aa4eb590 100644 --- a/simul_test.go +++ b/simul_test.go @@ -18,11 +18,8 @@ import ( ) 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 { @@ -32,7 +29,7 @@ func TestSimultOpen(t *testing.T) { // 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) } } diff --git a/swarm.go b/swarm.go index c34497f7..43f7a36f 100644 --- a/swarm.go +++ b/swarm.go @@ -18,8 +18,6 @@ import ( "github.com/libp2p/go-libp2p-core/transport" logging "github.com/ipfs/go-log" - "github.com/jbenet/goprocess" - goprocessctx "github.com/jbenet/goprocess/context" ma "github.com/multiformats/go-multiaddr" ) @@ -92,9 +90,11 @@ type Swarm struct { limiter *dialLimiter gater connmgr.ConnectionGater - proc goprocess.Process - ctx context.Context - bwc metrics.Reporter + closeOnce sync.Once + ctx context.Context // is canceled when Close is called + ctxCancel context.CancelFunc + + bwc metrics.Reporter } // NewSwarm constructs a Swarm. @@ -103,11 +103,14 @@ type Swarm struct { // `extra` interface{} parameter facilitates the future migration. Supported // elements are: // - connmgr.ConnectionGater -func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, extra ...interface{}) *Swarm { +func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, extra ...interface{}) *Swarm { + ctx, cancel := context.WithCancel(context.Background()) s := &Swarm{ - local: local, - peers: peers, - bwc: bwc, + local: local, + peers: peers, + bwc: bwc, + ctx: ctx, + ctxCancel: cancel, } s.conns.m = make(map[peer.ID][]*Conn) @@ -124,25 +127,19 @@ func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc s.dsync = newDialSync(s.dialWorkerLoop) s.limiter = newDialLimiter(s.dialAddr) - s.proc = goprocessctx.WithContext(ctx) - s.ctx = goprocessctx.OnClosingContext(s.proc) s.backf.init(s.ctx) - - // Set teardown after setting the context/process so we don't start the - // teardown process early. - s.proc.SetTeardown(s.teardown) - return s } -func (s *Swarm) teardown() error { - // Wait for the context to be canceled. - // This allows other parts of the swarm to detect that we're shutting - // down. - <-s.ctx.Done() +func (s *Swarm) Close() error { + s.closeOnce.Do(s.close) + return nil +} - // Prevents new connections and/or listeners from being added to the swarm. +func (s *Swarm) close() { + s.ctxCancel() + // Prevents new connections and/or listeners from being added to the swarm. s.listeners.Lock() listeners := s.listeners.m s.listeners.m = nil @@ -197,13 +194,6 @@ func (s *Swarm) teardown() error { } } wg.Wait() - - return nil -} - -// Process returns the Process of the swarm -func (s *Swarm) Process() goprocess.Process { - return s.proc } func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn, error) { @@ -293,16 +283,6 @@ func (s *Swarm) Peerstore() peerstore.Peerstore { return s.peers } -// Context returns the context of the swarm -func (s *Swarm) Context() context.Context { - return s.ctx -} - -// Close stops the Swarm. -func (s *Swarm) Close() error { - return s.proc.Close() -} - // TODO: We probably don't need the conn handlers. // SetConnHandler assigns the handler for new connections. diff --git a/swarm_addr_test.go b/swarm_addr_test.go index baeac462..21ecafa2 100644 --- a/swarm_addr_test.go +++ b/swarm_addr_test.go @@ -6,6 +6,7 @@ import ( "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/test" + "github.com/stretchr/testify/require" ma "github.com/multiformats/go-multiaddr" @@ -13,7 +14,6 @@ import ( ) func TestDialBadAddrs(t *testing.T) { - m := func(s string) ma.Multiaddr { maddr, err := ma.NewMultiaddr(s) if err != nil { @@ -22,13 +22,12 @@ func TestDialBadAddrs(t *testing.T) { return maddr } - ctx := context.Background() - s := makeSwarms(ctx, t, 1)[0] + s := makeSwarms(t, 1)[0] test := func(a ma.Multiaddr) { p := test.RandPeerIDFatal(t) s.Peerstore().AddAddr(p, a, peerstore.PermanentAddrTTL) - if _, err := s.DialPeer(ctx, p); err == nil { + if _, err := s.DialPeer(context.Background(), p); err == nil { t.Errorf("swarm should not dial: %s", p) } } @@ -39,19 +38,13 @@ func TestDialBadAddrs(t *testing.T) { } func TestAddrRace(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := makeSwarms(ctx, t, 1)[0] + s := makeSwarms(t, 1)[0] defer s.Close() a1, err := s.InterfaceListenAddresses() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) a2, err := s.InterfaceListenAddresses() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if len(a1) > 0 && len(a2) > 0 && &a1[0] == &a2[0] { t.Fatal("got the exact same address set twice; this could lead to data races") @@ -59,15 +52,8 @@ func TestAddrRace(t *testing.T) { } func TestAddressesWithoutListening(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := swarmt.GenSwarm(t, ctx, swarmt.OptDialOnly) - + s := swarmt.GenSwarm(t, swarmt.OptDialOnly) a1, err := s.InterfaceListenAddresses() - if err != nil { - t.Fatal(err) - } - if len(a1) != 0 { - t.Fatalf("expected to be listening on no addresses, was listening on %d", len(a1)) - } + require.NoError(t, err) + require.Empty(t, a1, "expected to be listening on no addresses") } diff --git a/swarm_listen.go b/swarm_listen.go index c064ae85..ca54280c 100644 --- a/swarm_listen.go +++ b/swarm_listen.go @@ -46,7 +46,7 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error { // // Distinguish between these two cases to avoid confusing users. select { - case <-s.proc.Closing(): + case <-s.ctx.Done(): return ErrSwarmClosed default: return ErrNoTransport diff --git a/swarm_net_test.go b/swarm_net_test.go index 05984f6b..1f1d0454 100644 --- a/swarm_net_test.go +++ b/swarm_net_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/libp2p/go-libp2p-core/network" . "github.com/libp2p/go-libp2p-swarm/testing" @@ -15,19 +17,16 @@ import ( // TestConnectednessCorrect starts a few networks, connects a few // and tests Connectedness value is correct. func TestConnectednessCorrect(t *testing.T) { - - ctx := context.Background() - nets := make([]network.Network, 4) for i := 0; i < 4; i++ { - nets[i] = GenSwarm(t, ctx) + nets[i] = GenSwarm(t) } // connect 0-1, 0-2, 0-3, 1-2, 2-3 dial := func(a, b network.Network) { DivulgeAddresses(b, a) - if _, err := a.DialPeer(ctx, b.LocalPeer()); err != nil { + if _, err := a.DialPeer(context.Background(), b.LocalPeer()); err != nil { t.Fatalf("Failed to dial: %s", err) } } @@ -54,33 +53,17 @@ func TestConnectednessCorrect(t *testing.T) { expectConnectedness(t, nets[0], nets[2], network.NotConnected) expectConnectedness(t, nets[1], nets[3], network.NotConnected) - if len(nets[0].Peers()) != 2 { - t.Fatal("expected net 0 to have two peers") - } - - if len(nets[2].Peers()) != 2 { - t.Fatal("expected net 2 to have two peers") - } - - if len(nets[1].ConnsToPeer(nets[3].LocalPeer())) != 0 { - t.Fatal("net 1 should have no connections to net 3") - } - - if err := nets[2].ClosePeer(nets[1].LocalPeer()); err != nil { - t.Fatal(err) - } + require.Len(t, nets[0].Peers(), 2, "expected net 0 to have two peers") + require.Len(t, nets[2].Peers(), 2, "expected net 2 to have two peers") + require.NotZerof(t, nets[1].ConnsToPeer(nets[3].LocalPeer()), "net 1 should have no connections to net 3") + require.NoError(t, nets[2].ClosePeer(nets[1].LocalPeer())) time.Sleep(time.Millisecond * 50) - expectConnectedness(t, nets[2], nets[1], network.NotConnected) for _, n := range nets { n.Close() } - - for _, n := range nets { - <-n.Process().Closed() - } } func expectConnectedness(t *testing.T, a, b network.Network, expected network.Connectedness) { @@ -113,7 +96,7 @@ func TestNetworkOpenStream(t *testing.T) { nets := make([]network.Network, 4) for i := 0; i < 4; i++ { - nets[i] = GenSwarm(t, ctx) + nets[i] = GenSwarm(t) } dial := func(a, b network.Network) { diff --git a/swarm_notif_test.go b/swarm_notif_test.go index 33836172..c0c6f82d 100644 --- a/swarm_notif_test.go +++ b/swarm_notif_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" @@ -18,8 +20,7 @@ func TestNotifications(t *testing.T) { notifiees := make([]*netNotifiee, swarmSize) - ctx := context.Background() - swarms := makeSwarms(ctx, t, swarmSize) + swarms := makeSwarms(t, swarmSize) defer func() { for i, s := range swarms { select { @@ -27,10 +28,7 @@ func TestNotifications(t *testing.T) { t.Error("should not have been closed") default: } - err := s.Close() - if err != nil { - t.Error(err) - } + require.NoError(t, s.Close()) select { case <-notifiees[i].listenClose: default: @@ -48,7 +46,7 @@ func TestNotifications(t *testing.T) { notifiees[i] = n } - connectSwarms(t, ctx, swarms) + connectSwarms(t, context.Background(), swarms) time.Sleep(50 * time.Millisecond) // should've gotten 5 by now. diff --git a/swarm_test.go b/swarm_test.go index a94281b1..9f799f66 100644 --- a/swarm_test.go +++ b/swarm_test.go @@ -58,27 +58,23 @@ func EchoStreamHandler(stream network.Stream) { }() } -func makeDialOnlySwarm(ctx context.Context, t *testing.T) *Swarm { - swarm := GenSwarm(t, ctx, OptDialOnly) +func makeDialOnlySwarm(t *testing.T) *Swarm { + swarm := GenSwarm(t, OptDialOnly) swarm.SetStreamHandler(EchoStreamHandler) - return swarm } -func makeSwarms(ctx context.Context, t *testing.T, num int, opts ...Option) []*Swarm { +func makeSwarms(t *testing.T, num int, opts ...Option) []*Swarm { swarms := make([]*Swarm, 0, num) - for i := 0; i < num; i++ { - swarm := GenSwarm(t, ctx, opts...) + swarm := GenSwarm(t, opts...) swarm.SetStreamHandler(EchoStreamHandler) swarms = append(swarms, swarm) } - return swarms } func connectSwarms(t *testing.T, ctx context.Context, swarms []*Swarm) { - var wg sync.WaitGroup connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) { // TODO: make a DialAddr func. @@ -104,13 +100,10 @@ func connectSwarms(t *testing.T, ctx context.Context, swarms []*Swarm) { } func SubtestSwarm(t *testing.T, SwarmNum int, MsgNum int) { - // t.Skip("skipping for another test") - - ctx := context.Background() - swarms := makeSwarms(ctx, t, SwarmNum, OptDisableReuseport) + swarms := makeSwarms(t, SwarmNum, OptDisableReuseport) // connect everyone - connectSwarms(t, ctx, swarms) + connectSwarms(t, context.Background(), swarms) // ping/pong for _, s1 := range swarms { @@ -118,7 +111,7 @@ func SubtestSwarm(t *testing.T, SwarmNum int, MsgNum int) { log.Debugf("%s ping pong round", s1.LocalPeer()) log.Debugf("-------------------------------------------------------") - _, cancel := context.WithCancel(ctx) + _, cancel := context.WithCancel(context.Background()) got := map[peer.ID]int{} errChan := make(chan error, MsgNum*len(swarms)) streamChan := make(chan network.Stream, MsgNum) @@ -132,7 +125,7 @@ func SubtestSwarm(t *testing.T, SwarmNum int, MsgNum int) { defer wg.Done() // first, one stream per peer (nice) - stream, err := s1.NewStream(ctx, p) + stream, err := s1.NewStream(context.Background(), p) if err != nil { errChan <- err return @@ -253,7 +246,7 @@ func TestConnHandler(t *testing.T) { t.Parallel() ctx := context.Background() - swarms := makeSwarms(ctx, t, 5) + swarms := makeSwarms(t, 5) gotconn := make(chan struct{}, 10) swarms[0].SetConnHandler(func(conn network.Conn) { @@ -387,8 +380,8 @@ func TestConnectionGating(t *testing.T) { p2Gater = tc.p2Gater(p2Gater) } - sw1 := GenSwarm(t, ctx, OptConnGater(p1Gater), optTransport) - sw2 := GenSwarm(t, ctx, OptConnGater(p2Gater), optTransport) + sw1 := GenSwarm(t, OptConnGater(p1Gater), optTransport) + sw2 := GenSwarm(t, OptConnGater(p2Gater), optTransport) p1 := sw1.LocalPeer() p2 := sw2.LocalPeer() @@ -408,10 +401,9 @@ func TestConnectionGating(t *testing.T) { } func TestNoDial(t *testing.T) { - ctx := context.Background() - swarms := makeSwarms(ctx, t, 2) + swarms := makeSwarms(t, 2) - _, err := swarms[0].NewStream(network.WithNoDial(ctx, "swarm test"), swarms[1].LocalPeer()) + _, err := swarms[0].NewStream(network.WithNoDial(context.Background(), "swarm test"), swarms[1].LocalPeer()) if err != network.ErrNoConn { t.Fatal("should have failed with ErrNoConn") } @@ -419,36 +411,29 @@ func TestNoDial(t *testing.T) { func TestCloseWithOpenStreams(t *testing.T) { ctx := context.Background() - swarms := makeSwarms(ctx, t, 2) + swarms := makeSwarms(t, 2) connectSwarms(t, ctx, swarms) s, err := swarms[0].NewStream(ctx, swarms[1].LocalPeer()) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer s.Close() // close swarm before stream. - err = swarms[0].Close() - if err != nil { - t.Fatal(err) - } + require.NoError(t, swarms[0].Close()) } func TestTypedNilConn(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := GenSwarm(t, ctx) + s := GenSwarm(t) defer s.Close() // We can't dial ourselves. - c, err := s.DialPeer(ctx, s.LocalPeer()) + c, err := s.DialPeer(context.Background(), s.LocalPeer()) require.Error(t, err) // If we fail to dial, the connection should be nil. - require.True(t, c == nil) + require.Nil(t, c) } func TestPreventDialListenAddr(t *testing.T) { - s := GenSwarm(t, context.Background(), OptDialOnly) + s := GenSwarm(t, OptDialOnly) if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil { t.Fatal(err) } diff --git a/testing/testing.go b/testing/testing.go index ba517769..201b4f0f 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -1,7 +1,6 @@ package testing import ( - "context" "testing" csms "github.com/libp2p/go-conn-security-multistream" @@ -22,7 +21,6 @@ import ( msmux "github.com/libp2p/go-stream-muxer-multistream" "github.com/libp2p/go-tcp-transport" - "github.com/jbenet/goprocess" ma "github.com/multiformats/go-multiaddr" ) @@ -89,7 +87,7 @@ func GenUpgrader(n *swarm.Swarm) *tptu.Upgrader { } // GenSwarm generates a new test swarm. -func GenSwarm(t *testing.T, ctx context.Context, opts ...Option) *swarm.Swarm { +func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm { var cfg config for _, o := range opts { o(t, &cfg) @@ -113,11 +111,9 @@ func GenSwarm(t *testing.T, ctx context.Context, opts ...Option) *swarm.Swarm { ps := pstoremem.NewPeerstore() ps.AddPubKey(p.ID, p.PubKey) ps.AddPrivKey(p.ID, p.PrivKey) - s := swarm.NewSwarm(ctx, p.ID, ps, metrics.NewBandwidthCounter(), cfg.connectionGater) + t.Cleanup(func() { ps.Close() }) - // Call AddChildNoWait because we can't call AddChild after the process - // may have been closed (e.g., if the context was canceled). - s.Process().AddChildNoWait(goprocess.WithTeardown(ps.Close)) + s := swarm.NewSwarm(p.ID, ps, metrics.NewBandwidthCounter(), cfg.connectionGater) upgrader := GenUpgrader(s) upgrader.ConnGater = cfg.connectionGater diff --git a/testing/testing_test.go b/testing/testing_test.go index a80cca17..60cd2787 100644 --- a/testing/testing_test.go +++ b/testing/testing_test.go @@ -1,14 +1,13 @@ package testing import ( - "context" "testing" "github.com/stretchr/testify/require" ) func TestGenSwarm(t *testing.T) { - swarm := GenSwarm(t, context.Background()) + swarm := GenSwarm(t) require.NoError(t, swarm.Close()) GenUpgrader(swarm) } diff --git a/transport_test.go b/transport_test.go index 82225840..6d5913cf 100644 --- a/transport_test.go +++ b/transport_test.go @@ -9,7 +9,10 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/transport" + ma "github.com/multiformats/go-multiaddr" + + "github.com/stretchr/testify/require" ) type dummyTransport struct { @@ -43,34 +46,22 @@ func (dt *dummyTransport) Close() error { } func TestUselessTransport(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := swarmt.GenSwarm(t, ctx) - err := s.AddTransport(new(dummyTransport)) - if err == nil { - t.Fatal("adding a transport that supports no protocols should have failed") - } + s := swarmt.GenSwarm(t) + require.Error(t, s.AddTransport(new(dummyTransport)), "adding a transport that supports no protocols should have failed") } func TestTransportClose(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := swarmt.GenSwarm(t, ctx) + s := swarmt.GenSwarm(t) tpt := &dummyTransport{protocols: []int{1}} - if err := s.AddTransport(tpt); err != nil { - t.Fatal(err) - } + require.NoError(t, s.AddTransport(tpt)) _ = s.Close() if !tpt.closed { t.Fatal("expected transport to be closed") } - } func TestTransportAfterClose(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := swarmt.GenSwarm(t, ctx) + s := swarmt.GenSwarm(t) s.Close() tpt := &dummyTransport{protocols: []int{1}}