@@ -2,7 +2,9 @@ package liquidity
22
33import (
44 "context"
5+ "reflect"
56 "testing"
7+ "time"
68
79 "github.com/btcsuite/btcd/btcutil"
810 "github.com/lightninglabs/lndclient"
@@ -11,8 +13,10 @@ import (
1113 "github.com/lightninglabs/loop/swap"
1214 "github.com/lightninglabs/loop/test"
1315 "github.com/lightningnetwork/lnd/clock"
16+ "github.com/lightningnetwork/lnd/lntypes"
1417 "github.com/lightningnetwork/lnd/ticker"
1518 "github.com/stretchr/testify/assert"
19+ "github.com/stretchr/testify/require"
1620)
1721
1822type autoloopTestCtx struct {
@@ -45,9 +49,17 @@ type autoloopTestCtx struct {
4549 // loopOuts is a channel that we get existing loop out swaps on.
4650 loopOuts chan []* loopdb.LoopOut
4751
52+ // loopOutSingle is the single loop out returned from fetching a single
53+ // swap from store.
54+ loopOutSingle * loopdb.LoopOut
55+
4856 // loopIns is a channel that we get existing loop in swaps on.
4957 loopIns chan []* loopdb.LoopIn
5058
59+ // loopInSingle is the single loop in returned from fetching a single
60+ // swap from store.
61+ loopInSingle * loopdb.LoopIn
62+
5163 // restrictions is a channel that we get swap restrictions on.
5264 restrictions chan * Restrictions
5365
@@ -131,6 +143,9 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
131143 ListLoopOut : func () ([]* loopdb.LoopOut , error ) {
132144 return <- testCtx .loopOuts , nil
133145 },
146+ GetLoopOut : func (hash lntypes.Hash ) (* loopdb.LoopOut , error ) {
147+ return testCtx .loopOutSingle , nil
148+ },
134149 ListLoopIn : func () ([]* loopdb.LoopIn , error ) {
135150 return <- testCtx .loopIns , nil
136151 },
@@ -188,6 +203,10 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
188203 testCtx .manager = NewManager (cfg )
189204 err := testCtx .manager .setParameters (context .Background (), parameters )
190205 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
191210 <- done
192211 return testCtx
193212}
@@ -241,14 +260,17 @@ type loopInRequestResp struct {
241260// autoloopStep contains all of the information to required to step
242261// through an autoloop tick.
243262type 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
252274}
253275
254276// autoloop walks our test context through the process of triggering our
@@ -269,6 +291,9 @@ func (c *autoloopTestCtx) autoloop(step *autoloopStep) {
269291 c .loopOuts <- step .existingOut
270292 c .loopIns <- step .existingIn
271293
294+ c .loopOutSingle = step .existingOutSingle
295+ c .loopInSingle = step .existingInSingle
296+
272297 // Assert that we query the server for a quote for each of our
273298 // recommended swaps. Note that this differs from our set of expected
274299 // swaps because we may get quotes for suggested swaps but then just
@@ -299,25 +324,77 @@ func (c *autoloopTestCtx) autoloop(step *autoloopStep) {
299324 c .quotes <- expected .quote
300325 }
301326
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 ++ {
304346 actual := <- c .outRequest
305347
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 {
309349 actual .DestAddr = nil
310350 }
311351
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+ }
314367 }
315368
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 ++ {
317381 actual := <- c .inRequest
318382
319- assert .Equal (c .t , expected .request , actual )
383+ inner:
384+ for i , swap := range swapsCopy {
385+ equal := reflect .DeepEqual (swap .request , actual )
320386
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+ }
322397 }
398+
399+ return len (swapsCopy ) == 0
323400}
0 commit comments