Replace context.Background with testing.TB.Context where possible (#15416)

* Replace context.Background with testing.TB.Context where possible

* Fix failing tests
This commit is contained in:
Preston Van Loon
2025-06-16 17:09:18 -05:00
committed by GitHub
parent 6a13ba9125
commit 62fec4d1f3
409 changed files with 3175 additions and 3456 deletions

View File

@@ -48,7 +48,7 @@ func TestServer_AuthenticateUsingExistingToken(t *testing.T) {
ctxMD := map[string][]string{
"authorization": {"Bearer " + srv.authToken},
}
ctx := context.Background()
ctx := t.Context()
ctx = metadata.NewIncomingContext(ctx, ctxMD)
_, err = srv.AuthTokenInterceptor()(ctx, "xyz", unaryInfo, unaryHandler)
require.NoError(t, err)
@@ -77,7 +77,7 @@ func TestServer_RefreshAuthTokenOnFileChange(t *testing.T) {
require.NoError(t, err)
currentToken := srv.authToken
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
go srv.refreshAuthTokenFromFileChanges(ctx, srv.authTokenPath)

View File

@@ -1,7 +1,6 @@
package rpc
import (
"context"
"testing"
"github.com/OffchainLabs/prysm/v6/testing/assert"
@@ -11,7 +10,7 @@ import (
func TestGrpcHeaders(t *testing.T) {
s := &Server{
ctx: context.Background(),
ctx: t.Context(),
grpcHeaders: []string{"first=value1", "second=value2"},
}
err := s.registerBeaconClient()

View File

@@ -2,7 +2,6 @@ package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@@ -31,7 +30,7 @@ import (
const strongPass = "29384283xasjasd32%%&*@*#*"
func TestServer_CreateWallet_Local(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
opts := []accounts.Option{
@@ -424,7 +423,7 @@ func TestServer_WalletConfig_NoWalletFound(t *testing.T) {
func TestServer_WalletConfig(t *testing.T) {
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
ctx := context.Background()
ctx := t.Context()
s := &Server{
walletInitializedFeed: new(event.Feed),
walletDir: defaultWalletPath,

View File

@@ -3,7 +3,6 @@ package rpc
import (
"archive/zip"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
@@ -37,7 +36,7 @@ var (
)
func TestServer_ListAccounts(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
// We attempt to create the wallet.
@@ -142,7 +141,7 @@ func TestServer_ListAccounts(t *testing.T) {
}
func TestServer_BackupAccounts(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
// We attempt to create the wallet.
@@ -235,7 +234,7 @@ func TestServer_BackupAccounts(t *testing.T) {
func TestServer_VoluntaryExit(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
ctx := t.Context()
mockValidatorClient := validatormock.NewMockValidatorClient(ctrl)
mockNodeClient := validatormock.NewMockNodeClient(ctrl)

View File

@@ -1,7 +1,6 @@
package rpc
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -33,7 +32,7 @@ func TestInitialize(t *testing.T) {
}
acc, err := accounts.NewCLIManager(opts...)
require.NoError(t, err)
_, err = acc.WalletCreate(context.Background())
_, err = acc.WalletCreate(t.Context())
require.NoError(t, err)
server := &Server{walletDir: localWalletDir, authTokenPath: authTokenPath}

View File

@@ -72,7 +72,7 @@ func TestStreamBeaconLogs(t *testing.T) {
// Setting up the mock in the server struct
s := Server{
ctx: context.Background(),
ctx: t.Context(),
healthClient: mockClient,
}
@@ -107,7 +107,7 @@ func TestStreamBeaconLogs(t *testing.T) {
}
func TestStreamValidatorLogs(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
mockLogs := [][]byte{
[]byte("[2023-10-31 10:00:00] INFO: Starting server..."),
[]byte("[2023-10-31 10:01:23] DEBUG: Database connection established."),
@@ -166,7 +166,7 @@ func TestStreamValidatorLogs(t *testing.T) {
func TestServer_GetVersion(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
ctx := t.Context()
mockNodeClient := validatormock.NewMockNodeClient(ctrl)
s := Server{
ctx: ctx,

View File

@@ -2,7 +2,6 @@ package rpc
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
@@ -49,7 +48,7 @@ import (
)
func TestServer_ListKeystores(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
t.Run("wallet not ready", func(t *testing.T) {
m := &testutil.FakeValidator{}
vs, err := client.NewValidatorService(ctx, &client.Config{
@@ -132,7 +131,7 @@ func TestServer_ListKeystores(t *testing.T) {
}
func TestServer_ImportKeystores(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
opts := []accounts.Option{
@@ -353,7 +352,7 @@ func TestServer_ImportKeystores(t *testing.T) {
}
func TestServer_ImportKeystores_WrongKeymanagerKind(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
newDir := filepath.Join(t.TempDir(), "new")
@@ -404,7 +403,7 @@ func TestServer_ImportKeystores_WrongKeymanagerKind(t *testing.T) {
func TestServer_DeleteKeystores(t *testing.T) {
for _, isSlashingProtectionMinimal := range []bool{false, true} {
ctx := context.Background()
ctx := t.Context()
srv := setupServerWithWallet(t)
// We recover 3 accounts from a test mnemonic.
@@ -577,7 +576,7 @@ func TestServer_DeleteKeystores(t *testing.T) {
func TestServer_DeleteKeystores_FailedSlashingProtectionExport(t *testing.T) {
for _, isSlashingProtectionMinimal := range []bool{false, true} {
t.Run(fmt.Sprintf("minimalSlashingProtection:%v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
srv := setupServerWithWallet(t)
// We recover 3 accounts from a test mnemonic.
@@ -636,7 +635,7 @@ func TestServer_DeleteKeystores_FailedSlashingProtectionExport(t *testing.T) {
}
func TestServer_DeleteKeystores_WrongKeymanagerKind(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
newDir := filepath.Join(t.TempDir(), "new")
@@ -680,7 +679,7 @@ func TestServer_DeleteKeystores_WrongKeymanagerKind(t *testing.T) {
}
func setupServerWithWallet(t testing.TB) *Server {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
opts := []accounts.Option{
@@ -714,7 +713,7 @@ func TestServer_SetVoluntaryExit(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
ctx := t.Context()
defaultWalletPath = setupWalletDir(t)
opts := []accounts.Option{
accounts.WithWalletDir(defaultWalletPath),
@@ -898,7 +897,7 @@ func TestServer_SetVoluntaryExit(t *testing.T) {
}
func TestServer_GetGasLimit(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
byteval, err := hexutil.Decode("0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493")
byteval2, err2 := hexutil.Decode("0x1234567878903438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493")
require.NoError(t, err)
@@ -977,7 +976,7 @@ func TestServer_SetGasLimit(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
beaconClient := validatormock.NewMockValidatorClient(ctrl)
ctx := context.Background()
ctx := t.Context()
pubkey1, err := hexutil.Decode("0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493")
pubkey2, err2 := hexutil.Decode("0xbedefeaa94e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2cdddddddddddddddddddddddd")
@@ -1185,7 +1184,7 @@ func TestServer_SetGasLimit_InvalidPubKey(t *testing.T) {
}
func TestServer_DeleteGasLimit(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkey1, err := hexutil.Decode("0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493")
pubkey2, err2 := hexutil.Decode("0xbedefeaa94e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2cdddddddddddddddddddddddd")
require.NoError(t, err)
@@ -1333,7 +1332,7 @@ func TestServer_DeleteGasLimit(t *testing.T) {
}
func TestServer_ListRemoteKeys(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
newDir := filepath.Join(t.TempDir(), "new")
@@ -1389,7 +1388,7 @@ func TestServer_ListRemoteKeys(t *testing.T) {
}
func TestServer_ImportRemoteKeys(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
newDir := filepath.Join(t.TempDir(), "new")
@@ -1450,7 +1449,7 @@ func TestServer_ImportRemoteKeys(t *testing.T) {
}
func TestServer_DeleteRemoteKeys(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
app := cli.App{}
set := flag.NewFlagSet("test", 0)
newDir := filepath.Join(t.TempDir(), "new")
@@ -1511,7 +1510,7 @@ func TestServer_DeleteRemoteKeys(t *testing.T) {
}
func TestServer_ListFeeRecipientByPubkey(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkey := "0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493"
byteval, err := hexutil.Decode(pubkey)
require.NoError(t, err)
@@ -1589,7 +1588,7 @@ func TestServer_ListFeeRecipientByPubkey(t *testing.T) {
}
func TestServer_ListFeeRecipientByPubKey_NoFeeRecipientSet(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
vs, err := client.NewValidatorService(ctx, &client.Config{
Validator: &testutil.FakeValidator{},
@@ -1638,7 +1637,7 @@ func TestServer_FeeRecipientByPubkey(t *testing.T) {
defer ctrl.Finish()
beaconClient := validatormock.NewMockValidatorClient(ctrl)
ctx := context.Background()
ctx := t.Context()
pubkey := "0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493"
byteval, err := hexutil.Decode(pubkey)
require.NoError(t, err)
@@ -1848,7 +1847,7 @@ func TestServer_SetFeeRecipientByPubkey_InvalidFeeRecipient(t *testing.T) {
}
func TestServer_DeleteFeeRecipientByPubkey(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkey := "0xaf2e7ba294e03438ea819bd4033c6c1bf6b04320ee2075b77273c08d02f8a61bcc303c2c06bd3713cb442072ae591493"
byteval, err := hexutil.Decode(pubkey)
require.NoError(t, err)
@@ -1940,7 +1939,7 @@ func TestServer_DeleteFeeRecipientByPubkey_InvalidPubKey(t *testing.T) {
func TestServer_Graffiti(t *testing.T) {
graffiti := "graffiti"
m := &testutil.FakeValidator{}
vs, err := client.NewValidatorService(context.Background(), &client.Config{
vs, err := client.NewValidatorService(t.Context(), &client.Config{
Validator: m,
})
require.NoError(t, err)

View File

@@ -2,7 +2,6 @@ package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@@ -23,7 +22,7 @@ import (
func TestImportSlashingProtection_Preconditions(t *testing.T) {
for _, isSlashingProtectionMinimal := range []bool{false, true} {
t.Run(fmt.Sprintf("slashing protection minimal: %v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
@@ -119,7 +118,7 @@ func TestImportSlashingProtection_Preconditions(t *testing.T) {
func TestExportSlashingProtection_Preconditions(t *testing.T) {
for _, isSlashingProtectionMinimal := range []bool{false, true} {
t.Run(fmt.Sprintf("slashing protection minimal: %v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
@@ -146,7 +145,7 @@ func TestExportSlashingProtection_Preconditions(t *testing.T) {
PubKeys: pubKeys,
})
} else {
validatorDB, err = kv.NewKVStore(context.Background(), t.TempDir(), &kv.Config{
validatorDB, err = kv.NewKVStore(t.Context(), t.TempDir(), &kv.Config{
PubKeys: pubKeys,
})
}
@@ -171,7 +170,7 @@ func TestExportSlashingProtection_Preconditions(t *testing.T) {
func TestImportExportSlashingProtection_RoundTrip(t *testing.T) {
// Round trip is only suitable with complete slashing protection, since
// minimal slashing protections only keep latest attestation and proposal.
ctx := context.Background()
ctx := t.Context()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir

View File

@@ -30,7 +30,7 @@ func TestServer_AuthTokenInterceptor_Verify(t *testing.T) {
ctxMD := map[string][]string{
"authorization": {"Bearer " + token},
}
ctx := context.Background()
ctx := t.Context()
ctx = metadata.NewIncomingContext(ctx, ctxMD)
_, err := interceptor(ctx, "xyz", unaryInfo, unaryHandler)
require.NoError(t, err)
@@ -52,7 +52,7 @@ func TestServer_AuthTokenInterceptor_BadToken(t *testing.T) {
ctxMD := map[string][]string{
"authorization": {"Bearer bad-token"},
}
ctx := context.Background()
ctx := t.Context()
ctx = metadata.NewIncomingContext(ctx, ctxMD)
_, err := interceptor(ctx, "xyz", unaryInfo, unaryHandler)
require.ErrorContains(t, "token value is invalid", err)