refactor: replace context.WithCancel with t.Context

Signed-off-by: cargoedit <cargoedit@outlook.com>
This commit is contained in:
cargoedit
2025-09-17 14:11:16 +08:00
committed by Marco Munizaga
parent b3f1e66e26
commit c8e06bb688
14 changed files with 35 additions and 72 deletions

View File

@@ -550,8 +550,7 @@ func TestWebRTCReuseAddrWithQUIC(t *testing.T) {
} }
t.Run("quic client can connect", func(t *testing.T) { t.Run("quic client can connect", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
p := ping.NewPingService(quicClient) p := ping.NewPingService(quicClient)
resCh := p.Ping(ctx, h1.ID()) resCh := p.Ping(ctx, h1.ID())
res := <-resCh res := <-resCh
@@ -559,8 +558,7 @@ func TestWebRTCReuseAddrWithQUIC(t *testing.T) {
}) })
t.Run("webrtc client can connect", func(t *testing.T) { t.Run("webrtc client can connect", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
p := ping.NewPingService(webrtcClient) p := ping.NewPingService(webrtcClient)
resCh := p.Ping(ctx, h1.ID()) resCh := p.Ping(ctx, h1.ID())
res := <-resCh res := <-resCh

View File

@@ -89,8 +89,7 @@ func withClock(c clock) BackoffDiscoveryOption {
} }
func TestBackoffDiscoverySingleBackoff(t *testing.T) { func TestBackoffDiscoverySingleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
clock := mockClock.NewMock() clock := mockClock.NewMock()
discServer := mocks.NewDiscoveryServer(clock) discServer := mocks.NewDiscoveryServer(clock)
@@ -137,8 +136,7 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
func TestBackoffDiscoveryMultipleBackoff(t *testing.T) { func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
clock := mockClock.NewMock() clock := mockClock.NewMock()
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
discServer := mocks.NewDiscoveryServer(clock) discServer := mocks.NewDiscoveryServer(clock)
@@ -195,8 +193,7 @@ func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
} }
func TestBackoffDiscoverySimultaneousQuery(t *testing.T) { func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
clock := mockClock.NewMock() clock := mockClock.NewMock()
discServer := mocks.NewDiscoveryServer(clock) discServer := mocks.NewDiscoveryServer(clock)
@@ -257,8 +254,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
} }
func TestBackoffDiscoveryCacheCapacity(t *testing.T) { func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
clock := mockClock.NewMock() clock := mockClock.NewMock()
discServer := mocks.NewDiscoveryServer(clock) discServer := mocks.NewDiscoveryServer(clock)

View File

@@ -76,8 +76,7 @@ func (m *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, _ int
} }
func TestRoutingDiscovery(t *testing.T) { func TestRoutingDiscovery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -110,8 +109,7 @@ func TestRoutingDiscovery(t *testing.T) {
} }
func TestDiscoveryRouting(t *testing.T) { func TestDiscoveryRouting(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))

View File

@@ -43,8 +43,7 @@ func makeAutoNATClient(t *testing.T) (host.Host, Client) {
// Note: these tests assume that the host has only private network addresses! // Note: these tests assume that the host has only private network addresses!
func TestAutoNATServiceDialRefused(t *testing.T) { func TestAutoNATServiceDialRefused(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
c := makeAutoNATConfig(t) c := makeAutoNATConfig(t)
defer c.host.Close() defer c.host.Close()
@@ -68,8 +67,7 @@ func TestAutoNATServiceDialRefused(t *testing.T) {
} }
func TestAutoNATServiceDialSuccess(t *testing.T) { func TestAutoNATServiceDialSuccess(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
c := makeAutoNATConfig(t) c := makeAutoNATConfig(t)
defer c.host.Close() defer c.host.Close()
@@ -88,8 +86,7 @@ func TestAutoNATServiceDialSuccess(t *testing.T) {
} }
func TestAutoNATServiceDialRateLimiter(t *testing.T) { func TestAutoNATServiceDialRateLimiter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
c := makeAutoNATConfig(t) c := makeAutoNATConfig(t)
defer c.host.Close() defer c.host.Close()
@@ -128,8 +125,7 @@ func TestAutoNATServiceDialRateLimiter(t *testing.T) {
} }
func TestAutoNATServiceGlobalLimiter(t *testing.T) { func TestAutoNATServiceGlobalLimiter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
c := makeAutoNATConfig(t) c := makeAutoNATConfig(t)
defer c.host.Close() defer c.host.Close()

View File

@@ -375,8 +375,7 @@ func TestHostProtoPreference(t *testing.T) {
} }
func TestHostProtoMismatch(t *testing.T) { func TestHostProtoMismatch(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, h2 := getHostPair(t) h1, h2 := getHostPair(t)
defer h1.Close() defer h1.Close()
@@ -469,8 +468,7 @@ func TestHostProtoPreknowledge(t *testing.T) {
} }
func TestNewDialOld(t *testing.T) { func TestNewDialOld(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, h2 := getHostPair(t) h1, h2 := getHostPair(t)
defer h1.Close() defer h1.Close()
@@ -541,8 +539,7 @@ func TestNewStreamResolve(t *testing.T) {
} }
func TestProtoDowngrade(t *testing.T) { func TestProtoDowngrade(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, h2 := getHostPair(t) h1, h2 := getHostPair(t)
defer h1.Close() defer h1.Close()
@@ -756,8 +753,7 @@ func TestHostAddrChangeDetection(t *testing.T) {
} }
func TestNegotiationCancel(t *testing.T) { func TestNegotiationCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, h2 := getHostPair(t) h1, h2 := getHostPair(t)
defer h1.Close() defer h1.Close()

View File

@@ -2,7 +2,6 @@ package gostream
import ( import (
"bufio" "bufio"
"context"
"io" "io"
"testing" "testing"
"time" "time"
@@ -38,8 +37,7 @@ func TestServerClient(t *testing.T) {
clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL) clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL)
var tag protocol.ID = "/testitytest" var tag protocol.ID = "/testitytest"
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {

View File

@@ -24,8 +24,7 @@ func setupPSKConns(_ context.Context, t *testing.T) (net.Conn, net.Conn) {
} }
func TestPSKSimpelMessges(t *testing.T) { func TestPSKSimpelMessges(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO()) ctx := t.Context()
defer cancel()
psk1, psk2 := setupPSKConns(ctx, t) psk1, psk2 := setupPSKConns(ctx, t)
msg1 := []byte("hello world") msg1 := []byte("hello world")
@@ -56,8 +55,7 @@ func TestPSKSimpelMessges(t *testing.T) {
} }
func TestPSKFragmentation(t *testing.T) { func TestPSKFragmentation(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO()) ctx := t.Context()
defer cancel()
psk1, psk2 := setupPSKConns(ctx, t) psk1, psk2 := setupPSKConns(ctx, t)

View File

@@ -81,8 +81,7 @@ func TestLimiterBasicDials(t *testing.T) {
resch := make(chan transport.DialUpdate) resch := make(chan transport.DialUpdate)
pid := peer.ID("testpeer") pid := peer.ID("testpeer")
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
tryDialAddrs(ctx, l, pid, bads, resch) tryDialAddrs(ctx, l, pid, bads, resch)

View File

@@ -88,8 +88,7 @@ func printConns(n network.Network) string {
} }
func TestNetworkOpenStream(t *testing.T) { func TestNetworkOpenStream(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
testString := "hello ipfs" testString := "hello ipfs"

View File

@@ -97,8 +97,7 @@ func addTransport(t *testing.T, h host.Host, upgrader transport.Upgrader) {
} }
func TestBasicRelay(t *testing.T) { func TestBasicRelay(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
hosts, upgraders := getNetHosts(t, ctx, 3) hosts, upgraders := getNetHosts(t, ctx, 3)
addTransport(t, hosts[0], upgraders[0]) addTransport(t, hosts[0], upgraders[0])
@@ -205,8 +204,7 @@ func TestBasicRelay(t *testing.T) {
} }
func TestRelayLimitTime(t *testing.T) { func TestRelayLimitTime(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
hosts, upgraders := getNetHosts(t, ctx, 3) hosts, upgraders := getNetHosts(t, ctx, 3)
addTransport(t, hosts[0], upgraders[0]) addTransport(t, hosts[0], upgraders[0])
@@ -279,8 +277,7 @@ func TestRelayLimitTime(t *testing.T) {
} }
func TestRelayLimitData(t *testing.T) { func TestRelayLimitData(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
hosts, upgraders := getNetHosts(t, ctx, 3) hosts, upgraders := getNetHosts(t, ctx, 3)
addTransport(t, hosts[0], upgraders[0]) addTransport(t, hosts[0], upgraders[0])

View File

@@ -363,8 +363,7 @@ func TestLocalhostAddrFiltering(t *testing.T) {
// TestIdentifyPushWhileIdentifyingConn tests that the host waits to push updates if an identify is ongoing. // TestIdentifyPushWhileIdentifyingConn tests that the host waits to push updates if an identify is ongoing.
func TestIdentifyPushWhileIdentifyingConn(t *testing.T) { func TestIdentifyPushWhileIdentifyingConn(t *testing.T) {
t.Skip() t.Skip()
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := blhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -436,8 +435,7 @@ func TestIdentifyPushWhileIdentifyingConn(t *testing.T) {
} }
func TestIdentifyPushOnAddrChange(t *testing.T) { func TestIdentifyPushOnAddrChange(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := blhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -510,8 +508,7 @@ func TestIdentifyPushOnAddrChange(t *testing.T) {
} }
func TestUserAgent(t *testing.T) { func TestUserAgent(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, err := libp2p.New(libp2p.UserAgent("foo"), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) h1, err := libp2p.New(libp2p.UserAgent("foo"), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil { if err != nil {
@@ -542,8 +539,7 @@ func TestNotListening(t *testing.T) {
// Make sure we don't panic if we're not listening on any addresses. // Make sure we don't panic if we're not listening on any addresses.
// //
// https://github.com/libp2p/go-libp2p/issues/939 // https://github.com/libp2p/go-libp2p/issues/939
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, err := libp2p.New(libp2p.NoListenAddrs) h1, err := libp2p.New(libp2p.NoListenAddrs)
if err != nil { if err != nil {
@@ -564,8 +560,7 @@ func TestNotListening(t *testing.T) {
} }
func TestSendPush(t *testing.T) { func TestSendPush(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := blhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -729,8 +724,7 @@ func randString(n int) string {
} }
func TestLargePushMessage(t *testing.T) { func TestLargePushMessage(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := blhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -805,8 +799,7 @@ func TestLargePushMessage(t *testing.T) {
} }
func TestIdentifyResponseReadTimeout(t *testing.T) { func TestIdentifyResponseReadTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := blhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := blhost.NewBlankHost(swarmt.GenSwarm(t))
@@ -845,8 +838,7 @@ func TestIdentifyResponseReadTimeout(t *testing.T) {
} }
func TestIncomingIDStreamsTimeout(t *testing.T) { func TestIncomingIDStreamsTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
protocols := []protocol.ID{identify.IDPush} protocols := []protocol.ID{identify.IDPush}

View File

@@ -15,8 +15,7 @@ import (
) )
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil) h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err) require.NoError(t, err)
defer h1.Close() defer h1.Close()

View File

@@ -1,7 +1,6 @@
package backpressure_tests package backpressure_tests
import ( import (
"context"
"os" "os"
"testing" "testing"
"time" "time"
@@ -20,8 +19,7 @@ var log = logging.Logger("backpressure")
// TestStBackpressureStreamWrite tests whether streams see proper // TestStBackpressureStreamWrite tests whether streams see proper
// backpressure when writing data over the network streams. // backpressure when writing data over the network streams.
func TestStBackpressureStreamWrite(t *testing.T) { func TestStBackpressureStreamWrite(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil) h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err) require.NoError(t, err)

View File

@@ -860,8 +860,7 @@ func TestDiscoverPeerIDFromSecurityNegotiation(t *testing.T) {
// runs a test to verify we can extract the peer ID from a target with just its address // runs a test to verify we can extract the peer ID from a target with just its address
t.Helper() t.Helper()
ctx, cancel := context.WithCancel(context.Background()) ctx := t.Context()
defer cancel()
// Use a bogus peer ID so that when we connect to the target we get an error telling // Use a bogus peer ID so that when we connect to the target we get an error telling
// us the targets real peer ID // us the targets real peer ID