From e29b0f889432b85f96ce6e7aa54874b480a9f9e0 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 19 Dec 2018 14:09:30 +0100 Subject: [PATCH] utxonursery: add mock sweeper This commit adds a simple mock of the sweeper to be able to test dependents of sweeper in isolation. --- utxonursery_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/utxonursery_test.go b/utxonursery_test.go index 577d1c44..a6eef7be 100644 --- a/utxonursery_test.go +++ b/utxonursery_test.go @@ -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") + } + } +}