diff --git a/pkg/file/joiner/joiner_test.go b/pkg/file/joiner/joiner_test.go index dfed2aecc91..27591252548 100644 --- a/pkg/file/joiner/joiner_test.go +++ b/pkg/file/joiner/joiner_test.go @@ -56,8 +56,7 @@ func TestJoinerSingleChunk(t *testing.T) { store := inmemchunkstore.New() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // create the chunk to mockAddrHex := fmt.Sprintf("%064s", "2a") @@ -95,8 +94,7 @@ func TestJoinerDecryptingStore_NormalChunk(t *testing.T) { st := inmemchunkstore.New() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // create the chunk to mockAddrHex := fmt.Sprintf("%064s", "2a") @@ -134,8 +132,7 @@ func TestJoinerWithReference(t *testing.T) { st := inmemchunkstore.New() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // create root chunk and two data chunks referenced in the root chunk rootChunk := filetest.GenerateTestRandomFileChunk(swarm.ZeroAddress, swarm.ChunkSize*2, swarm.SectionSize*2) @@ -185,8 +182,7 @@ func TestJoinerMalformed(t *testing.T) { store := inmemchunkstore.New() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() subTrie := []byte{8085: 1} pb := builder.NewPipelineBuilder(ctx, store, false, 0) @@ -253,8 +249,7 @@ func TestEncryptDecrypt(t *testing.T) { if err != nil { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() pipe := builder.NewPipelineBuilder(ctx, store, true, 0) testDataReader := bytes.NewReader(testData) resultAddress, err := builder.FeedPipeline(ctx, pipe, testDataReader) @@ -335,8 +330,7 @@ func TestSeek(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() store := inmemchunkstore.New() testutil.CleanupCloser(t, store) @@ -612,8 +606,7 @@ func TestPrefetch(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() store := inmemchunkstore.New() testutil.CleanupCloser(t, store) @@ -917,8 +910,7 @@ func TestJoinerIterateChunkAddresses_Encrypted(t *testing.T) { if err != nil { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() pipe := builder.NewPipelineBuilder(ctx, store, true, 0) testDataReader := bytes.NewReader(testData) resultAddress, err := builder.FeedPipeline(ctx, pipe, testDataReader) @@ -1072,8 +1064,7 @@ func TestJoinerRedundancy(t *testing.T) { }, } { t.Run(fmt.Sprintf("redundancy=%d encryption=%t", tc.rLevel, tc.encryptChunk), func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() shardCnt := tc.rLevel.GetMaxShards() parityCnt := tc.rLevel.GetParities(shardCnt) if tc.encryptChunk { diff --git a/pkg/file/splitter/internal/job_test.go b/pkg/file/splitter/internal/job_test.go index bc7945c4b42..7e9824d4748 100644 --- a/pkg/file/splitter/internal/job_test.go +++ b/pkg/file/splitter/internal/job_test.go @@ -29,8 +29,7 @@ func TestSplitterJobPartialSingleChunk(t *testing.T) { store := inmemchunkstore.New() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() data := []byte("foo") j := internal.NewSimpleSplitterJob(ctx, store, int64(len(data)), false) diff --git a/pkg/p2p/libp2p/connections_test.go b/pkg/p2p/libp2p/connections_test.go index c85b016d801..51227ab1555 100644 --- a/pkg/p2p/libp2p/connections_test.go +++ b/pkg/p2p/libp2p/connections_test.go @@ -59,8 +59,7 @@ func TestAddresses(t *testing.T) { func TestConnectDisconnect(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -88,8 +87,7 @@ func TestConnectDisconnect(t *testing.T) { func TestConnectToLightPeer(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, _ := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: false, @@ -110,8 +108,7 @@ func TestConnectToLightPeer(t *testing.T) { func TestLightPeerLimit(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() var ( limit = 3 @@ -160,8 +157,7 @@ func TestStreamsMaxIncomingLimit(t *testing.T) { maxIncomingStreams := 5000 - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -283,8 +279,7 @@ func TestStreamsMaxIncomingLimit(t *testing.T) { func TestDoubleConnect(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -311,8 +306,7 @@ func TestDoubleConnect(t *testing.T) { func TestDoubleDisconnect(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -347,8 +341,7 @@ func TestDoubleDisconnect(t *testing.T) { func TestMultipleConnectDisconnect(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -392,8 +385,7 @@ func TestMultipleConnectDisconnect(t *testing.T) { func TestConnectDisconnectOnAllAddresses(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -426,8 +418,7 @@ func TestConnectDisconnectOnAllAddresses(t *testing.T) { func TestDoubleConnectOnAllAddresses(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{ notifier: mockNotifier(noopCf, noopDf, true), @@ -471,8 +462,7 @@ func TestDoubleConnectOnAllAddresses(t *testing.T) { func TestDifferentNetworkIDs(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, _ := newService(t, 1, libp2pServiceOpts{}) s2, _ := newService(t, 2, libp2pServiceOpts{}) @@ -490,8 +480,7 @@ func TestDifferentNetworkIDs(t *testing.T) { func TestConnectWithEnabledWSTransports(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{ libp2pOpts: libp2p.Options{ @@ -521,8 +510,7 @@ func TestConnectWithEnabledWSTransports(t *testing.T) { func TestConnectRepeatHandshake(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -970,8 +958,7 @@ func TestWithDisconnectStreams(t *testing.T) { const headersRWTimeout = 60 * time.Second - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -1021,8 +1008,7 @@ func TestWithDisconnectStreams(t *testing.T) { func TestWithBlocklistStreams(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -1073,8 +1059,7 @@ func TestWithBlocklistStreams(t *testing.T) { func TestUserAgentLogging(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // use concurrent-safe buffers as handlers are logging concurrently s1Logs := new(buffer) diff --git a/pkg/p2p/libp2p/headers_test.go b/pkg/p2p/libp2p/headers_test.go index 5805dafa0d4..d46653bd719 100644 --- a/pkg/p2p/libp2p/headers_test.go +++ b/pkg/p2p/libp2p/headers_test.go @@ -23,8 +23,7 @@ func TestHeaders(t *testing.T) { "other-key": []byte("other-value"), } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -74,8 +73,7 @@ func TestHeaders(t *testing.T) { func TestHeaders_empty(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -134,8 +132,7 @@ func TestHeadler(t *testing.T) { "other-sent-key": []byte("other-sent-value"), } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, diff --git a/pkg/p2p/libp2p/protocols_test.go b/pkg/p2p/libp2p/protocols_test.go index 4563083726a..50a9d9c2970 100644 --- a/pkg/p2p/libp2p/protocols_test.go +++ b/pkg/p2p/libp2p/protocols_test.go @@ -26,8 +26,7 @@ import ( func TestNewStream(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -61,8 +60,7 @@ func TestNewStream(t *testing.T) { func TestNewStream_OnlyFull(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -101,8 +99,7 @@ func TestNewStream_OnlyFull(t *testing.T) { func TestNewStream_Mixed(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -140,8 +137,7 @@ func TestNewStream_Mixed(t *testing.T) { func TestNewStreamMulti(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -190,8 +186,7 @@ func TestNewStreamMulti(t *testing.T) { func TestNewStream_errNotSupported(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -229,8 +224,7 @@ func TestNewStream_errNotSupported(t *testing.T) { func TestNewStream_semanticVersioning(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -292,8 +286,7 @@ func TestNewStream_semanticVersioning(t *testing.T) { func TestDisconnectError(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, @@ -324,8 +317,7 @@ func TestDisconnectError(t *testing.T) { func TestConnectDisconnectEvents(t *testing.T) { t.Parallel() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ FullNode: true, diff --git a/pkg/p2p/libp2p/tracing_test.go b/pkg/p2p/libp2p/tracing_test.go index 7e02f8e2d7c..533a49088ec 100644 --- a/pkg/p2p/libp2p/tracing_test.go +++ b/pkg/p2p/libp2p/tracing_test.go @@ -58,15 +58,13 @@ func TestTracing(t *testing.T) { addr := serviceUnderlayAddress(t, s1) - connectContext, connectCancel := context.WithCancel(context.Background()) - defer connectCancel() + connectContext := t.Context() if _, err := s2.Connect(connectContext, addr); err != nil { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() span, _, ctx := tracer2.StartSpanFromContext(ctx, "test-p2p-client", nil) defer span.Finish()