Skip to content

Commit 026890a

Browse files
committed
sweepbatcher/test: protect mock data with mutex
Several structures were accessed without protection causing crashes under -race.
1 parent ab59bda commit 026890a

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

sweepbatcher/sweep_batcher_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,18 +811,26 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
811811
type wrappedLogger struct {
812812
btclog.Logger
813813

814+
mu sync.Mutex
815+
814816
debugMessages []string
815817
infoMessages []string
816818
}
817819

818820
// Debugf logs debug message.
819821
func (l *wrappedLogger) Debugf(format string, params ...interface{}) {
822+
l.mu.Lock()
823+
defer l.mu.Unlock()
824+
820825
l.debugMessages = append(l.debugMessages, format)
821826
l.Logger.Debugf(format, params...)
822827
}
823828

824829
// Infof logs info message.
825830
func (l *wrappedLogger) Infof(format string, params ...interface{}) {
831+
l.mu.Lock()
832+
defer l.mu.Unlock()
833+
826834
l.infoMessages = append(l.infoMessages, format)
827835
l.Logger.Infof(format, params...)
828836
}
@@ -950,6 +958,9 @@ func testDelays(t *testing.T, store testStore, batcherStore testBatcherStore) {
950958
// Wait for batch publishing to be skipped, because initialDelay has not
951959
// ended.
952960
require.EventuallyWithT(t, func(c *assert.CollectT) {
961+
testLogger.mu.Lock()
962+
defer testLogger.mu.Unlock()
963+
953964
assert.Contains(c, testLogger.debugMessages, stillWaitingMsg)
954965
}, test.Timeout, eventuallyCheckFrequency)
955966

@@ -1274,6 +1285,9 @@ func testDelays(t *testing.T, store testStore, batcherStore testBatcherStore) {
12741285

12751286
// Wait for sweep to be added to the batch.
12761287
require.EventuallyWithT(t, func(c *assert.CollectT) {
1288+
testLogger2.mu.Lock()
1289+
defer testLogger2.mu.Unlock()
1290+
12771291
assert.Contains(c, testLogger2.infoMessages, "adding sweep %x")
12781292
}, test.Timeout, eventuallyCheckFrequency)
12791293

@@ -2810,11 +2824,22 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
28102824

28112825
type sweepFetcherMock struct {
28122826
store map[lntypes.Hash]*SweepInfo
2827+
mu sync.Mutex
2828+
}
2829+
2830+
func (f *sweepFetcherMock) setSweep(hash lntypes.Hash, info *SweepInfo) {
2831+
f.mu.Lock()
2832+
defer f.mu.Unlock()
2833+
2834+
f.store[hash] = info
28132835
}
28142836

28152837
func (f *sweepFetcherMock) FetchSweep(ctx context.Context, hash lntypes.Hash) (
28162838
*SweepInfo, error) {
28172839

2840+
f.mu.Lock()
2841+
defer f.mu.Unlock()
2842+
28182843
return f.store[hash], nil
28192844
}
28202845

@@ -3279,7 +3304,7 @@ func testWithMixedBatch(t *testing.T, store testStore,
32793304
if i == 0 {
32803305
sweepInfo.NonCoopHint = true
32813306
}
3282-
sweepFetcher.store[swapHash] = sweepInfo
3307+
sweepFetcher.setSweep(swapHash, sweepInfo)
32833308

32843309
// Create sweep request.
32853310
sweepReq := SweepRequest{
@@ -3433,7 +3458,7 @@ func testWithMixedBatchCustom(t *testing.T, store testStore,
34333458
)
34343459
require.NoError(t, err)
34353460

3436-
sweepFetcher.store[swapHash] = &SweepInfo{
3461+
sweepFetcher.setSweep(swapHash, &SweepInfo{
34373462
Preimage: preimages[i],
34383463
NonCoopHint: nonCoopHints[i],
34393464

@@ -3445,7 +3470,7 @@ func testWithMixedBatchCustom(t *testing.T, store testStore,
34453470
HTLC: *htlc,
34463471
HTLCSuccessEstimator: htlc.AddSuccessToEstimator,
34473472
DestAddr: destAddr,
3448-
}
3473+
})
34493474

34503475
// Create sweep request.
34513476
sweepReq := SweepRequest{
@@ -4035,13 +4060,18 @@ type loopdbBatcherStore struct {
40354060
BatcherStore
40364061

40374062
sweepsSet map[lntypes.Hash]struct{}
4063+
4064+
mu sync.Mutex
40384065
}
40394066

40404067
// UpsertSweep inserts a sweep into the database, or updates an existing sweep
40414068
// if it already exists. This wrapper was added to update sweepsSet.
40424069
func (s *loopdbBatcherStore) UpsertSweep(ctx context.Context,
40434070
sweep *dbSweep) error {
40444071

4072+
s.mu.Lock()
4073+
defer s.mu.Unlock()
4074+
40454075
err := s.BatcherStore.UpsertSweep(ctx, sweep)
40464076
if err == nil {
40474077
s.sweepsSet[sweep.SwapHash] = struct{}{}
@@ -4051,7 +4081,11 @@ func (s *loopdbBatcherStore) UpsertSweep(ctx context.Context,
40514081

40524082
// AssertSweepStored asserts that a sweep is stored.
40534083
func (s *loopdbBatcherStore) AssertSweepStored(id lntypes.Hash) bool {
4084+
s.mu.Lock()
4085+
defer s.mu.Unlock()
4086+
40544087
_, has := s.sweepsSet[id]
4088+
40554089
return has
40564090
}
40574091

0 commit comments

Comments
 (0)