From 3dfd3d041657d83b4a4024067530d5c4c365f9d0 Mon Sep 17 00:00:00 2001 From: hardlydearly <167623323+hardlydearly@users.noreply.github.com> Date: Thu, 22 May 2025 01:11:02 +0800 Subject: [PATCH] refactor: replace context.WithCancel with t.Context in tests (#15233) Signed-off-by: hardlydearly <799511800@qq.com> --- api/client/event/event_stream_test.go | 3 +-- beacon-chain/p2p/peers/peerdata/store_test.go | 6 ++--- .../p2p/peers/scorers/bad_responses_test.go | 19 +++++----------- .../p2p/peers/scorers/block_providers_test.go | 22 ++++++------------- .../p2p/peers/scorers/gossip_scorer_test.go | 4 +--- .../p2p/peers/scorers/peer_status_test.go | 3 +-- .../p2p/peers/scorers/service_test.go | 3 +-- changelog/hardlydearly_use_t.Context.md | 3 +++ .../middleware/engine-api-proxy/proxy_test.go | 12 ++++------ 9 files changed, 26 insertions(+), 49 deletions(-) create mode 100644 changelog/hardlydearly_use_t.Context.md diff --git a/api/client/event/event_stream_test.go b/api/client/event/event_stream_test.go index 03e5cc4e00..10318eba3b 100644 --- a/api/client/event/event_stream_test.go +++ b/api/client/event/event_stream_test.go @@ -83,8 +83,7 @@ func TestEventStream(t *testing.T) { func TestEventStreamRequestError(t *testing.T) { topics := []string{"head"} eventsChannel := make(chan *Event, 1) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() // use valid url that will result in failed request with nil body stream, err := NewEventStream(ctx, http.DefaultClient, "http://badhost:1234", topics) diff --git a/beacon-chain/p2p/peers/peerdata/store_test.go b/beacon-chain/p2p/peers/peerdata/store_test.go index d3ea75fa48..57a992acff 100644 --- a/beacon-chain/p2p/peers/peerdata/store_test.go +++ b/beacon-chain/p2p/peers/peerdata/store_test.go @@ -11,8 +11,7 @@ import ( ) func TestStore_GetSetDelete(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() store := peerdata.NewStore(ctx, &peerdata.StoreConfig{ MaxPeers: 12, @@ -52,8 +51,7 @@ func TestStore_GetSetDelete(t *testing.T) { } func TestStore_PeerDataGetOrCreate(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() store := peerdata.NewStore(ctx, &peerdata.StoreConfig{ MaxPeers: 12, diff --git a/beacon-chain/p2p/peers/scorers/bad_responses_test.go b/beacon-chain/p2p/peers/scorers/bad_responses_test.go index fc2ac15c44..1cb70d7b81 100644 --- a/beacon-chain/p2p/peers/scorers/bad_responses_test.go +++ b/beacon-chain/p2p/peers/scorers/bad_responses_test.go @@ -1,7 +1,6 @@ package scorers_test import ( - "context" "sort" "testing" @@ -17,8 +16,7 @@ import ( func TestScorers_BadResponses_Score(t *testing.T) { const pid = "peer1" - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ PeerLimit: 30, @@ -50,8 +48,7 @@ func TestScorers_BadResponses_Score(t *testing.T) { } func TestScorers_BadResponses_ParamsThreshold(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() maxBadResponses := 2 peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ @@ -67,8 +64,7 @@ func TestScorers_BadResponses_ParamsThreshold(t *testing.T) { } func TestScorers_BadResponses_Count(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ PeerLimit: 30, @@ -87,8 +83,7 @@ func TestScorers_BadResponses_Count(t *testing.T) { } func TestScorers_BadResponses_Decay(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() maxBadResponses := 2 peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ @@ -143,8 +138,7 @@ func TestScorers_BadResponses_Decay(t *testing.T) { } func TestScorers_BadResponses_IsBadPeer(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ PeerLimit: 30, @@ -168,8 +162,7 @@ func TestScorers_BadResponses_IsBadPeer(t *testing.T) { } func TestScorers_BadResponses_BadPeers(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ PeerLimit: 30, diff --git a/beacon-chain/p2p/peers/scorers/block_providers_test.go b/beacon-chain/p2p/peers/scorers/block_providers_test.go index ee22e2aa68..26b4b735f3 100644 --- a/beacon-chain/p2p/peers/scorers/block_providers_test.go +++ b/beacon-chain/p2p/peers/scorers/block_providers_test.go @@ -1,7 +1,6 @@ package scorers_test import ( - "context" "fmt" "sort" "strconv" @@ -18,8 +17,7 @@ import ( ) func TestScorers_BlockProvider_Score(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() batchSize := uint64(flags.Get().BlockBatchLimit) tests := []struct { @@ -136,8 +134,7 @@ func TestScorers_BlockProvider_Score(t *testing.T) { } func TestScorers_BlockProvider_GettersSetters(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ ScorerParams: &scorers.Config{}, @@ -150,8 +147,7 @@ func TestScorers_BlockProvider_GettersSetters(t *testing.T) { } func TestScorers_BlockProvider_WeightSorted(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ ScorerParams: &scorers.Config{ BlockProviderScorerConfig: &scorers.BlockProviderScorerConfig{ @@ -290,8 +286,7 @@ func TestScorers_BlockProvider_Sorted(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ ScorerParams: &scorers.Config{ BlockProviderScorerConfig: &scorers.BlockProviderScorerConfig{ @@ -307,8 +302,7 @@ func TestScorers_BlockProvider_Sorted(t *testing.T) { } func TestScorers_BlockProvider_MaxScore(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() batchSize := uint64(flags.Get().BlockBatchLimit) tests := []struct { @@ -345,8 +339,7 @@ func TestScorers_BlockProvider_MaxScore(t *testing.T) { } func TestScorers_BlockProvider_FormatScorePretty(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() batchSize := uint64(flags.Get().BlockBatchLimit) format := "[%0.1f%%, raw: %0.2f, blocks: %d/1280]" @@ -473,8 +466,7 @@ func TestScorers_BlockProvider_FormatScorePretty(t *testing.T) { } func TestScorers_BlockProvider_BadPeerMarking(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() peerStatuses := peers.NewStatus(ctx, &peers.StatusConfig{ ScorerParams: &scorers.Config{}, diff --git a/beacon-chain/p2p/peers/scorers/gossip_scorer_test.go b/beacon-chain/p2p/peers/scorers/gossip_scorer_test.go index b461670959..55556fd13e 100644 --- a/beacon-chain/p2p/peers/scorers/gossip_scorer_test.go +++ b/beacon-chain/p2p/peers/scorers/gossip_scorer_test.go @@ -1,7 +1,6 @@ package scorers_test import ( - "context" "testing" "github.com/OffchainLabs/prysm/v6/beacon-chain/p2p/peers" @@ -11,8 +10,7 @@ import ( ) func TestScorers_Gossip_Score(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() tests := []struct { name string diff --git a/beacon-chain/p2p/peers/scorers/peer_status_test.go b/beacon-chain/p2p/peers/scorers/peer_status_test.go index b0cabf90d5..a94630dd45 100644 --- a/beacon-chain/p2p/peers/scorers/peer_status_test.go +++ b/beacon-chain/p2p/peers/scorers/peer_status_test.go @@ -16,8 +16,7 @@ import ( ) func TestScorers_PeerStatus_Score(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() tests := []struct { name string diff --git a/beacon-chain/p2p/peers/scorers/service_test.go b/beacon-chain/p2p/peers/scorers/service_test.go index 840f9cd5c5..43e4d7931f 100644 --- a/beacon-chain/p2p/peers/scorers/service_test.go +++ b/beacon-chain/p2p/peers/scorers/service_test.go @@ -14,8 +14,7 @@ import ( ) func TestScorers_Service_Init(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() batchSize := uint64(flags.Get().BlockBatchLimit) diff --git a/changelog/hardlydearly_use_t.Context.md b/changelog/hardlydearly_use_t.Context.md new file mode 100644 index 0000000000..eb3f07f5cf --- /dev/null +++ b/changelog/hardlydearly_use_t.Context.md @@ -0,0 +1,3 @@ +### Ignored + +- Replace context.WithCancel with t.Context in tests \ No newline at end of file diff --git a/testing/middleware/engine-api-proxy/proxy_test.go b/testing/middleware/engine-api-proxy/proxy_test.go index 95dff28ada..488141c490 100644 --- a/testing/middleware/engine-api-proxy/proxy_test.go +++ b/testing/middleware/engine-api-proxy/proxy_test.go @@ -46,8 +46,7 @@ func TestProxy(t *testing.T) { require.LogsContain(t, hook, "Could not forward request to destination server") }) t.Run("properly proxies request/response", func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() wantDestinationResponse := &pb.ForkchoiceState{ HeadBlockHash: []byte("foo"), @@ -86,8 +85,7 @@ func TestProxy(t *testing.T) { func TestProxy_CustomInterceptors(t *testing.T) { t.Run("only intercepts engine API methods", func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() type syncingResponse struct { Syncing bool `json:"syncing"` @@ -138,8 +136,7 @@ func TestProxy_CustomInterceptors(t *testing.T) { require.DeepEqual(t, wantDestinationResponse, proxyResult) }) t.Run("only intercepts if trigger function returns true", func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() type engineResponse struct { BlockHash common.Hash `json:"blockHash"` @@ -206,8 +203,7 @@ func TestProxy_CustomInterceptors(t *testing.T) { require.DeepEqual(t, wantInterceptedResponse(), proxyResult) }) t.Run("triggers interceptor response correctly", func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() type engineResponse struct { BlockHash common.Hash `json:"blockHash"`