Skip to content

Commit bdf1858

Browse files
committed
unit: update swap tests for injected clocks
1 parent 5238bb4 commit bdf1858

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

cost_migration_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lightninglabs/lndclient"
1010
"github.com/lightninglabs/loop/loopdb"
1111
"github.com/lightninglabs/loop/test"
12+
"github.com/lightningnetwork/lnd/clock"
1213
"github.com/lightningnetwork/lnd/lntypes"
1314
"github.com/lightningnetwork/lnd/lnwire"
1415
"github.com/stretchr/testify/require"
@@ -25,6 +26,7 @@ func TestCalculateLoopOutCost(t *testing.T) {
2526
lnd: &lnd.LndServices,
2627
store: store,
2728
server: server,
29+
clock: clock.NewTestClock(time.Unix(123, 0)),
2830
}
2931

3032
height := int32(600)
@@ -118,6 +120,7 @@ func TestCostMigration(t *testing.T) {
118120
lnd: &lnd.LndServices,
119121
store: store,
120122
server: server,
123+
clock: clock.NewTestClock(time.Unix(123, 0)),
121124
}
122125

123126
height := int32(600)

loopin_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"fmt"
66
"testing"
7+
"time"
78

89
"github.com/btcsuite/btcd/btcutil"
910
"github.com/btcsuite/btcd/wire"
1011
"github.com/lightninglabs/loop/loopdb"
1112
"github.com/lightninglabs/loop/test"
1213
"github.com/lightninglabs/loop/utils"
1314
"github.com/lightningnetwork/lnd/chainntnfs"
15+
"github.com/lightningnetwork/lnd/clock"
1416
invpkg "github.com/lightningnetwork/lnd/invoices"
1517
"github.com/lightningnetwork/lnd/routing/route"
1618
"github.com/stretchr/testify/require"
@@ -47,7 +49,10 @@ func testLoopInSuccess(t *testing.T) {
4749

4850
height := int32(600)
4951

50-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
52+
cfg := newSwapConfig(
53+
&ctx.lnd.LndServices, ctx.store, ctx.server, nil,
54+
clock.NewTestClock(time.Unix(123, 0)),
55+
)
5156

5257
expectedLastHop := &route.Vertex{0x02}
5358

@@ -196,7 +201,10 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
196201

197202
height := int32(600)
198203

199-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
204+
cfg := newSwapConfig(
205+
&ctx.lnd.LndServices, ctx.store, ctx.server, nil,
206+
clock.NewTestClock(time.Unix(123, 0)),
207+
)
200208

201209
req := testLoopInRequest
202210
if externalValue != 0 {
@@ -407,7 +415,10 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
407415
ctxb := context.Background()
408416

409417
ctx := newLoopInTestContext(t)
410-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
418+
cfg := newSwapConfig(
419+
&ctx.lnd.LndServices, ctx.store, ctx.server, nil,
420+
clock.NewTestClock(time.Unix(123, 0)),
421+
)
411422

412423
// Create sender and receiver keys.
413424
_, senderPubKey := test.CreateKey(1)
@@ -762,7 +773,10 @@ func advanceToPublishedHtlc(t *testing.T, ctx *loopInTestContext) SwapInfo {
762773
func startNewLoopIn(t *testing.T, ctx *loopInTestContext, height int32) (
763774
*swapConfig, *loopInSwap, error) {
764775

765-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
776+
cfg := newSwapConfig(
777+
&ctx.lnd.LndServices, ctx.store, ctx.server, nil,
778+
clock.NewTestClock(time.Unix(123, 0)),
779+
)
766780

767781
req := &testLoopInRequest
768782

loopout_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/lightninglabs/loop/sweep"
1919
"github.com/lightninglabs/loop/sweepbatcher"
2020
"github.com/lightninglabs/loop/test"
21+
"github.com/lightningnetwork/lnd/clock"
2122
"github.com/lightningnetwork/lnd/lnrpc"
2223
"github.com/lightningnetwork/lnd/lntypes"
2324
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -57,11 +58,13 @@ func testLoopOutPaymentParameters(t *testing.T) {
5758
}
5859

5960
height := int32(600)
61+
startTime := time.Unix(123, 0)
6062

6163
cfg := &swapConfig{
6264
lnd: &lnd.LndServices,
6365
store: store,
6466
server: server,
67+
clock: clock.NewTestClock(startTime),
6568
}
6669

6770
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
@@ -82,6 +85,7 @@ func testLoopOutPaymentParameters(t *testing.T) {
8285
)
8386
require.NoError(t, err)
8487
swap := initResult.swap
88+
require.Equal(t, startTime, swap.InitiationTime)
8589

8690
// Execute the swap in its own goroutine.
8791
errChan := make(chan error)
@@ -193,7 +197,10 @@ func testLateHtlcPublish(t *testing.T) {
193197

194198
height := int32(600)
195199

196-
cfg := newSwapConfig(&lnd.LndServices, store, server, nil)
200+
cfg := newSwapConfig(
201+
&lnd.LndServices, store, server, nil,
202+
clock.NewTestClock(time.Unix(123, 0)),
203+
)
197204

198205
testRequest.Expiry = height + testLoopOutMinOnChainCltvDelta
199206

@@ -296,6 +303,7 @@ func testCustomSweepConfTarget(t *testing.T) {
296303

297304
cfg := newSwapConfig(
298305
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
306+
clock.NewTestClock(time.Unix(123, 0)),
299307
)
300308

301309
initResult, err := newLoopOutSwap(
@@ -536,6 +544,7 @@ func testPreimagePush(t *testing.T) {
536544

537545
cfg := newSwapConfig(
538546
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
547+
clock.NewTestClock(time.Unix(123, 0)),
539548
)
540549

541550
initResult, err := newLoopOutSwap(
@@ -794,6 +803,7 @@ func testFailedOffChainCancelation(t *testing.T) {
794803

795804
cfg := newSwapConfig(
796805
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
806+
clock.NewTestClock(time.Unix(123, 0)),
797807
)
798808

799809
initResult, err := newLoopOutSwap(
@@ -948,6 +958,7 @@ func TestLoopOutMuSig2Sweep(t *testing.T) {
948958

949959
cfg := newSwapConfig(
950960
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
961+
clock.NewTestClock(time.Unix(123, 0)),
951962
)
952963

953964
initResult, err := newLoopOutSwap(

0 commit comments

Comments
 (0)