Skip to content

Commit cae72b5

Browse files
committed
loopin/test: move invoice updates to loopin test context
1 parent 9d3d9ce commit cae72b5

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

loopin_test.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/lightninglabs/lndclient"
87
"github.com/lightninglabs/loop/loopdb"
98
"github.com/lightninglabs/loop/swap"
109
"github.com/lightninglabs/loop/test"
@@ -89,17 +88,11 @@ func TestLoopInSuccess(t *testing.T) {
8988
<-ctx.lnd.RegisterSpendChannel
9089

9190
// Client starts listening for swap invoice updates.
92-
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel
93-
if subscription.Hash != ctx.server.swapHash {
94-
t.Fatal("client subscribing to wrong invoice")
95-
}
91+
ctx.assertSubscribeInvoice(ctx.server.swapHash)
9692

9793
// Server has already paid invoice before spending the htlc. Signal
9894
// settled.
99-
subscription.Update <- lndclient.InvoiceUpdate{
100-
State: channeldb.ContractSettled,
101-
AmtPaid: 49000,
102-
}
95+
ctx.updateInvoiceState(49000, channeldb.ContractSettled)
10396

10497
// Swap is expected to move to the state InvoiceSettled
10598
ctx.assertState(loopdb.StateInvoiceSettled)
@@ -257,10 +250,7 @@ func testLoopInTimeout(t *testing.T,
257250
<-ctx.lnd.RegisterSpendChannel
258251

259252
// Client starts listening for swap invoice updates.
260-
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel
261-
if subscription.Hash != ctx.server.swapHash {
262-
t.Fatal("client subscribing to wrong invoice")
263-
}
253+
ctx.assertSubscribeInvoice(ctx.server.swapHash)
264254

265255
// Let htlc expire.
266256
ctx.blockEpochChan <- s.LoopInContract.CltvExpiry
@@ -294,10 +284,8 @@ func testLoopInTimeout(t *testing.T,
294284
// safely cancel the swap invoice.
295285
<-ctx.lnd.FailInvoiceChannel
296286

297-
// Signal the the invoice was canceled.
298-
subscription.Update <- lndclient.InvoiceUpdate{
299-
State: channeldb.ContractCanceled,
300-
}
287+
// Signal that the invoice was canceled.
288+
ctx.updateInvoiceState(0, channeldb.ContractCanceled)
301289

302290
ctx.assertState(loopdb.StateFailTimeout)
303291
state := ctx.store.assertLoopInState(loopdb.StateFailTimeout)
@@ -501,18 +489,12 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
501489
<-ctx.lnd.RegisterSpendChannel
502490

503491
// Client starts listening for swap invoice updates.
504-
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel
505-
if subscription.Hash != testPreimage.Hash() {
506-
t.Fatal("client subscribing to wrong invoice")
507-
}
492+
ctx.assertSubscribeInvoice(testPreimage.Hash())
508493

509494
// Server has already paid invoice before spending the htlc. Signal
510495
// settled.
511-
invoiceUpdate := lndclient.InvoiceUpdate{
512-
State: channeldb.ContractSettled,
513-
AmtPaid: 49000,
514-
}
515-
subscription.Update <- invoiceUpdate
496+
amtPaid := btcutil.Amount(49000)
497+
ctx.updateInvoiceState(amtPaid, channeldb.ContractSettled)
516498

517499
// Swap is expected to move to the state InvoiceSettled
518500
ctx.assertState(loopdb.StateInvoiceSettled)
@@ -537,7 +519,6 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
537519
// We expect our server fee to reflect as the difference between htlc
538520
// value and invoice amount paid. We use our original on-chain cost, set
539521
// earlier in the test, because we expect this value to be unchanged.
540-
cost.Server = btcutil.Amount(htlcTx.TxOut[0].Value) -
541-
invoiceUpdate.AmtPaid
522+
cost.Server = btcutil.Amount(htlcTx.TxOut[0].Value) - amtPaid
542523
require.Equal(t, cost, finalState.Cost)
543524
}

loopin_testcontext_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/btcsuite/btcutil"
8+
"github.com/lightninglabs/lndclient"
79
"github.com/lightninglabs/loop/loopdb"
810
"github.com/lightninglabs/loop/sweep"
911
"github.com/lightninglabs/loop/test"
12+
"github.com/lightningnetwork/lnd/channeldb"
13+
"github.com/lightningnetwork/lnd/lntypes"
14+
"github.com/stretchr/testify/require"
1015
)
1116

1217
type loopInTestContext struct {
@@ -18,6 +23,8 @@ type loopInTestContext struct {
1823
cfg *executeConfig
1924
statusChan chan SwapInfo
2025
blockEpochChan chan interface{}
26+
27+
swapInvoiceSubscription *test.SingleInvoiceSubscription
2128
}
2229

2330
func newLoopInTestContext(t *testing.T) *loopInTestContext {
@@ -61,3 +68,20 @@ func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
6168
state.State)
6269
}
6370
}
71+
72+
// assertSubscribeInvoice asserts that the client subscribes to invoice updates
73+
// for our swap invoice.
74+
func (c *loopInTestContext) assertSubscribeInvoice(hash lntypes.Hash) {
75+
c.swapInvoiceSubscription = <-c.lnd.SingleInvoiceSubcribeChannel
76+
require.Equal(c.t, hash, c.swapInvoiceSubscription.Hash)
77+
}
78+
79+
// updateInvoiceState mocks an update to our swap invoice state.
80+
func (c *loopInTestContext) updateInvoiceState(amount btcutil.Amount,
81+
state channeldb.ContractState) {
82+
83+
c.swapInvoiceSubscription.Update <- lndclient.InvoiceUpdate{
84+
AmtPaid: amount,
85+
State: state,
86+
}
87+
}

0 commit comments

Comments
 (0)