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

+18 -19
View File
@@ -1,7 +1,6 @@
package invoices_test
import (
"context"
"crypto/rand"
"database/sql"
"fmt"
@@ -194,7 +193,7 @@ func testSettleInvoice(t *testing.T,
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.Nil(t, err)
defer allSubscriptions.Cancel()
@@ -383,7 +382,7 @@ func testCancelInvoiceImpl(t *testing.T, gc bool,
cfg.GcCanceledInvoicesOnTheFly = gc
ctx := newTestContext(t, &cfg, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.Nil(t, err)
defer allSubscriptions.Cancel()
@@ -547,7 +546,7 @@ func testSettleHoldInvoice(t *testing.T,
require.NoError(t, err)
defer registry.Stop()
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := registry.SubscribeNotifications(ctxb, 0, 0)
require.Nil(t, err)
defer allSubscriptions.Cancel()
@@ -720,7 +719,7 @@ func testCancelHoldInvoice(t *testing.T,
require.NoError(t, registry.Stop())
})
ctxb := context.Background()
ctxb := t.Context()
// Add the invoice.
invoice := newInvoice(t, true, false)
@@ -824,7 +823,7 @@ func testKeySendImpl(t *testing.T, keySendEnabled bool,
ctx := newTestContext(t, &cfg, makeDB)
allSubscriptions, err := ctx.registry.SubscribeNotifications(
context.Background(), 0, 0,
t.Context(), 0, 0,
)
require.NoError(t, err)
defer allSubscriptions.Cancel()
@@ -959,7 +958,7 @@ func testHoldKeysendImpl(t *testing.T, timeoutKeysend bool,
cfg.KeysendHoldTime = holdDuration
ctx := newTestContext(t, &cfg, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.NoError(t, err)
defer allSubscriptions.Cancel()
@@ -1047,7 +1046,7 @@ func testMppPayment(t *testing.T,
defer timeout()()
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
// Add the invoice.
testInvoice := newInvoice(t, false, false)
@@ -1143,7 +1142,7 @@ func testMppPaymentWithOverpayment(t *testing.T,
t.Parallel()
ctxb := context.Background()
ctxb := t.Context()
f := func(overpaymentRand uint64) bool {
ctx := newTestContext(t, nil, makeDB)
@@ -1241,7 +1240,7 @@ func testInvoiceExpiryWithRegistry(t *testing.T,
t, testTime, 0, numExpired, numPending,
)
ctxb := context.Background()
ctxb := t.Context()
var expectedCancellations []lntypes.Hash
expiredInvoices := existingInvoices.expiredInvoices
@@ -1351,7 +1350,7 @@ func testOldInvoiceRemovalOnStart(t *testing.T,
t, testTime, 0, numExpired, numPending,
)
ctxb := context.Background()
ctxb := t.Context()
i := 0
for paymentHash, invoice := range existingInvoices.expiredInvoices {
@@ -1443,7 +1442,7 @@ func testHeightExpiryWithRegistryImpl(t *testing.T, numParts int, settle bool,
testInvoice.HodlInvoice = true
testInvoice.PaymentRequest = []byte{1, 2, 3}
ctxb := context.Background()
ctxb := t.Context()
_, err := ctx.registry.AddInvoice(
ctxb, testInvoice, testInvoicePaymentHash,
)
@@ -1554,7 +1553,7 @@ func testMultipleSetHeightExpiry(t *testing.T,
// Add a hold invoice.
testInvoice := newInvoice(t, true, false)
ctxb := context.Background()
ctxb := t.Context()
_, err := ctx.registry.AddInvoice(
ctxb, testInvoice, testInvoicePaymentHash,
)
@@ -1646,7 +1645,7 @@ func testSettleInvoicePaymentAddrRequired(t *testing.T,
t.Parallel()
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.NoError(t, err)
@@ -1738,7 +1737,7 @@ func testSettleInvoicePaymentAddrRequiredOptionalGrace(t *testing.T,
t.Parallel()
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.NoError(t, err)
@@ -1946,7 +1945,7 @@ func testSpontaneousAmpPaymentImpl(
cfg := defaultRegistryConfig()
cfg.AcceptAMP = ampEnabled
ctx := newTestContext(t, &cfg, makeDB)
ctxb := context.Background()
ctxb := t.Context()
allSubscriptions, err := ctx.registry.SubscribeNotifications(ctxb, 0, 0)
require.Nil(t, err)
@@ -2132,7 +2131,7 @@ func testFailPartialMPPPaymentExternal(t *testing.T,
// Add an invoice which we are going to pay via a MPP set.
testInvoice := newInvoice(t, false, false)
ctxb := context.Background()
ctxb := t.Context()
_, err := ctx.registry.AddInvoice(
ctxb, testInvoice, testInvoicePaymentHash,
)
@@ -2243,7 +2242,7 @@ func testFailPartialAMPPayment(t *testing.T,
t.Parallel()
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
const (
expiry = uint32(testCurrentHeight + 20)
@@ -2449,7 +2448,7 @@ func testCancelAMPInvoicePendingHTLCs(t *testing.T,
t.Parallel()
ctx := newTestContext(t, nil, makeDB)
ctxb := context.Background()
ctxb := t.Context()
const (
expiry = uint32(testCurrentHeight + 20)
+21 -22
View File
@@ -1,7 +1,6 @@
package invoices_test
import (
"context"
"crypto/rand"
"database/sql"
"fmt"
@@ -373,7 +372,7 @@ func testInvoiceWorkflowImpl(t *testing.T, test invWorkflowTest,
ref = invpkg.InvoiceRefByHash(payHash)
}
ctxb := context.Background()
ctxb := t.Context()
// Add the invoice to the database, this should succeed as there aren't
// any existing invoices within the database with the same payment
// hash.
@@ -498,7 +497,7 @@ func testAddDuplicatePayAddr(t *testing.T,
require.NoError(t, err)
invoice2.Terms.PaymentAddr = invoice1.Terms.PaymentAddr
ctxb := context.Background()
ctxb := t.Context()
// First insert should succeed.
inv1Hash := invoice1.Terms.PaymentPreimage.Hash()
@@ -529,7 +528,7 @@ func testAddDuplicateKeysendPayAddr(t *testing.T,
require.NoError(t, err)
invoice2.Terms.PaymentAddr = invpkg.BlankPayAddr
ctxb := context.Background()
ctxb := t.Context()
// Inserting both should succeed without a duplicate payment address
// failure.
@@ -573,7 +572,7 @@ func testFailInvoiceLookupMPPPayAddrOnly(t *testing.T,
payHash := invoice.Terms.PaymentPreimage.Hash()
payAddr := invoice.Terms.PaymentAddr
ctxb := context.Background()
ctxb := t.Context()
_, err = db.AddInvoice(ctxb, invoice, payHash)
require.NoError(t, err)
@@ -601,7 +600,7 @@ func testInvRefEquivocation(t *testing.T,
invoice1, err := randInvoice(1000)
require.NoError(t, err)
ctxb := context.Background()
ctxb := t.Context()
inv1Hash := invoice1.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(ctxb, invoice1, inv1Hash)
require.NoError(t, err)
@@ -651,7 +650,7 @@ func testInvoiceCancelSingleHtlc(t *testing.T,
},
}
ctxb := context.Background()
ctxb := t.Context()
if _, err := db.AddInvoice(ctxb, testInvoice, paymentHash); err != nil {
t.Fatalf("unable to find invoice: %v", err)
}
@@ -729,7 +728,7 @@ func testInvoiceCancelSingleHtlcAMP(t *testing.T,
// preimages.
invoice.Terms.Features = ampFeatures
ctxb := context.Background()
ctxb := t.Context()
preimage := *invoice.Terms.PaymentPreimage
payHash := preimage.Hash()
_, err = db.AddInvoice(ctxb, invoice, payHash)
@@ -898,7 +897,7 @@ func testInvoiceAddTimeSeries(t *testing.T,
t.Parallel()
db := makeDB(t)
ctxb := context.Background()
ctxb := t.Context()
_, err := db.InvoicesAddedSince(ctxb, 0)
require.NoError(t, err)
@@ -1073,7 +1072,7 @@ func testSettleIndexAmpPayments(t *testing.T,
// Add the invoice to the DB, we use a dummy payment hash here but the
// invoice will have a valid payment address set.
ctxb := context.Background()
ctxb := t.Context()
preimage := *testInvoice.Terms.PaymentPreimage
payHash := preimage.Hash()
_, err = db.AddInvoice(ctxb, testInvoice, payHash)
@@ -1236,7 +1235,7 @@ func testFetchPendingInvoices(t *testing.T,
t.Parallel()
db := makeDB(t)
ctxb := context.Background()
ctxb := t.Context()
// Make sure that fetching pending invoices from an empty database
// returns an empty result and no errors.
@@ -1305,7 +1304,7 @@ func testDuplicateSettleInvoice(t *testing.T,
payHash := invoice.Terms.PaymentPreimage.Hash()
ctxb := context.Background()
ctxb := t.Context()
if _, err := db.AddInvoice(ctxb, invoice, payHash); err != nil {
t.Fatalf("unable to add invoice %v", err)
}
@@ -1372,7 +1371,7 @@ func testQueryInvoices(t *testing.T,
pendingInvoices []invpkg.Invoice
)
ctxb := context.Background()
ctxb := t.Context()
for i := 1; i <= numInvoices; i++ {
amt := lnwire.MilliSatoshi(i)
invoice, err := randInvoice(amt)
@@ -1813,7 +1812,7 @@ func testCustomRecords(t *testing.T,
},
}
ctxb := context.Background()
ctxb := t.Context()
if _, err := db.AddInvoice(ctxb, testInvoice, paymentHash); err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
@@ -1893,7 +1892,7 @@ func testInvoiceHtlcAMPFieldsImpl(t *testing.T, isAMP bool,
testInvoice.Terms.Features = ampFeatures
}
ctxb := context.Background()
ctxb := t.Context()
payHash := testInvoice.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(ctxb, testInvoice, payHash)
require.Nil(t, err)
@@ -2101,7 +2100,7 @@ func testAddInvoiceWithHTLCs(t *testing.T,
testInvoice.Htlcs[key] = &invpkg.InvoiceHTLC{}
payHash := testInvoice.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(context.Background(), testInvoice, payHash)
_, err = db.AddInvoice(t.Context(), testInvoice, payHash)
require.Equal(t, invpkg.ErrInvoiceHasHtlcs, err)
}
@@ -2121,7 +2120,7 @@ func testSetIDIndex(t *testing.T, makeDB func(t *testing.T) invpkg.InvoiceDB) {
// preimages.
invoice.Terms.Features = ampFeatures
ctxb := context.Background()
ctxb := t.Context()
preimage := *invoice.Terms.PaymentPreimage
payHash := preimage.Hash()
_, err = db.AddInvoice(ctxb, invoice, payHash)
@@ -2460,7 +2459,7 @@ func testUnexpectedInvoicePreimage(t *testing.T,
invoice, err := randInvoice(lnwire.MilliSatoshi(100))
require.NoError(t, err)
ctxb := context.Background()
ctxb := t.Context()
// Add a random invoice indexed by payment hash and payment addr.
paymentHash := invoice.Terms.PaymentPreimage.Hash()
@@ -2530,7 +2529,7 @@ func testUpdateHTLCPreimagesImpl(t *testing.T, test updateHTLCPreimageTestCase,
// preimages.
invoice.Terms.Features = ampFeatures
ctxb := context.Background()
ctxb := t.Context()
_, err = db.AddInvoice(ctxb, invoice, payHash)
require.Nil(t, err)
@@ -2594,7 +2593,7 @@ func testDeleteInvoices(t *testing.T,
numInvoices := 3
invoicesToDelete := make([]invpkg.InvoiceDeleteRef, numInvoices)
ctxb := context.Background()
ctxb := t.Context()
for i := 0; i < numInvoices; i++ {
invoice, err := randInvoice(lnwire.MilliSatoshi(i + 1))
require.NoError(t, err)
@@ -2689,7 +2688,7 @@ func testDeleteCanceledInvoices(t *testing.T,
}
// Test deletion of canceled invoices when there are none.
ctxb := context.Background()
ctxb := t.Context()
require.NoError(t, db.DeleteCanceledInvoices(ctxb))
// Add some invoices to the test db.
@@ -2750,7 +2749,7 @@ func testAddInvoiceInvalidFeatureDeps(t *testing.T,
)
hash := invoice.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(context.Background(), invoice, hash)
_, err = db.AddInvoice(t.Context(), invoice, hash)
require.Error(t, err, feature.NewErrMissingFeatureDep(
lnwire.PaymentAddrOptional,
))
+2 -3
View File
@@ -1,7 +1,6 @@
package invoices_test
import (
"context"
"database/sql"
"os"
"path"
@@ -67,7 +66,7 @@ func TestMigrationWithChannelDB(t *testing.T) {
sqlite bool) {
sqlInvoiceStore, sqlStore := makeSQLDB(t, sqlite)
ctxb := context.Background()
ctxb := t.Context()
const batchSize = 11
err := sqlStore.ExecTx(
@@ -163,7 +162,7 @@ func TestMigrationWithChannelDB(t *testing.T) {
sqliteBackend, err := kvdb.Open(
kvdb.SqliteBackendName,
context.Background(),
t.Context(),
sqliteConfig, test.dbPath,
lncfg.SqliteChannelDBName,
lncfg.NSChannelDB,
+1 -2
View File
@@ -1,7 +1,6 @@
package invoices
import (
"context"
crand "crypto/rand"
"database/sql"
"math/rand"
@@ -305,7 +304,7 @@ func TestMigrateSingleInvoiceRapid(t *testing.T) {
func testMigrateSingleInvoiceRapid(t *rapid.T, store *SQLStore, mpp bool,
amp bool) {
ctxb := context.Background()
ctxb := t.Context()
invoices := make(map[lntypes.Hash]*Invoice)
for i := 0; i < 100; i++ {