Skip to content

Commit 3be5a37

Browse files
committed
liquidity: fix flaky autoloop test
This failure became normal recently: === RUN TestAutoLoopInEnabled autoloop_testcontext_test.go:318: Error Trace: /home/runner/work/loop/loop/liquidity/autoloop_testcontext_test.go:318 /home/runner/work/loop/loop/liquidity/autoloop_test.go:804 Error: Not equal: expected: 80000 actual : 160000 Test: TestAutoLoopInEnabled autoloop_testcontext_test.go:318: Error Trace: /home/runner/work/loop/loop/liquidity/autoloop_testcontext_test.go:318 /home/runner/work/loop/loop/liquidity/autoloop_test.go:804 Error: Not equal: expected: 160000 actual : 80000 Test: TestAutoLoopInEnabled autoloop_testcontext_test.go:343: Error Trace: /home/runner/work/loop/loop/liquidity/autoloop_testcontext_test.go:343 /home/runner/work/loop/loop/liquidity/autoloop_test.go:804 Error: Should be true Test: TestAutoLoopInEnabled The root cause is them the order of items in c.quoteRequestIn depends on the order of loopInBuilder.buildSwap calls, which depends on the order of channel handling in Manager.SuggestSwaps, which depends on the order of map traversal, which is not determitistic. Since in the test all the amounts are different, I used amount as a key and put the expected calls into a map using amount as a key. When I extract an item from c.quoteRequestIn channel, I find the corresponding item in the map and remove it. All other logic is preserved.
1 parent 5c88cf8 commit 3be5a37

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

liquidity/autoloop_testcontext_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,26 @@ func (c *autoloopTestCtx) autoloop(step *autoloopStep) {
312312
// Assert that we query the server for a quote for each of our
313313
// recommended swaps. Note that this differs from our set of expected
314314
// swaps because we may get quotes for suggested swaps but then just
315-
// log them.
315+
// log them. The order in c.quoteRequestIn is not deterministic,
316+
// it depends on the order of map traversal (map peerChannels in
317+
// method Manager.SuggestSwaps). So receive from the channel an item
318+
// and then find a corresponding expected item, using amount as a key.
319+
amt2expected := make(map[btcutil.Amount]quoteInRequestResp)
316320
for _, expected := range step.quotesIn {
321+
// Make sure all amounts are unique.
322+
require.NotContains(c.t, amt2expected, expected.request.Amount)
323+
324+
amt2expected[expected.request.Amount] = expected
325+
}
326+
327+
for i := 0; i < len(step.quotesIn); i++ {
317328
request := <-c.quoteRequestIn
329+
330+
// Get the expected item, using amount as a key.
331+
expected, has := amt2expected[request.Amount]
332+
require.True(c.t, has)
333+
delete(amt2expected, request.Amount)
334+
318335
assert.Equal(
319336
c.t, expected.request.Amount, request.Amount,
320337
)

0 commit comments

Comments
 (0)