utxonursery: add mock sweeper

This commit adds a simple mock of the sweeper to be able to test
dependents of sweeper in isolation.
This commit is contained in:
Joost Jager 2018-12-19 14:09:30 +01:00
parent 74e9852e3d
commit e29b0f8894
No known key found for this signature in database
GPG Key ID: AE6B0D042C8E38D9

View File

@ -12,6 +12,7 @@ import (
"os"
"reflect"
"runtime/pprof"
"sync"
"testing"
"time"
@ -1100,3 +1101,67 @@ func (m *nurseryMockSigner) ComputeInputScript(tx *wire.MsgTx,
return &lnwallet.InputScript{}, nil
}
type mockSweeper struct {
lock sync.Mutex
resultChans map[wire.OutPoint]chan sweep.Result
t *testing.T
sweepChan chan sweep.Input
}
func newMockSweeper(t *testing.T) *mockSweeper {
return &mockSweeper{
resultChans: make(map[wire.OutPoint]chan sweep.Result),
sweepChan: make(chan sweep.Input, 1),
t: t,
}
}
func (s *mockSweeper) sweepInput(input sweep.Input) (chan sweep.Result, error) {
utxnLog.Debugf("mockSweeper sweepInput called for %v", *input.OutPoint())
select {
case s.sweepChan <- input:
case <-time.After(defaultTestTimeout):
s.t.Fatal("signal result timeout")
}
s.lock.Lock()
defer s.lock.Unlock()
c := make(chan sweep.Result, 1)
s.resultChans[*input.OutPoint()] = c
return c, nil
}
func (s *mockSweeper) expectSweep() {
s.t.Helper()
select {
case <-s.sweepChan:
case <-time.After(defaultTestTimeout):
s.t.Fatal("signal result timeout")
}
}
func (s *mockSweeper) sweepAll() {
s.t.Helper()
s.lock.Lock()
currentChans := s.resultChans
s.resultChans = make(map[wire.OutPoint]chan sweep.Result)
s.lock.Unlock()
for o, c := range currentChans {
utxnLog.Debugf("mockSweeper signal swept for %v", o)
select {
case c <- sweep.Result{}:
case <-time.After(defaultTestTimeout):
s.t.Fatal("signal result timeout")
}
}
}