Skip to content

Commit e28ce8d

Browse files
committed
tapgarden: use atomic counter in MockKeyRing and relax test checks
Introduce an atomic counter `deriveNextKeyCallCount` in `MockKeyRing` to track calls to `DeriveNextKey` without relying on the internal mock mutex. This allows tests to query and reset the count directly in a thread-safe way. Update `queueSeedlingsInBatch` to: * replace `require.Eventually` with `wait.NoError`, so failures log the actual state for easier debugging, * relax the check from strict equality to `>=` expected, tolerating extra key derivations and reducing flakiness.
1 parent 76af068 commit e28ce8d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

tapgarden/mock.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ type MockKeyRing struct {
857857
KeyIndex uint32
858858

859859
Keys map[keychain.KeyLocator]*btcec.PrivateKey
860+
861+
// deriveNextKeyCallCount is used to track the number of calls to
862+
// DeriveNextKey.
863+
deriveNextKeyCallCount atomic.Uint64
860864
}
861865

862866
var _ KeyRing = (*MockKeyRing)(nil)
@@ -899,6 +903,7 @@ func (m *MockKeyRing) DeriveNextKey(ctx context.Context,
899903
}()
900904

901905
m.Called(ctx, keyFam)
906+
m.deriveNextKeyCallCount.Add(1)
902907

903908
select {
904909
case <-ctx.Done():
@@ -1007,6 +1012,19 @@ func (m *MockKeyRing) DeriveSharedKey(_ context.Context, key *btcec.PublicKey,
10071012
return ecdh.ECDH(key)
10081013
}
10091014

1015+
// DeriveNextKeyCallCount returns the number of calls to DeriveNextKey. This is
1016+
// useful in tests to assert that the key ring was used as expected in
1017+
// concurrent scenarios.
1018+
func (m *MockKeyRing) DeriveNextKeyCallCount() int {
1019+
return int(m.deriveNextKeyCallCount.Load())
1020+
}
1021+
1022+
// ResetDeriveNextKeyCallCount resets the call counter for DeriveNextKey to
1023+
// zero. This is useful in tests to ensure a clean state for assertions.
1024+
func (m *MockKeyRing) ResetDeriveNextKeyCallCount() {
1025+
m.deriveNextKeyCallCount.Store(0)
1026+
}
1027+
10101028
type MockGenSigner struct {
10111029
KeyRing *MockKeyRing
10121030
failSigning atomic.Bool

tapgarden/planter_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ func (t *mintingTestHarness) queueSeedlingsInBatch(isFunded bool,
272272

273273
for i, seedling := range seedlings {
274274
seedling := seedling
275+
276+
t.keyRing.ResetDeriveNextKeyCallCount()
275277
keyCount := 0
276-
t.keyRing.Calls = nil
277278

278279
// For the first seedling sent, we should get a new request,
279280
// representing the batch internal key.
@@ -310,18 +311,21 @@ func (t *mintingTestHarness) queueSeedlingsInBatch(isFunded bool,
310311
// The received update should be a state of MintingStateSeed.
311312
require.Equal(t, tapgarden.MintingStateSeed, update.NewState)
312313

313-
require.Eventually(t, func() bool {
314+
err = wait.NoError(func() error {
314315
// Assert that the key ring method DeriveNextKey was
315316
// called the expected number of times.
316-
count := 0
317-
for _, call := range t.keyRing.Calls {
318-
if call.Method == "DeriveNextKey" {
319-
count++
320-
}
317+
expectedCount := keyCount
318+
actualCount := t.keyRing.DeriveNextKeyCallCount()
319+
320+
if actualCount < expectedCount {
321+
return fmt.Errorf("expected %d calls to key "+
322+
"derivation, got %d", expectedCount,
323+
actualCount)
321324
}
322325

323-
return count == keyCount
324-
}, defaultTimeout, wait.PollInterval)
326+
return nil
327+
}, defaultTimeout)
328+
require.NoError(t, err)
325329
}
326330
}
327331

0 commit comments

Comments
 (0)