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 committed 2025-08-30 14:13:44 -03:00
1 parent 6ffe257004
commit dee8ad3754
71 files changed
+361 -393

No files matched your search

+9 -9
View File
@@ -189,7 +189,7 @@ func sqlSQLite(t testing.TB, dbPath, file string) BatchedSQLQueries {
// database for testing purposes.
func kvdbPostgres(t testing.TB, dsn string) kvdb.Backend {
kvStore, err := kvdb.Open(
kvdb.PostgresBackendName, context.Background(),
kvdb.PostgresBackendName, t.Context(),
&postgres.Config{
Dsn: dsn,
MaxConnections: testMaxPostgresConnections,
@@ -217,7 +217,7 @@ func connectKVDBPostgres(t testing.TB, dsn string) V1Store {
func kvdbSqlite(t testing.TB, dbPath, fileName string) kvdb.Backend {
sqlbase.Init(testMaxSQLiteConnections)
kvStore, err := kvdb.Open(
kvdb.SqliteBackendName, context.Background(),
kvdb.SqliteBackendName, t.Context(),
&sqlite.Config{
BusyTimeout: testSQLBusyTimeout,
MaxConnections: testMaxSQLiteConnections,
@@ -273,7 +273,7 @@ func newKVStore(t testing.TB, backend kvdb.Backend) V1Store {
// provided sqldb.DB instance.
func newSQLExecutor(t testing.TB, db sqldb.DB) BatchedSQLQueries {
err := db.ApplyAllMigrations(
context.Background(), sqldb.GetMigrations(),
t.Context(), sqldb.GetMigrations(),
)
require.NoError(t, err)
@@ -317,7 +317,7 @@ func newSQLStore(t testing.TB, cfg *sqldb.QueryConfig,
// very long to sync the graph.
func TestPopulateDBs(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
// NOTE: uncomment the line below to run this test locally, then provide
// the desired source database (and make sure the destination Postgres
@@ -539,7 +539,7 @@ func TestPopulateViaMigration(t *testing.T) {
// Use the graph migration to populate the SQL graph from the
// kvdb graph.
ctx := context.Background()
ctx := t.Context()
err := dstSQL.ExecTx(
ctx, sqldb.WriteTxOpt(), func(queries SQLQueries) error {
return MigrateGraphToSQL(
@@ -556,7 +556,7 @@ func TestPopulateViaMigration(t *testing.T) {
// syncGraph synchronizes the source graph with the destination graph by
// copying all nodes and channels from the source to the destination.
func syncGraph(t *testing.T, src, dest *ChannelGraph) {
ctx := context.Background()
ctx := t.Context()
var (
s = rate.Sometimes{
@@ -683,7 +683,7 @@ func syncGraph(t *testing.T, src, dest *ChannelGraph) {
// NOTE: the TestPopulateDBs test helper can be used to populate a set of test
// DBs from a single source db.
func BenchmarkCacheLoading(b *testing.B) {
ctx := context.Background()
ctx := b.Context()
tests := []dbConnection{
kvdbBBoltConn,
@@ -723,7 +723,7 @@ func BenchmarkCacheLoading(b *testing.B) {
// NOTE: the TestPopulateDBs test helper can be used to populate a set of test
// DBs from a single source db.
func BenchmarkGraphReadMethods(b *testing.B) {
ctx := context.Background()
ctx := b.Context()
backends := []dbConnection{
kvdbBBoltConn,
@@ -900,7 +900,7 @@ func BenchmarkFindOptimalSQLQueryConfig(b *testing.B) {
for _, size := range testSizes {
b.Run(fmt.Sprintf("%s-%s-%d", configOption, dbName, size),
func(b *testing.B) {
ctx := context.Background()
ctx := b.Context()
cfg := sqldb.DefaultSQLiteConfig()
if testPostgres {
+37 -37
View File
@@ -100,7 +100,7 @@ func createTestVertex(t testing.TB) *models.LightningNode {
// TestNodeInsertionAndDeletion tests the CRUD operations for a LightningNode.
func TestNodeInsertionAndDeletion(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -276,7 +276,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
// only the pubkey is known to the database.
func TestPartialNode(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -344,7 +344,7 @@ func TestPartialNode(t *testing.T) {
// TestAliasLookup tests the alias lookup functionality of the graph store.
func TestAliasLookup(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -375,7 +375,7 @@ func TestAliasLookup(t *testing.T) {
// TestSourceNode tests the source node functionality of the graph store.
func TestSourceNode(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -402,7 +402,7 @@ func TestSourceNode(t *testing.T) {
// TestEdgeInsertionDeletion tests the basic CRUD operations for channel edges.
func TestEdgeInsertionDeletion(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -527,7 +527,7 @@ func createEdge(height, txIndex uint32, txPosition uint16, outPointIndex uint32,
// database is what we expect after calling DisconnectBlockAtHeight.
func TestDisconnectBlockAtHeight(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -818,7 +818,7 @@ func createChannelEdge(node1, node2 *models.LightningNode,
func TestEdgeInfoUpdates(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -914,7 +914,7 @@ func TestEdgeInfoUpdates(t *testing.T) {
// TestEdgePolicyCRUD tests basic CRUD operations for edge policies.
func TestEdgePolicyCRUD(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1208,7 +1208,7 @@ func newEdgePolicy(chanID uint64, updateTime int64) *models.ChannelEdgePolicy {
// TestAddEdgeProof tests the ability to add an edge proof to an existing edge.
func TestAddEdgeProof(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1267,7 +1267,7 @@ func TestAddEdgeProof(t *testing.T) {
// correctly iterates through the channels of the set source node.
func TestForEachSourceNodeChannel(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1356,7 +1356,7 @@ func TestForEachSourceNodeChannel(t *testing.T) {
func TestGraphTraversal(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1455,7 +1455,7 @@ func TestGraphTraversal(t *testing.T) {
// working correctly.
func TestGraphTraversalCacheable(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1582,7 +1582,7 @@ func TestGraphCacheTraversal(t *testing.T) {
func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes,
numChannels int) (map[uint64]struct{}, []*models.LightningNode) {
ctx := context.Background()
ctx := t.Context()
nodes := make([]*models.LightningNode, numNodes)
nodeIndex := map[string]struct{}{}
@@ -1694,7 +1694,7 @@ func assertPruneTip(t *testing.T, graph *ChannelGraph,
func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
numChans := 0
err := graph.ForEachChannel(
context.Background(), func(*models.ChannelEdgeInfo,
t.Context(), func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error {
@@ -1710,7 +1710,7 @@ func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
func assertNumNodes(t *testing.T, graph *ChannelGraph, n int) {
numNodes := 0
err := graph.ForEachNode(context.Background(),
err := graph.ForEachNode(t.Context(),
func(_ *models.LightningNode) error {
numNodes++
@@ -1772,7 +1772,7 @@ func assertChanViewEqualChanPoints(t *testing.T, a []EdgePoint,
func TestGraphPruning(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -1963,7 +1963,7 @@ func TestGraphPruning(t *testing.T) {
// known channel ID in the database.
func TestHighestChanID(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -2023,7 +2023,7 @@ func TestHighestChanID(t *testing.T) {
// insertion of a new edge, the edge update index is updated properly.
func TestChanUpdatesInHorizon(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -2181,7 +2181,7 @@ func TestChanUpdatesInHorizon(t *testing.T) {
// the most recent node updates within a particular time horizon.
func TestNodeUpdatesInHorizon(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -2352,7 +2352,7 @@ func TestFilterKnownChanIDsZombieRevival(t *testing.T) {
// know of on disk.
func TestFilterKnownChanIDs(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -2535,7 +2535,7 @@ func TestStressTestChannelGraphAPI(t *testing.T) {
t.Skipf("Skipping test in short mode")
}
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -2824,7 +2824,7 @@ func TestStressTestChannelGraphAPI(t *testing.T) {
// set of short channel ID's for a given block range.
func TestFilterChannelRange(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3043,7 +3043,7 @@ func TestFilterChannelRange(t *testing.T) {
// of ChannelEdge structs for a given set of short channel ID's.
func TestFetchChanInfos(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3146,7 +3146,7 @@ func TestFetchChanInfos(t *testing.T) {
// both sides.
func TestIncompleteChannelPolicies(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3239,7 +3239,7 @@ func TestIncompleteChannelPolicies(t *testing.T) {
// up.
func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3387,7 +3387,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
// PruneSyncState method.
func TestPruneGraphNodes(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3453,7 +3453,7 @@ func TestPruneGraphNodes(t *testing.T) {
// database, then shell edges are created for each node if needed.
func TestAddChannelEdgeShellNodes(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3492,7 +3492,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
// well.
func TestNodePruningUpdateIndexDeletion(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3553,7 +3553,7 @@ func nextUpdateTime() time.Time {
// public within the network graph.
func TestNodeIsPublic(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
// We'll start off the test by creating a small network of 3
// participants with the following graph:
@@ -3688,7 +3688,7 @@ func TestNodeIsPublic(t *testing.T) {
// DisabledChannelIDs is correct.
func TestDisabledChannelIDs(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3773,7 +3773,7 @@ func TestDisabledChannelIDs(t *testing.T) {
// receive the proper update.
func TestEdgePolicyMissingMaxHTLC(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -3916,7 +3916,7 @@ func assertNumZombies(t *testing.T, graph *ChannelGraph, expZombies uint64) {
// TestGraphZombieIndex ensures that we can mark edges correctly as zombie/live.
func TestGraphZombieIndex(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
// We'll start by creating our test graph along with a test edge.
graph := MakeTestGraph(t)
@@ -4103,7 +4103,7 @@ func TestComputeFee(t *testing.T) {
// executes multiple AddChannelEdge requests in a single txn.
func TestBatchedAddChannelEdge(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -4180,7 +4180,7 @@ func TestBatchedAddChannelEdge(t *testing.T) {
// executes multiple UpdateEdgePolicy requests in a single txn.
func TestBatchedUpdateEdgePolicy(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -4233,7 +4233,7 @@ func TestBatchedUpdateEdgePolicy(t *testing.T) {
// allocations and the total memory consumed by the full graph traversal.
func BenchmarkForEachChannel(b *testing.B) {
graph := MakeTestGraph(b)
ctx := context.Background()
ctx := b.Context()
const numNodes = 100
const numChannels = 4
@@ -4286,7 +4286,7 @@ func BenchmarkForEachChannel(b *testing.B) {
// method works as expected, and is able to handle nil self edges.
func TestGraphCacheForEachNodeChannel(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
graph := MakeTestGraph(t)
@@ -4448,7 +4448,7 @@ var testNodeAnn = "01012674c2e7ef68c73a086b7de2603f4ef1567358df84bb4edaa06c" +
// that the two messages are equal.
func TestLightningNodePersistence(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
// Create a new test graph instance.
graph := MakeTestGraph(t)
+9 -10
View File
@@ -5,7 +5,6 @@ package graphdb
import (
"bytes"
"cmp"
"context"
"crypto/rand"
"errors"
"fmt"
@@ -58,7 +57,7 @@ var (
// and expected to have the exact same data in them.
func TestMigrateGraphToSQL(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
dbFixture := NewTestDBFixture(t)
@@ -393,7 +392,7 @@ func assertInSync(t *testing.T, kvDB *KVStore, sqlDB *SQLStore,
func fetchAllNodes(t *testing.T, store V1Store) []*models.LightningNode {
nodes := make([]*models.LightningNode, 0)
err := store.ForEachNode(context.Background(),
err := store.ForEachNode(t.Context(),
func(node *models.LightningNode) error {
// Call PubKey to ensure the objects cached pubkey is set so that
@@ -423,7 +422,7 @@ func fetchAllNodes(t *testing.T, store V1Store) []*models.LightningNode {
// fetchSourceNode retrieves the source node from the given store.
func fetchSourceNode(t *testing.T, store V1Store) *models.LightningNode {
node, err := store.SourceNode(context.Background())
node, err := store.SourceNode(t.Context())
if errors.Is(err, ErrSourceNodeNotSet) {
return nil
} else {
@@ -461,7 +460,7 @@ func (c chanSet) CountPolicies() int {
// fetchAllChannelsAndPolicies retrieves all channels and their policies
// from the given store and returns them sorted by their channel ID.
func fetchAllChannelsAndPolicies(t *testing.T, store V1Store) chanSet {
ctx := context.Background()
ctx := t.Context()
channels := make(chanSet, 0)
err := store.ForEachChannel(ctx, func(info *models.ChannelEdgeInfo,
p1 *models.ChannelEdgePolicy,
@@ -506,7 +505,7 @@ func checkKVPruneLogEntries(t *testing.T, kv *KVStore, sql *SQLStore,
err := forEachPruneLogEntry(
kv.db, func(height uint32, hash *chainhash.Hash) error {
sqlHash, err := sql.db.GetPruneHashByHeight(
context.Background(), int64(height),
t.Context(), int64(height),
)
require.NoError(t, err)
require.Equal(t, hash[:], sqlHash)
@@ -736,7 +735,7 @@ func makeTestPolicy(chanID uint64, toNode route.Vertex, isNode1 bool,
// and set the "fileName" variable to the name of the channel database file you
// want to use for the migration test.
func TestMigrationWithChannelDB(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
// NOTE: comment this line out to run the test.
t.Skipf("skipping test meant for local debugging only")
@@ -855,7 +854,7 @@ func TestMigrationWithChannelDB(t *testing.T) {
// will differ slightly.
func TestSQLMigrationEdgeCases(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := t.Context()
var invalidTLVData = []byte{0x01, 0x02, 0x03}
@@ -1115,7 +1114,7 @@ func TestSQLMigrationEdgeCases(t *testing.T) {
func runTestMigration(t *testing.T, populateKV func(t *testing.T, db *KVStore),
expState dbState) {
ctx := context.Background()
ctx := t.Context()
// Set up our source kvdb DB.
kvDB := setUpKVStore(t)
@@ -1208,7 +1207,7 @@ func TestMigrateGraphToSQLRapid(t *testing.T) {
func testMigrateGraphToSQLRapidOnce(t *testing.T, rt *rapid.T,
dbFixture *sqldb.TestPgFixture, maxNumNodes, maxNumChannels int) {
ctx := context.Background()
ctx := t.Context()
// Set up our source kvdb DB.
kvDB := setUpKVStore(t)