Skip to content

Commit 800f0e0

Browse files
authored
Merge pull request #889 from starius/fix-unit-test-races
sweepbatcher: fix race conditions in unit tests
2 parents e86ccb9 + 60f4906 commit 800f0e0

File tree

8 files changed

+601
-278
lines changed

8 files changed

+601
-278
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020

2121
# If you change this value, please change it in the following files as well:
2222
# /Dockerfile
23-
GO_VERSION: 1.21.10
23+
GO_VERSION: 1.24.0
2424

2525
jobs:
2626
########################

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=${BUILDPLATFORM} golang:1.22-alpine as builder
1+
FROM --platform=${BUILDPLATFORM} golang:1.24-alpine as builder
22

33
# Copy in the local repository to build from.
44
COPY . /go/src/github.com/lightningnetwork/loop

sweepbatcher/greedy_batch_selection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func (b *Batcher) greedyAddSweep(ctx context.Context, sweep *sweep) error {
9292
return nil
9393
}
9494

95-
log.Debugf("Batch selection algorithm returned batch id %d for"+
96-
" sweep %x, but acceptance failed.", batchId,
95+
debugf("Batch selection algorithm returned batch id %d "+
96+
"for sweep %x, but acceptance failed.", batchId,
9797
sweep.swapHash[:6])
9898
}
9999

sweepbatcher/log.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package sweepbatcher
22

33
import (
44
"fmt"
5+
"sync/atomic"
56

67
"github.com/btcsuite/btclog"
78
"github.com/lightningnetwork/lnd/build"
89
)
910

10-
// log is a logger that is initialized with no output filters. This
11+
// log_ is a logger that is initialized with no output filters. This
1112
// means the package will not perform any logging by default until the
1213
// caller requests it.
13-
var log btclog.Logger
14+
var log_ atomic.Pointer[btclog.Logger]
15+
16+
// log returns active logger.
17+
func log() btclog.Logger {
18+
return *log_.Load()
19+
}
1420

1521
// The default amount of logging is none.
1622
func init() {
@@ -20,12 +26,32 @@ func init() {
2026
// batchPrefixLogger returns a logger that prefixes all log messages with
2127
// the ID.
2228
func batchPrefixLogger(batchID string) btclog.Logger {
23-
return build.NewPrefixLog(fmt.Sprintf("[Batch %s]", batchID), log)
29+
return build.NewPrefixLog(fmt.Sprintf("[Batch %s]", batchID), log())
2430
}
2531

2632
// UseLogger uses a specified Logger to output package logging info.
2733
// This should be used in preference to SetLogWriter if the caller is also
2834
// using btclog.
2935
func UseLogger(logger btclog.Logger) {
30-
log = logger
36+
log_.Store(&logger)
37+
}
38+
39+
// debugf logs a message with level DEBUG.
40+
func debugf(format string, params ...interface{}) {
41+
log().Debugf(format, params...)
42+
}
43+
44+
// infof logs a message with level INFO.
45+
func infof(format string, params ...interface{}) {
46+
log().Infof(format, params...)
47+
}
48+
49+
// warnf logs a message with level WARN.
50+
func warnf(format string, params ...interface{}) {
51+
log().Warnf(format, params...)
52+
}
53+
54+
// errorf logs a message with level ERROR.
55+
func errorf(format string, params ...interface{}) {
56+
log().Errorf(format, params...)
3157
}

sweepbatcher/store_mock.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"sort"
7+
"sync"
78

89
"github.com/btcsuite/btcd/btcutil"
910
"github.com/lightningnetwork/lnd/lntypes"
@@ -13,6 +14,7 @@ import (
1314
type StoreMock struct {
1415
batches map[int32]dbBatch
1516
sweeps map[lntypes.Hash]dbSweep
17+
mu sync.Mutex
1618
}
1719

1820
// NewStoreMock instantiates a new mock store.
@@ -28,6 +30,9 @@ func NewStoreMock() *StoreMock {
2830
func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
2931
[]*dbBatch, error) {
3032

33+
s.mu.Lock()
34+
defer s.mu.Unlock()
35+
3136
result := []*dbBatch{}
3237
for _, batch := range s.batches {
3338
batch := batch
@@ -44,6 +49,9 @@ func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
4449
func (s *StoreMock) InsertSweepBatch(ctx context.Context,
4550
batch *dbBatch) (int32, error) {
4651

52+
s.mu.Lock()
53+
defer s.mu.Unlock()
54+
4755
var id int32
4856

4957
if len(s.batches) == 0 {
@@ -66,12 +74,18 @@ func (s *StoreMock) DropBatch(ctx context.Context, id int32) error {
6674
func (s *StoreMock) UpdateSweepBatch(ctx context.Context,
6775
batch *dbBatch) error {
6876

77+
s.mu.Lock()
78+
defer s.mu.Unlock()
79+
6980
s.batches[batch.ID] = *batch
7081
return nil
7182
}
7283

7384
// ConfirmBatch confirms a batch.
7485
func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
86+
s.mu.Lock()
87+
defer s.mu.Unlock()
88+
7589
batch, ok := s.batches[id]
7690
if !ok {
7791
return errors.New("batch not found")
@@ -87,6 +101,9 @@ func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
87101
func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
88102
id int32) ([]*dbSweep, error) {
89103

104+
s.mu.Lock()
105+
defer s.mu.Unlock()
106+
90107
result := []*dbSweep{}
91108
for _, sweep := range s.sweeps {
92109
sweep := sweep
@@ -104,14 +121,21 @@ func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
104121

105122
// UpsertSweep inserts a sweep into the database, or updates an existing sweep.
106123
func (s *StoreMock) UpsertSweep(ctx context.Context, sweep *dbSweep) error {
124+
s.mu.Lock()
125+
defer s.mu.Unlock()
126+
107127
s.sweeps[sweep.SwapHash] = *sweep
128+
108129
return nil
109130
}
110131

111132
// GetSweepStatus returns the status of a sweep.
112133
func (s *StoreMock) GetSweepStatus(ctx context.Context,
113134
swapHash lntypes.Hash) (bool, error) {
114135

136+
s.mu.Lock()
137+
defer s.mu.Unlock()
138+
115139
sweep, ok := s.sweeps[swapHash]
116140
if !ok {
117141
return false, nil
@@ -127,6 +151,9 @@ func (s *StoreMock) Close() error {
127151

128152
// AssertSweepStored asserts that a sweep is stored.
129153
func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
154+
s.mu.Lock()
155+
defer s.mu.Unlock()
156+
130157
_, ok := s.sweeps[id]
131158
return ok
132159
}
@@ -135,6 +162,9 @@ func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
135162
func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
136163
*dbBatch, error) {
137164

165+
s.mu.Lock()
166+
defer s.mu.Unlock()
167+
138168
for _, sweep := range s.sweeps {
139169
if sweep.SwapHash == swapHash {
140170
batch, ok := s.batches[sweep.BatchID]
@@ -153,6 +183,9 @@ func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
153183
func (s *StoreMock) TotalSweptAmount(ctx context.Context, batchID int32) (
154184
btcutil.Amount, error) {
155185

186+
s.mu.Lock()
187+
defer s.mu.Unlock()
188+
156189
batch, ok := s.batches[batchID]
157190
if !ok {
158191
return 0, errors.New("batch not found")

0 commit comments

Comments
 (0)