2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-09 19:10:47 +00:00
loop/loopout_test.go

102 lines
2.1 KiB
Go
Raw Normal View History

package loop
2019-03-06 20:13:50 +00:00
import (
"context"
"errors"
"testing"
"time"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/test"
2019-03-06 20:13:50 +00:00
)
// TestLateHtlcPublish tests that the client is not revealing the preimage if
// there are not enough blocks left.
func TestLateHtlcPublish(t *testing.T) {
defer test.Guard(t)()
lnd := test.NewMockLnd()
ctx := test.NewContext(t, lnd)
server := newServerMock()
store := newStoreMock(t)
expiryChan := make(chan time.Time)
timerFactory := func(expiry time.Duration) <-chan time.Time {
return expiryChan
}
height := int32(600)
cfg := &swapConfig{
lnd: &lnd.LndServices,
store: store,
server: server,
}
swap, err := newLoopOutSwap(
2019-03-06 20:13:50 +00:00
context.Background(), cfg, height, testRequest,
)
if err != nil {
t.Fatal(err)
}
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
blockEpochChan := make(chan interface{})
statusChan := make(chan SwapInfo)
errChan := make(chan error)
go func() {
err := swap.execute(context.Background(), &executeConfig{
statusChan: statusChan,
sweeper: sweeper,
blockEpochChan: blockEpochChan,
timerFactory: timerFactory,
}, height)
if err != nil {
logger.Error(err)
}
errChan <- err
}()
store.assertLoopOutStored()
2019-03-06 20:13:50 +00:00
state := <-statusChan
if state.State != loopdb.StateInitiated {
2019-03-06 20:13:50 +00:00
t.Fatal("unexpected state")
}
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
// Expect client to register for conf
ctx.AssertRegisterConf()
// // Wait too long before publishing htlc.
blockEpochChan <- int32(swap.CltvExpiry - 10)
signalSwapPaymentResult(
errors.New(lndclient.PaymentResultUnknownPaymentHash),
)
signalPrepaymentResult(
errors.New(lndclient.PaymentResultUnknownPaymentHash),
)
store.assertStoreFinished(loopdb.StateFailTimeout)
2019-03-06 20:13:50 +00:00
status := <-statusChan
if status.State != loopdb.StateFailTimeout {
2019-03-06 20:13:50 +00:00
t.Fatal("unexpected state")
}
err = <-errChan
if err != nil {
t.Fatal(err)
}
}