2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-04 06:00:21 +00:00
loop/loopin_testcontext_test.go

97 lines
2.5 KiB
Go
Raw Normal View History

2019-03-12 15:10:37 +00:00
package loop
import (
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/lndclient"
2019-03-12 15:10:37 +00:00
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/test"
invpkg "github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
2019-03-12 15:10:37 +00:00
)
type loopInTestContext struct {
t *testing.T
lnd *test.LndMockServices
server *serverMock
2024-01-18 11:26:07 +00:00
store *loopdb.StoreMock
2019-03-12 15:10:37 +00:00
sweeper *sweep.Sweeper
cfg *executeConfig
statusChan chan SwapInfo
2023-11-14 18:05:59 +00:00
errChan chan error
2019-03-12 15:10:37 +00:00
blockEpochChan chan interface{}
swapInvoiceSubscription *test.SingleInvoiceSubscription
2019-03-12 15:10:37 +00:00
}
func newLoopInTestContext(t *testing.T) *loopInTestContext {
lnd := test.NewMockLnd()
2020-05-29 09:27:47 +00:00
server := newServerMock(lnd)
2024-01-18 11:26:07 +00:00
store := loopdb.NewStoreMock(t)
2019-03-12 15:10:37 +00:00
sweeper := sweep.Sweeper{Lnd: &lnd.LndServices}
blockEpochChan := make(chan interface{})
statusChan := make(chan SwapInfo)
2023-11-14 18:05:59 +00:00
errChan := make(chan error)
2019-03-12 15:10:37 +00:00
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,
2019-03-12 15:10:37 +00:00
}
return &loopInTestContext{
t: t,
lnd: lnd,
server: server,
store: store,
sweeper: &sweeper,
cfg: &cfg,
statusChan: statusChan,
2023-11-14 18:05:59 +00:00
errChan: errChan,
2019-03-12 15:10:37 +00:00
blockEpochChan: blockEpochChan,
}
}
func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
state := <-c.statusChan
require.Equal(c.t, expectedState, state.State)
2019-03-12 15:10:37 +00:00
}
// 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 invpkg.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 == invpkg.ContractCanceled ||
state == invpkg.ContractSettled {
close(c.swapInvoiceSubscription.Update)
close(c.swapInvoiceSubscription.Err)
}
}