@@ -2,7 +2,9 @@ package liquidity
2
2
3
3
import (
4
4
"context"
5
+ "reflect"
5
6
"testing"
7
+ "time"
6
8
7
9
"github.com/btcsuite/btcd/btcutil"
8
10
"github.com/lightninglabs/lndclient"
@@ -11,8 +13,10 @@ import (
11
13
"github.com/lightninglabs/loop/swap"
12
14
"github.com/lightninglabs/loop/test"
13
15
"github.com/lightningnetwork/lnd/clock"
16
+ "github.com/lightningnetwork/lnd/lntypes"
14
17
"github.com/lightningnetwork/lnd/ticker"
15
18
"github.com/stretchr/testify/assert"
19
+ "github.com/stretchr/testify/require"
16
20
)
17
21
18
22
type autoloopTestCtx struct {
@@ -45,9 +49,17 @@ type autoloopTestCtx struct {
45
49
// loopOuts is a channel that we get existing loop out swaps on.
46
50
loopOuts chan []* loopdb.LoopOut
47
51
52
+ // loopOutSingle is the single loop out returned from fetching a single
53
+ // swap from store.
54
+ loopOutSingle * loopdb.LoopOut
55
+
48
56
// loopIns is a channel that we get existing loop in swaps on.
49
57
loopIns chan []* loopdb.LoopIn
50
58
59
+ // loopInSingle is the single loop in returned from fetching a single
60
+ // swap from store.
61
+ loopInSingle * loopdb.LoopIn
62
+
51
63
// restrictions is a channel that we get swap restrictions on.
52
64
restrictions chan * Restrictions
53
65
@@ -131,6 +143,9 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
131
143
ListLoopOut : func () ([]* loopdb.LoopOut , error ) {
132
144
return <- testCtx .loopOuts , nil
133
145
},
146
+ GetLoopOut : func (hash lntypes.Hash ) (* loopdb.LoopOut , error ) {
147
+ return testCtx .loopOutSingle , nil
148
+ },
134
149
ListLoopIn : func () ([]* loopdb.LoopIn , error ) {
135
150
return <- testCtx .loopIns , nil
136
151
},
@@ -188,6 +203,10 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
188
203
testCtx .manager = NewManager (cfg )
189
204
err := testCtx .manager .setParameters (context .Background (), parameters )
190
205
assert .NoError (t , err )
206
+ // Override the payments check interval for the tests in order to not
207
+ // timeout.
208
+ testCtx .manager .params .CustomPaymentCheckInterval =
209
+ 150 * time .Millisecond
191
210
<- done
192
211
return testCtx
193
212
}
@@ -241,14 +260,17 @@ type loopInRequestResp struct {
241
260
// autoloopStep contains all of the information to required to step
242
261
// through an autoloop tick.
243
262
type autoloopStep struct {
244
- minAmt btcutil.Amount
245
- maxAmt btcutil.Amount
246
- existingOut []* loopdb.LoopOut
247
- existingIn []* loopdb.LoopIn
248
- quotesOut []quoteRequestResp
249
- quotesIn []quoteInRequestResp
250
- expectedOut []loopOutRequestResp
251
- expectedIn []loopInRequestResp
263
+ minAmt btcutil.Amount
264
+ maxAmt btcutil.Amount
265
+ existingOut []* loopdb.LoopOut
266
+ existingOutSingle * loopdb.LoopOut
267
+ existingIn []* loopdb.LoopIn
268
+ existingInSingle * loopdb.LoopIn
269
+ quotesOut []quoteRequestResp
270
+ quotesIn []quoteInRequestResp
271
+ expectedOut []loopOutRequestResp
272
+ expectedIn []loopInRequestResp
273
+ keepDestAddr bool
252
274
}
253
275
254
276
// autoloop walks our test context through the process of triggering our
@@ -269,6 +291,9 @@ func (c *autoloopTestCtx) autoloop(step *autoloopStep) {
269
291
c .loopOuts <- step .existingOut
270
292
c .loopIns <- step .existingIn
271
293
294
+ c .loopOutSingle = step .existingOutSingle
295
+ c .loopInSingle = step .existingInSingle
296
+
272
297
// Assert that we query the server for a quote for each of our
273
298
// recommended swaps. Note that this differs from our set of expected
274
299
// swaps because we may get quotes for suggested swaps but then just
@@ -299,25 +324,77 @@ func (c *autoloopTestCtx) autoloop(step *autoloopStep) {
299
324
c .quotes <- expected .quote
300
325
}
301
326
302
- // Assert that we dispatch the expected set of swaps.
303
- for _ , expected := range step .expectedOut {
327
+ require .True (c .t , c .matchLoopOuts (step .expectedOut , step .keepDestAddr ))
328
+ require .True (c .t , c .matchLoopIns (step .expectedIn ))
329
+ }
330
+
331
+ // matchLoopOuts checks that the actual loop out requests we got match the
332
+ // expected ones. The argument keepDestAddr is used to indicate whether we keep
333
+ // the actual loops destination address for the comparison. This is useful
334
+ // because we don't want to compare the destination address generated by the
335
+ // wallet mock. We want to compare the destination address when testing the
336
+ // autoloop DestAddr parameter for loop outs.
337
+ func (c * autoloopTestCtx ) matchLoopOuts (swaps []loopOutRequestResp ,
338
+ keepDestAddr bool ) bool {
339
+
340
+ swapsCopy := make ([]loopOutRequestResp , len (swaps ))
341
+ copy (swapsCopy , swaps )
342
+
343
+ length := len (swapsCopy )
344
+
345
+ for i := 0 ; i < length ; i ++ {
304
346
actual := <- c .outRequest
305
347
306
- // Set our destination address to nil so that we do not need to
307
- // provide the address that is obtained by the mock wallet kit.
308
- if expected .request .DestAddr == nil {
348
+ if ! keepDestAddr {
309
349
actual .DestAddr = nil
310
350
}
311
351
312
- assert .Equal (c .t , expected .request , actual )
313
- c .loopOut <- expected .response
352
+ inner:
353
+ for index , swap := range swapsCopy {
354
+ equal := reflect .DeepEqual (swap .request , actual )
355
+
356
+ if equal {
357
+ c .loopOut <- swap .response
358
+
359
+ swapsCopy = append (
360
+ swapsCopy [:index ],
361
+ swapsCopy [index + 1 :]... ,
362
+ )
363
+
364
+ break inner
365
+ }
366
+ }
314
367
}
315
368
316
- for _ , expected := range step .expectedIn {
369
+ return len (swapsCopy ) == 0
370
+ }
371
+
372
+ // matchLoopIns checks that the actual loop in requests we got match the
373
+ // expected ones.
374
+ func (c * autoloopTestCtx ) matchLoopIns (
375
+ swaps []loopInRequestResp ) bool {
376
+
377
+ swapsCopy := make ([]loopInRequestResp , len (swaps ))
378
+ copy (swapsCopy , swaps )
379
+
380
+ for i := 0 ; i < len (swapsCopy ); i ++ {
317
381
actual := <- c .inRequest
318
382
319
- assert .Equal (c .t , expected .request , actual )
383
+ inner:
384
+ for i , swap := range swapsCopy {
385
+ equal := reflect .DeepEqual (swap .request , actual )
320
386
321
- c .loopIn <- expected .response
387
+ if equal {
388
+ c .loopIn <- swap .response
389
+
390
+ swapsCopy = append (
391
+ swapsCopy [:i ], swapsCopy [i + 1 :]... ,
392
+ )
393
+
394
+ break inner
395
+ }
396
+ }
322
397
}
398
+
399
+ return len (swapsCopy ) == 0
323
400
}
0 commit comments