Skip to content

Commit b600049

Browse files
authored
Merge pull request #1482 from lightninglabs/fix-planter-unittest-flakes
Fix CI Unit Test Flakes
2 parents 89a3e17 + e5ac27c commit b600049

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

tapgarden/custodian_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,28 @@ func TestBookAssetSyncer(t *testing.T) {
584584
// If we add the asset to the asset syncer, address creation should
585585
// succeed.
586586
h.syncer.AddAsset(*newAsset)
587-
addrVersion = test.RandFlip(address.V0, address.V1)
588-
newAddr, err := h.addrBook.NewAddress(
589-
ctx, addrVersion, newAsset.ID(), 1, nil, proofCourierAddr,
590-
)
587+
588+
// Fetch the asset from the syncer. This should trigger the
589+
// background goroutine to add the asset to the address book.
590+
_, err = h.syncer.FetchAsset(newAsset.ID())
591591
require.NoError(t, err)
592-
require.NotNil(t, newAddr)
592+
593+
// Eventually, the asset should be registered and we should be able to
594+
// create a new address for it.
595+
var newAddr *address.AddrWithKeyInfo
596+
addrVersion = test.RandFlip(address.V0, address.V1)
597+
598+
require.Eventually(t, func() bool {
599+
newAddr, err = h.addrBook.NewAddress(
600+
ctx, addrVersion, newAsset.ID(), 1, nil,
601+
proofCourierAddr,
602+
)
603+
if err != nil {
604+
return false
605+
}
606+
607+
return newAddr != nil
608+
}, defaultTimeout, wait.PollInterval)
593609

594610
h.keyRing.AssertNumberOfCalls(t, "DeriveNextTaprootAssetKey", 2)
595611

tapgarden/planter_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646
// Default to a large interval so the planter never actually ticks and only
4747
// rely on our manual ticks.
4848
var (
49-
defaultTimeout = time.Second * 5
49+
defaultTimeout = time.Second * 10
5050
noCaretakerStates = fn.NewSet(
5151
tapgarden.BatchStatePending,
5252
tapgarden.BatchStateSeedlingCancelled,
@@ -309,7 +309,18 @@ func (t *mintingTestHarness) queueSeedlingsInBatch(isFunded bool,
309309
// The received update should be a state of MintingStateSeed.
310310
require.Equal(t, tapgarden.MintingStateSeed, update.NewState)
311311

312-
t.keyRing.AssertNumberOfCalls(t, "DeriveNextKey", keyCount)
312+
require.Eventually(t, func() bool {
313+
// Assert that the key ring method DeriveNextKey was
314+
// called the expected number of times.
315+
count := 0
316+
for _, call := range t.keyRing.Calls {
317+
if call.Method == "DeriveNextKey" {
318+
count++
319+
}
320+
}
321+
322+
return count == keyCount
323+
}, defaultTimeout, wait.PollInterval)
313324
}
314325
}
315326

@@ -318,10 +329,15 @@ func (t *mintingTestHarness) queueSeedlingsInBatch(isFunded bool,
318329
func (t *mintingTestHarness) assertPendingBatchExists(numSeedlings int) {
319330
t.Helper()
320331

321-
batch, err := t.planter.PendingBatch()
322-
require.NoError(t, err)
323-
require.NotNil(t, batch)
324-
require.Len(t, batch.Seedlings, numSeedlings)
332+
// The planter is a state machine, so we need to wait until it has
333+
// reached the expected state.
334+
require.Eventually(t, func() bool {
335+
batch, err := t.planter.PendingBatch()
336+
require.NoError(t, err)
337+
338+
require.NotNil(t, batch)
339+
return len(batch.Seedlings) == numSeedlings
340+
}, defaultTimeout, wait.PollInterval)
325341
}
326342

327343
// assertNoActiveBatch asserts that no pending batch exists.

0 commit comments

Comments
 (0)