multi: context.Background() -> t.Context()

Use the new feature of Go 1.24, fix linter warnings.

This change was produced by:
 - running golangci-lint run --fix
 - sed 's/context.Background/t.Context/' -i `git grep -l context.Background | grep test.go`
 - manually fixing broken tests
 - itest, lntest: use ht.Context() where ht or hn is available
 - in HarnessNode.Stop() we keep using context.Background(), because it is
   called from a cleanup handler in which t.Context() is canceled already.
This commit is contained in:
Boris Nagaev
2025-08-19 14:54:41 -03:00
parent 6ffe257004
commit dee8ad3754
71 changed files with 361 additions and 393 deletions

View File

@@ -40,7 +40,7 @@ func TestCommitQueue(t *testing.T) {
}
}
ctx := context.Background()
ctx := t.Context()
ctx, cancel := context.WithCancel(ctx)
q := NewCommitQueue(ctx)
defer q.Stop()

View File

@@ -19,7 +19,7 @@ func TestDump(t *testing.T) {
f := NewEtcdTestFixture(t)
db, err := newEtcdBackend(context.Background(), f.BackendConfig())
db, err := newEtcdBackend(t.Context(), f.BackendConfig())
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
@@ -53,7 +53,7 @@ func TestAbortContext(t *testing.T) {
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
config := f.BackendConfig()
@@ -83,7 +83,7 @@ func TestNewEtcdClient(t *testing.T) {
f := NewEtcdTestFixture(t)
client, ctx, cancel, err := NewEtcdClient(
context.Background(), f.BackendConfig(),
t.Context(), f.BackendConfig(),
)
require.NoError(t, err)
t.Cleanup(cancel)

View File

@@ -4,7 +4,6 @@
package etcd
import (
"context"
"testing"
"github.com/btcsuite/btcwallet/walletdb"
@@ -16,7 +15,7 @@ func TestChangeDuringManualTx(t *testing.T) {
f := NewEtcdTestFixture(t)
db, err := newEtcdBackend(context.Background(), f.BackendConfig())
db, err := newEtcdBackend(t.Context(), f.BackendConfig())
require.NoError(t, err)
tx, err := db.BeginReadWriteTx()
@@ -44,7 +43,7 @@ func TestChangeDuringUpdate(t *testing.T) {
f := NewEtcdTestFixture(t)
db, err := newEtcdBackend(context.Background(), f.BackendConfig())
db, err := newEtcdBackend(t.Context(), f.BackendConfig())
require.NoError(t, err)
count := 0

View File

@@ -23,7 +23,7 @@ func TestPutToEmpty(t *testing.T) {
t.Parallel()
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
txQueue := NewCommitQueue(ctx)
t.Cleanup(func() {
@@ -50,7 +50,7 @@ func TestGetPutDel(t *testing.T) {
t.Parallel()
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
txQueue := NewCommitQueue(ctx)
t.Cleanup(func() {
@@ -151,7 +151,7 @@ func testFirstLastNextPrev(t *testing.T, prefetchKeys []string,
prefetchRange []string, expectedCallCount int) {
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
txQueue := NewCommitQueue(ctx)
t.Cleanup(func() {
@@ -325,7 +325,7 @@ func TestCommitError(t *testing.T) {
t.Parallel()
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
txQueue := NewCommitQueue(ctx)
t.Cleanup(func() {
@@ -374,7 +374,7 @@ func TestManualTxError(t *testing.T) {
t.Parallel()
f := NewEtcdTestFixture(t)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
txQueue := NewCommitQueue(ctx)
t.Cleanup(func() {

View File

@@ -4,7 +4,6 @@
package etcd
import (
"context"
"testing"
"github.com/btcsuite/btcwallet/walletdb/walletdbtest"
@@ -15,5 +14,5 @@ import (
func TestWalletDBInterface(t *testing.T) {
f := NewEtcdTestFixture(t)
cfg := f.BackendConfig()
walletdbtest.TestInterface(t, dbType, context.Background(), &cfg)
walletdbtest.TestInterface(t, dbType, t.Context(), &cfg)
}

View File

@@ -15,7 +15,6 @@ require (
go.etcd.io/etcd/client/pkg/v3 v3.5.12
go.etcd.io/etcd/client/v3 v3.5.12
go.etcd.io/etcd/server/v3 v3.5.12
golang.org/x/net v0.39.0
modernc.org/sqlite v1.29.10
)
@@ -118,6 +117,7 @@ require (
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect

View File

@@ -9,7 +9,6 @@ import (
"github.com/btcsuite/btcwallet/walletdb"
"github.com/btcsuite/btcwallet/walletdb/walletdbtest"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
// TestInterface performs all interfaces tests for this database driver.
@@ -24,7 +23,7 @@ func TestInterface(t *testing.T) {
// dbType is the database type name for this driver.
const dbType = "postgres"
ctx := context.Background()
ctx := t.Context()
cfg := &Config{
Dsn: f.Dsn,
}

View File

@@ -9,14 +9,13 @@ import (
"github.com/btcsuite/btcwallet/walletdb/walletdbtest"
"github.com/lightningnetwork/lnd/kvdb/sqlbase"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
// TestInterface performs all interfaces tests for this database driver.
func TestInterface(t *testing.T) {
// dbType is the database type name for this driver.
dir := t.TempDir()
ctx := context.Background()
ctx := t.Context()
sqlbase.Init(0)