forked from lightninglabs/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopin_testcontext_test.go
96 lines (81 loc) · 2.52 KB
/
loopin_testcontext_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package loop
import (
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
)
type loopInTestContext struct {
t *testing.T
lnd *test.LndMockServices
server *serverMock
store *storeMock
sweeper *sweep.Sweeper
cfg *executeConfig
statusChan chan SwapInfo
blockEpochChan chan interface{}
swapInvoiceSubscription *test.SingleInvoiceSubscription
}
func newLoopInTestContext(t *testing.T) *loopInTestContext {
lnd := test.NewMockLnd()
server := newServerMock(lnd)
store := newStoreMock(t)
sweeper := sweep.Sweeper{Lnd: &lnd.LndServices}
blockEpochChan := make(chan interface{})
statusChan := make(chan SwapInfo)
expiryChan := make(chan time.Time)
timerFactory := func(expiry time.Duration) <-chan time.Time {
return expiryChan
}
cfg := executeConfig{
statusChan: statusChan,
sweeper: &sweeper,
blockEpochChan: blockEpochChan,
timerFactory: timerFactory,
cancelSwap: server.CancelLoopOutSwap,
}
return &loopInTestContext{
t: t,
lnd: lnd,
server: server,
store: store,
sweeper: &sweeper,
cfg: &cfg,
statusChan: statusChan,
blockEpochChan: blockEpochChan,
}
}
func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
state := <-c.statusChan
if state.State != expectedState {
c.t.Fatalf("expected state %v but got %v", expectedState,
state.State)
}
}
// assertSubscribeInvoice asserts that the client subscribes to invoice updates
// for our swap invoice.
func (c *loopInTestContext) assertSubscribeInvoice(hash lntypes.Hash) {
c.swapInvoiceSubscription = <-c.lnd.SingleInvoiceSubcribeChannel
require.Equal(c.t, hash, c.swapInvoiceSubscription.Hash)
}
// updateInvoiceState mocks an update to our swap invoice state.
func (c *loopInTestContext) updateInvoiceState(amount btcutil.Amount,
state channeldb.ContractState) {
c.swapInvoiceSubscription.Update <- lndclient.InvoiceUpdate{
AmtPaid: amount,
State: state,
}
// If we're in a final state, close our update channels as lndclient
// would.
if state == channeldb.ContractCanceled ||
state == channeldb.ContractSettled {
close(c.swapInvoiceSubscription.Update)
close(c.swapInvoiceSubscription.Err)
}
}