Skip to content

Commit c8e06bb

Browse files
cargoeditMarcoPolo
authored andcommitted
refactor: replace context.WithCancel with t.Context
Signed-off-by: cargoedit <cargoedit@outlook.com>
1 parent b3f1e66 commit c8e06bb

File tree

14 files changed

+35
-72
lines changed

14 files changed

+35
-72
lines changed

libp2p_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,17 +550,15 @@ func TestWebRTCReuseAddrWithQUIC(t *testing.T) {
550550
}
551551

552552
t.Run("quic client can connect", func(t *testing.T) {
553-
ctx, cancel := context.WithCancel(context.Background())
554-
defer cancel()
553+
ctx := t.Context()
555554
p := ping.NewPingService(quicClient)
556555
resCh := p.Ping(ctx, h1.ID())
557556
res := <-resCh
558557
require.NoError(t, res.Error)
559558
})
560559

561560
t.Run("webrtc client can connect", func(t *testing.T) {
562-
ctx, cancel := context.WithCancel(context.Background())
563-
defer cancel()
561+
ctx := t.Context()
564562
p := ping.NewPingService(webrtcClient)
565563
resCh := p.Ping(ctx, h1.ID())
566564
res := <-resCh

p2p/discovery/backoff/backoffcache_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ func withClock(c clock) BackoffDiscoveryOption {
8989
}
9090

9191
func TestBackoffDiscoverySingleBackoff(t *testing.T) {
92-
ctx, cancel := context.WithCancel(context.Background())
93-
defer cancel()
92+
ctx := t.Context()
9493

9594
clock := mockClock.NewMock()
9695
discServer := mocks.NewDiscoveryServer(clock)
@@ -137,8 +136,7 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
137136

138137
func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
139138
clock := mockClock.NewMock()
140-
ctx, cancel := context.WithCancel(context.Background())
141-
defer cancel()
139+
ctx := t.Context()
142140

143141
discServer := mocks.NewDiscoveryServer(clock)
144142

@@ -195,8 +193,7 @@ func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
195193
}
196194

197195
func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
198-
ctx, cancel := context.WithCancel(context.Background())
199-
defer cancel()
196+
ctx := t.Context()
200197

201198
clock := mockClock.NewMock()
202199
discServer := mocks.NewDiscoveryServer(clock)
@@ -257,8 +254,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
257254
}
258255

259256
func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
260-
ctx, cancel := context.WithCancel(context.Background())
261-
defer cancel()
257+
ctx := t.Context()
262258

263259
clock := mockClock.NewMock()
264260
discServer := mocks.NewDiscoveryServer(clock)

p2p/discovery/routing/routing_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ func (m *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, _ int
7676
}
7777

7878
func TestRoutingDiscovery(t *testing.T) {
79-
ctx, cancel := context.WithCancel(context.Background())
80-
defer cancel()
79+
ctx := t.Context()
8180

8281
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
8382
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -110,8 +109,7 @@ func TestRoutingDiscovery(t *testing.T) {
110109
}
111110

112111
func TestDiscoveryRouting(t *testing.T) {
113-
ctx, cancel := context.WithCancel(context.Background())
114-
defer cancel()
112+
ctx := t.Context()
115113

116114
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
117115
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))

p2p/host/autonat/svc_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func makeAutoNATClient(t *testing.T) (host.Host, Client) {
4343

4444
// Note: these tests assume that the host has only private network addresses!
4545
func TestAutoNATServiceDialRefused(t *testing.T) {
46-
ctx, cancel := context.WithCancel(context.Background())
47-
defer cancel()
46+
ctx := t.Context()
4847

4948
c := makeAutoNATConfig(t)
5049
defer c.host.Close()
@@ -68,8 +67,7 @@ func TestAutoNATServiceDialRefused(t *testing.T) {
6867
}
6968

7069
func TestAutoNATServiceDialSuccess(t *testing.T) {
71-
ctx, cancel := context.WithCancel(context.Background())
72-
defer cancel()
70+
ctx := t.Context()
7371

7472
c := makeAutoNATConfig(t)
7573
defer c.host.Close()
@@ -88,8 +86,7 @@ func TestAutoNATServiceDialSuccess(t *testing.T) {
8886
}
8987

9088
func TestAutoNATServiceDialRateLimiter(t *testing.T) {
91-
ctx, cancel := context.WithCancel(context.Background())
92-
defer cancel()
89+
ctx := t.Context()
9390

9491
c := makeAutoNATConfig(t)
9592
defer c.host.Close()
@@ -128,8 +125,7 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) {
128125
}
129126

130127
func TestAutoNATServiceGlobalLimiter(t *testing.T) {
131-
ctx, cancel := context.WithCancel(context.Background())
132-
defer cancel()
128+
ctx := t.Context()
133129

134130
c := makeAutoNATConfig(t)
135131
defer c.host.Close()

p2p/host/basic/basic_host_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ func TestHostProtoPreference(t *testing.T) {
375375
}
376376

377377
func TestHostProtoMismatch(t *testing.T) {
378-
ctx, cancel := context.WithCancel(context.Background())
379-
defer cancel()
378+
ctx := t.Context()
380379

381380
h1, h2 := getHostPair(t)
382381
defer h1.Close()
@@ -469,8 +468,7 @@ func TestHostProtoPreknowledge(t *testing.T) {
469468
}
470469

471470
func TestNewDialOld(t *testing.T) {
472-
ctx, cancel := context.WithCancel(context.Background())
473-
defer cancel()
471+
ctx := t.Context()
474472

475473
h1, h2 := getHostPair(t)
476474
defer h1.Close()
@@ -541,8 +539,7 @@ func TestNewStreamResolve(t *testing.T) {
541539
}
542540

543541
func TestProtoDowngrade(t *testing.T) {
544-
ctx, cancel := context.WithCancel(context.Background())
545-
defer cancel()
542+
ctx := t.Context()
546543

547544
h1, h2 := getHostPair(t)
548545
defer h1.Close()
@@ -756,8 +753,7 @@ func TestHostAddrChangeDetection(t *testing.T) {
756753
}
757754

758755
func TestNegotiationCancel(t *testing.T) {
759-
ctx, cancel := context.WithCancel(context.Background())
760-
defer cancel()
756+
ctx := t.Context()
761757

762758
h1, h2 := getHostPair(t)
763759
defer h1.Close()

p2p/net/gostream/gostream_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gostream
22

33
import (
44
"bufio"
5-
"context"
65
"io"
76
"testing"
87
"time"
@@ -38,8 +37,7 @@ func TestServerClient(t *testing.T) {
3837
clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL)
3938

4039
var tag protocol.ID = "/testitytest"
41-
ctx, cancel := context.WithCancel(context.Background())
42-
defer cancel()
40+
ctx := t.Context()
4341

4442
done := make(chan struct{})
4543
go func() {

p2p/net/pnet/psk_conn_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func setupPSKConns(_ context.Context, t *testing.T) (net.Conn, net.Conn) {
2424
}
2525

2626
func TestPSKSimpelMessges(t *testing.T) {
27-
ctx, cancel := context.WithCancel(context.TODO())
28-
defer cancel()
27+
ctx := t.Context()
2928

3029
psk1, psk2 := setupPSKConns(ctx, t)
3130
msg1 := []byte("hello world")
@@ -56,8 +55,7 @@ func TestPSKSimpelMessges(t *testing.T) {
5655
}
5756

5857
func TestPSKFragmentation(t *testing.T) {
59-
ctx, cancel := context.WithCancel(context.TODO())
60-
defer cancel()
58+
ctx := t.Context()
6159

6260
psk1, psk2 := setupPSKConns(ctx, t)
6361

p2p/net/swarm/limiter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ func TestLimiterBasicDials(t *testing.T) {
8181

8282
resch := make(chan transport.DialUpdate)
8383
pid := peer.ID("testpeer")
84-
ctx, cancel := context.WithCancel(context.Background())
85-
defer cancel()
84+
ctx := t.Context()
8685

8786
tryDialAddrs(ctx, l, pid, bads, resch)
8887

p2p/net/swarm/swarm_net_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ func printConns(n network.Network) string {
8888
}
8989

9090
func TestNetworkOpenStream(t *testing.T) {
91-
ctx, cancel := context.WithCancel(context.Background())
92-
defer cancel()
91+
ctx := t.Context()
9392

9493
testString := "hello ipfs"
9594

p2p/protocol/circuitv2/relay/relay_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ func addTransport(t *testing.T, h host.Host, upgrader transport.Upgrader) {
9797
}
9898

9999
func TestBasicRelay(t *testing.T) {
100-
ctx, cancel := context.WithCancel(context.Background())
101-
defer cancel()
100+
ctx := t.Context()
102101

103102
hosts, upgraders := getNetHosts(t, ctx, 3)
104103
addTransport(t, hosts[0], upgraders[0])
@@ -205,8 +204,7 @@ func TestBasicRelay(t *testing.T) {
205204
}
206205

207206
func TestRelayLimitTime(t *testing.T) {
208-
ctx, cancel := context.WithCancel(context.Background())
209-
defer cancel()
207+
ctx := t.Context()
210208

211209
hosts, upgraders := getNetHosts(t, ctx, 3)
212210
addTransport(t, hosts[0], upgraders[0])
@@ -279,8 +277,7 @@ func TestRelayLimitTime(t *testing.T) {
279277
}
280278

281279
func TestRelayLimitData(t *testing.T) {
282-
ctx, cancel := context.WithCancel(context.Background())
283-
defer cancel()
280+
ctx := t.Context()
284281

285282
hosts, upgraders := getNetHosts(t, ctx, 3)
286283
addTransport(t, hosts[0], upgraders[0])

0 commit comments

Comments
 (0)