Skip to content

Commit fe1bb25

Browse files
dajohijrick
authored andcommitted
tests: Use a single context in tests.
1 parent 18d4c02 commit fe1bb25

15 files changed

+112
-129
lines changed

wallet/addresses_test.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var (
130130
}
131131
)
132132

133-
func setupWallet(t *testing.T, cfg *Config) (*Wallet, walletdb.DB, func()) {
133+
func setupWallet(ctx context.Context, t *testing.T, cfg *Config) (*Wallet, walletdb.DB, func()) {
134134
f, err := os.CreateTemp("", "testwallet.db")
135135
if err != nil {
136136
t.Fatal(err)
@@ -141,7 +141,6 @@ func setupWallet(t *testing.T, cfg *Config) (*Wallet, walletdb.DB, func()) {
141141
if err != nil {
142142
t.Fatal(err)
143143
}
144-
ctx := context.Background()
145144
err = Create(ctx, opaqueDB{db}, pubPassphrase, privPassphrase, seed, cfg.Params)
146145
if err != nil {
147146
db.Close()
@@ -167,12 +166,10 @@ func setupWallet(t *testing.T, cfg *Config) (*Wallet, walletdb.DB, func()) {
167166

168167
type newAddressFunc func(*Wallet, context.Context, uint32, ...NextAddressCallOption) (stdaddr.Address, error)
169168

170-
func testKnownAddresses(tc *testContext, prefix string, unlock bool, newAddr newAddressFunc, tests []expectedAddr) {
171-
w, db, teardown := setupWallet(tc.t, &walletConfig)
169+
func testKnownAddresses(ctx context.Context, tc *testContext, prefix string, unlock bool, newAddr newAddressFunc, tests []expectedAddr) {
170+
w, db, teardown := setupWallet(ctx, tc.t, &walletConfig)
172171
defer teardown()
173172

174-
ctx := context.Background()
175-
176173
if unlock {
177174
err := w.Unlock(ctx, privPassphrase, nil)
178175
if err != nil {
@@ -192,7 +189,7 @@ func testKnownAddresses(tc *testContext, prefix string, unlock bool, newAddr new
192189
}
193190

194191
for i := 0; i < len(tests); i++ {
195-
addr, err := newAddr(w, context.Background(), defaultAccount)
192+
addr, err := newAddr(w, ctx, defaultAccount)
196193
if err != nil {
197194
tc.t.Fatalf("%s: failed to generate external address: %v",
198195
prefix, err)
@@ -235,45 +232,49 @@ func testKnownAddresses(tc *testContext, prefix string, unlock bool, newAddr new
235232
}
236233

237234
func TestAddresses(t *testing.T) {
238-
testAddresses(t, false)
239-
testAddresses(t, true)
235+
ctx := context.Background()
236+
237+
testAddresses(ctx, t, false)
238+
testAddresses(ctx, t, true)
240239
}
241240

242-
func testAddresses(t *testing.T, unlock bool) {
243-
testKnownAddresses(&testContext{
241+
func testAddresses(ctx context.Context, t *testing.T, unlock bool) {
242+
testKnownAddresses(ctx, &testContext{
244243
t: t,
245244
account: defaultAccount,
246245
watchingOnly: false,
247246
}, "testInternalAddresses", unlock, (*Wallet).NewInternalAddress, expectedInternalAddrs)
248247

249-
testKnownAddresses(&testContext{
248+
testKnownAddresses(ctx, &testContext{
250249
t: t,
251250
account: defaultAccount,
252251
watchingOnly: true,
253252
}, "testInternalAddresses", unlock, (*Wallet).NewInternalAddress, expectedInternalAddrs)
254253

255-
testKnownAddresses(&testContext{
254+
testKnownAddresses(ctx, &testContext{
256255
t: t,
257256
account: defaultAccount,
258257
watchingOnly: false,
259258
}, "testExternalAddresses", unlock, (*Wallet).NewExternalAddress, expectedExternalAddrs)
260259

261-
testKnownAddresses(&testContext{
260+
testKnownAddresses(ctx, &testContext{
262261
t: t,
263262
account: defaultAccount,
264263
watchingOnly: true,
265264
}, "testExternalAddresses", unlock, (*Wallet).NewExternalAddress, expectedExternalAddrs)
266265
}
267266

268267
func TestAccountIndexes(t *testing.T) {
268+
ctx := context.Background()
269+
269270
cfg := basicWalletConfig
270-
w, teardown := testWallet(t, &cfg)
271+
w, teardown := testWallet(ctx, t, &cfg)
271272
defer teardown()
272273

273274
w.SetNetworkBackend(mockNetwork{})
274275

275276
tests := []struct {
276-
f func(t *testing.T, w *Wallet)
277+
f func(ctx context.Context, t *testing.T, w *Wallet)
277278
indexes accountIndexes
278279
}{
279280
{nil, accountIndexes{{^uint32(0), 0}, {^uint32(0), 0}}},
@@ -288,7 +289,7 @@ func TestAccountIndexes(t *testing.T) {
288289
}
289290
for i, test := range tests {
290291
if test.f != nil {
291-
test.f(t, w)
292+
test.f(ctx, t, w)
292293
}
293294
w.addressBuffersMu.Lock()
294295
b := w.addressBuffers[0]
@@ -311,29 +312,27 @@ type accountIndexes [2]struct {
311312
last, cursor uint32
312313
}
313314

314-
func nextAddresses(n int) func(t *testing.T, w *Wallet) {
315-
return func(t *testing.T, w *Wallet) {
315+
func nextAddresses(n int) func(ctx context.Context, t *testing.T, w *Wallet) {
316+
return func(ctx context.Context, t *testing.T, w *Wallet) {
316317
for i := 0; i < n; i++ {
317-
_, err := w.NewExternalAddress(context.Background(), 0)
318+
_, err := w.NewExternalAddress(ctx, 0)
318319
if err != nil {
319320
t.Fatal(err)
320321
}
321322
}
322323
}
323324
}
324325

325-
func watchFutureAddresses(t *testing.T, w *Wallet) {
326-
ctx := context.Background()
326+
func watchFutureAddresses(ctx context.Context, t *testing.T, w *Wallet) {
327327
n, _ := w.NetworkBackend()
328328
_, err := w.watchHDAddrs(ctx, false, n)
329329
if err != nil {
330330
t.Fatal(err)
331331
}
332332
}
333333

334-
func useAddress(child uint32) func(t *testing.T, w *Wallet) {
335-
ctx := context.Background()
336-
return func(t *testing.T, w *Wallet) {
334+
func useAddress(child uint32) func(ctx context.Context, t *testing.T, w *Wallet) {
335+
return func(ctx context.Context, t *testing.T, w *Wallet) {
337336
w.addressBuffersMu.Lock()
338337
xbranch := w.addressBuffers[0].albExternal.branchXpub
339338
w.addressBuffersMu.Unlock()
@@ -352,6 +351,6 @@ func useAddress(child uint32) func(t *testing.T, w *Wallet) {
352351
if err != nil {
353352
t.Fatal(err)
354353
}
355-
watchFutureAddresses(t, w)
354+
watchFutureAddresses(ctx, t, w)
356355
}
357356
}

wallet/discovery_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestDiscoveryCursorPos(t *testing.T) {
2121
// off-by-ones after the upgrade. will be fixed in a later commit.
2222
cfg.DisableCoinTypeUpgrades = true
2323

24-
w, teardown := testWallet(t, &cfg)
24+
w, teardown := testWallet(ctx, t, &cfg)
2525
defer teardown()
2626

2727
/*

wallet/locking_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@ import (
1515
var testPrivPass = []byte("private")
1616

1717
func TestLocking(t *testing.T) {
18-
w, teardown := testWallet(t, &basicWalletConfig)
18+
ctx := context.Background()
19+
20+
w, teardown := testWallet(ctx, t, &basicWalletConfig)
1921
defer teardown()
2022

21-
var tests = []func(t *testing.T, w *Wallet){
23+
var tests = []func(ctx context.Context, t *testing.T, w *Wallet){
2224
testUnlock,
2325
testLockOnBadPassphrase,
2426
testNoNilTimeoutReplacement,
2527
testNonNilTimeoutLock,
2628
testTimeoutReplacement,
2729
}
2830
for _, test := range tests {
29-
test(t, w)
31+
test(ctx, t, w)
3032
w.Lock()
3133
}
3234
}
3335

34-
func testUnlock(t *testing.T, w *Wallet) {
35-
ctx := context.Background()
36+
func testUnlock(ctx context.Context, t *testing.T, w *Wallet) {
3637
if !w.Locked() {
3738
t.Fatal("expected wallet to be locked")
3839
}
@@ -60,8 +61,7 @@ func testUnlock(t *testing.T, w *Wallet) {
6061
}
6162
}
6263

63-
func testLockOnBadPassphrase(t *testing.T, w *Wallet) {
64-
ctx := context.Background()
64+
func testLockOnBadPassphrase(ctx context.Context, t *testing.T, w *Wallet) {
6565
err := w.Unlock(ctx, testPrivPass, nil)
6666
if err != nil {
6767
t.Fatal("failed to unlock wallet")
@@ -90,8 +90,7 @@ func testLockOnBadPassphrase(t *testing.T, w *Wallet) {
9090
// Test:
9191
// If the wallet is currently unlocked without any timeout, timeout is ignored
9292
// and if non-nil, is read in a background goroutine to avoid blocking sends.
93-
func testNoNilTimeoutReplacement(t *testing.T, w *Wallet) {
94-
ctx := context.Background()
93+
func testNoNilTimeoutReplacement(ctx context.Context, t *testing.T, w *Wallet) {
9594
err := w.Unlock(ctx, testPrivPass, nil)
9695
if err != nil {
9796
t.Fatal("failed to unlock wallet")
@@ -114,9 +113,8 @@ func testNoNilTimeoutReplacement(t *testing.T, w *Wallet) {
114113
// Test:
115114
// If the wallet is locked and a non-nil timeout is provided, the wallet will be
116115
// locked in the background after reading from the channel.
117-
func testNonNilTimeoutLock(t *testing.T, w *Wallet) {
116+
func testNonNilTimeoutLock(ctx context.Context, t *testing.T, w *Wallet) {
118117
timeChan := make(chan time.Time)
119-
ctx := context.Background()
120118
err := w.Unlock(ctx, testPrivPass, timeChan)
121119
if err != nil {
122120
t.Fatal("failed to unlock wallet")
@@ -131,10 +129,9 @@ func testNonNilTimeoutLock(t *testing.T, w *Wallet) {
131129
// Test:
132130
// If the wallet is already unlocked with a previous timeout, the new timeout
133131
// replaces the prior.
134-
func testTimeoutReplacement(t *testing.T, w *Wallet) {
132+
func testTimeoutReplacement(ctx context.Context, t *testing.T, w *Wallet) {
135133
timeChan1 := make(chan time.Time)
136134
timeChan2 := make(chan time.Time)
137-
ctx := context.Background()
138135
err := w.Unlock(ctx, testPrivPass, timeChan1)
139136
if err != nil {
140137
t.Fatal("failed to unlock wallet")

wallet/reorg_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ func mustAddBlockNode(t *testing.T, forest *SidechainForest, n *BlockNode) {
9191
}
9292
}
9393

94-
func (tw *tw) evaluateBestChain(forest *SidechainForest, expectedBranchLen int, expectedTip *chainhash.Hash) []*BlockNode {
95-
ctx := context.Background()
94+
func (tw *tw) evaluateBestChain(ctx context.Context, forest *SidechainForest,
95+
expectedBranchLen int, expectedTip *chainhash.Hash) []*BlockNode {
96+
9697
bestChain, err := tw.EvaluateBestChain(ctx, forest)
9798
if err != nil {
9899
tw.Fatal(err)
@@ -106,13 +107,12 @@ func (tw *tw) evaluateBestChain(forest *SidechainForest, expectedBranchLen int,
106107
return bestChain
107108
}
108109

109-
func (tw *tw) assertNoBetterChain(forest *SidechainForest) {
110-
tw.evaluateBestChain(forest, 0, nil)
110+
func (tw *tw) assertNoBetterChain(ctx context.Context, forest *SidechainForest) {
111+
tw.evaluateBestChain(ctx, forest, 0, nil)
111112
}
112113

113-
func (tw *tw) chainSwitch(forest *SidechainForest, chain []*BlockNode) {
114-
ctx := context.Background()
115-
prevChain, err := tw.ChainSwitch(context.Background(), forest, chain, nil)
114+
func (tw *tw) chainSwitch(ctx context.Context, forest *SidechainForest, chain []*BlockNode) {
115+
prevChain, err := tw.ChainSwitch(ctx, forest, chain, nil)
116116
if err != nil {
117117
tw.Fatal(err)
118118
}
@@ -125,8 +125,7 @@ func (tw *tw) chainSwitch(forest *SidechainForest, chain []*BlockNode) {
125125
}
126126
}
127127

128-
func (tw *tw) expectBlockInMainChain(hash *chainhash.Hash, have, invalidated bool) {
129-
ctx := context.Background()
128+
func (tw *tw) expectBlockInMainChain(ctx context.Context, hash *chainhash.Hash, have, invalidated bool) {
130129
haveBlock, isInvalidated, err := tw.BlockInMainChain(ctx, hash)
131130
if err != nil {
132131
tw.Fatal(err)
@@ -155,9 +154,10 @@ func assertSidechainTree(t *testing.T, tree *sidechainRootedTree, root *chainhas
155154

156155
func TestReorg(t *testing.T) {
157156
t.Parallel()
157+
ctx := context.Background()
158158

159159
cfg := basicWalletConfig
160-
w, teardown := testWallet(t, &cfg)
160+
w, teardown := testWallet(ctx, t, &cfg)
161161
defer teardown()
162162

163163
tg := maketg(t, cfg.Params)
@@ -168,13 +168,13 @@ func TestReorg(t *testing.T) {
168168
mustAddBlockNode(t, forest, blockOne.BlockNode)
169169
t.Logf("Generated block one %v", blockOne.Hash)
170170

171-
bestChain := tw.evaluateBestChain(forest, 1, blockOne.Hash)
172-
tw.chainSwitch(forest, bestChain)
171+
bestChain := tw.evaluateBestChain(ctx, forest, 1, blockOne.Hash)
172+
tw.chainSwitch(ctx, forest, bestChain)
173173
t.Logf("Attached block one %v", blockOne.Hash)
174174
if len(forest.trees) != 0 {
175175
t.Fatalf("Did not prune block one from forest")
176176
}
177-
tw.assertNoBetterChain(forest)
177+
tw.assertNoBetterChain(ctx, forest)
178178

179179
// Generate blocks 2a and 3a and attach to the wallet's main chain together.
180180
for i := 2; i <= 3; i++ {
@@ -189,12 +189,12 @@ func TestReorg(t *testing.T) {
189189
b2aHash := tg.blockHashByName("2a")
190190
b3aHash := tg.blockHashByName("3a")
191191
assertSidechainTree(t, forest.trees[0], b2aHash, b3aHash)
192-
bestChain = tw.evaluateBestChain(forest, 2, b3aHash)
193-
tw.chainSwitch(forest, bestChain)
192+
bestChain = tw.evaluateBestChain(ctx, forest, 2, b3aHash)
193+
tw.chainSwitch(ctx, forest, bestChain)
194194
if len(forest.trees) != 0 {
195195
t.Fatalf("Did not prune blocks 2a-3a from forest")
196196
}
197-
tw.assertNoBetterChain(forest)
197+
tw.assertNoBetterChain(ctx, forest)
198198

199199
// Generate sidechain blocks 2b-3b and assert it does not create a better
200200
// chain.
@@ -211,7 +211,7 @@ func TestReorg(t *testing.T) {
211211
b2bHash := tg.blockHashByName("2b")
212212
b3bHash := tg.blockHashByName("3b")
213213
assertSidechainTree(t, forest.trees[0], b2bHash, b3bHash)
214-
tw.assertNoBetterChain(forest)
214+
tw.assertNoBetterChain(ctx, forest)
215215

216216
// Generate sidechain block 4b, and attach the better chain 2b-4b to
217217
// wallet's main chain, reorging out 2a and 3a.
@@ -224,16 +224,16 @@ func TestReorg(t *testing.T) {
224224
t.Fatalf("Expected one tree in forest")
225225
}
226226
assertSidechainTree(t, forest.trees[0], b2bHash, b4bHash)
227-
bestChain = tw.evaluateBestChain(forest, 3, b4bHash)
228-
tw.chainSwitch(forest, bestChain)
227+
bestChain = tw.evaluateBestChain(ctx, forest, 3, b4bHash)
228+
tw.chainSwitch(ctx, forest, bestChain)
229229
if len(forest.trees) != 1 {
230230
t.Fatalf("Expected single tree in forest after reorg")
231231
}
232-
tw.assertNoBetterChain(forest)
232+
tw.assertNoBetterChain(ctx, forest)
233233
assertSidechainTree(t, forest.trees[0], b2aHash, b3aHash)
234-
tw.expectBlockInMainChain(b2aHash, false, false)
235-
tw.expectBlockInMainChain(b3aHash, false, false)
236-
tw.expectBlockInMainChain(b2bHash, true, false)
237-
tw.expectBlockInMainChain(b3bHash, true, false)
238-
tw.expectBlockInMainChain(b4bHash, true, false)
234+
tw.expectBlockInMainChain(ctx, b2aHash, false, false)
235+
tw.expectBlockInMainChain(ctx, b3aHash, false, false)
236+
tw.expectBlockInMainChain(ctx, b2bHash, true, false)
237+
tw.expectBlockInMainChain(ctx, b3bHash, true, false)
238+
tw.expectBlockInMainChain(ctx, b4bHash, true, false)
239239
}

wallet/rescan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (w *Wallet) SaveRescanned(ctx context.Context, hash *chainhash.Hash, txs []
174174
if err != nil {
175175
return err
176176
}
177-
_, err = w.processTransactionRecord(context.Background(), dbtx, rec, header, &blockMeta)
177+
_, err = w.processTransactionRecord(ctx, dbtx, rec, header, &blockMeta)
178178
if err != nil {
179179
return err
180180
}

wallet/setup_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ var basicWalletConfig = Config{
2222
Params: chaincfg.SimNetParams(),
2323
}
2424

25-
func testWallet(t *testing.T, cfg *Config) (w *Wallet, teardown func()) {
26-
ctx := context.Background()
25+
func testWallet(ctx context.Context, t *testing.T, cfg *Config) (w *Wallet, teardown func()) {
2726
f, err := os.CreateTemp("", "dcrwallet.testdb")
2827
if err != nil {
2928
t.Fatal(err)

0 commit comments

Comments
 (0)