Skip to content

Commit d18c3d4

Browse files
committed
sweepbatcher: test that empty batches won't prevent startup
1 parent 67fe54b commit d18c3d4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

sweepbatcher/sweep_batcher.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ type Batcher struct {
153153
// quit signals that the batch must stop.
154154
quit chan struct{}
155155

156+
// initDone is a channel that is closed when the batcher has been
157+
// initialized.
158+
initDone chan struct{}
159+
156160
// wallet is the wallet kit client that is used by batches.
157161
wallet lndclient.WalletKitClient
158162

@@ -200,6 +204,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
200204
sweepReqs: make(chan SweepRequest),
201205
errChan: make(chan error, 1),
202206
quit: make(chan struct{}),
207+
initDone: make(chan struct{}),
203208
wallet: wallet,
204209
chainNotifier: chainNotifier,
205210
signerClient: signerClient,
@@ -239,6 +244,9 @@ func (b *Batcher) Run(ctx context.Context) error {
239244
}
240245
}
241246

247+
// Signal that the batcher has been initialized.
248+
close(b.initDone)
249+
242250
for {
243251
select {
244252
case sweepReq := <-b.sweepReqs:

sweepbatcher/sweep_batcher_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sweepbatcher
33
import (
44
"context"
55
"strings"
6+
"sync"
67
"testing"
78
"time"
89

@@ -1029,3 +1030,36 @@ func TestGetFeePortionForSweep(t *testing.T) {
10291030
})
10301031
}
10311032
}
1033+
1034+
func TestRestoringEmptyBatch(t *testing.T) {
1035+
defer test.Guard(t)()
1036+
1037+
lnd := test.NewMockLnd()
1038+
ctx, cancel := context.WithCancel(context.Background())
1039+
1040+
store := loopdb.NewStoreMock(t)
1041+
1042+
batcherStore := NewStoreMock()
1043+
batcherStore.InsertSweepBatch(ctx, &dbBatch{})
1044+
1045+
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
1046+
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
1047+
1048+
var wg sync.WaitGroup
1049+
wg.Add(1)
1050+
1051+
var err error
1052+
go func() {
1053+
defer wg.Done()
1054+
err = batcher.Run(ctx)
1055+
}()
1056+
1057+
// Wait for the batcher to be initialized.
1058+
<-batcher.initDone
1059+
1060+
// Now make it quit by canceling the context.
1061+
cancel()
1062+
wg.Wait()
1063+
1064+
require.Error(t, context.Canceled, err)
1065+
}

0 commit comments

Comments
 (0)