refactor: replace context.WithCancel with t.Context in tests (#15233)

Signed-off-by: hardlydearly <799511800@qq.com>
This commit is contained in:
hardlydearly
2025-05-22 01:11:02 +08:00
committed by GitHub
parent b20821dd8e
commit 3dfd3d0416
9 changed files with 26 additions and 49 deletions

View File

@@ -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)

View File

@@ -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,

View File

@@ -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,

View File

@@ -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{},

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -0,0 +1,3 @@
### Ignored
- Replace context.WithCancel with t.Context in tests

View File

@@ -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"`