From 13bbf2b0520bb22a24404481254dfc79d7079231 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 18 Jul 2022 10:46:24 +0200 Subject: [PATCH] Fix test that was failing intermittently Give test more time to complete but do not increase sleep time more than necessary, by introducing waiting loop for test purposes. --- tests/codex/blockexchange/engine/testblockexc.nim | 10 ++++------ tests/codex/helpers.nim | 3 ++- tests/codex/helpers/eventually.nim | 13 +++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/codex/helpers/eventually.nim diff --git a/tests/codex/blockexchange/engine/testblockexc.nim b/tests/codex/blockexchange/engine/testblockexc.nim index f898bb79..b8c5f0a9 100644 --- a/tests/codex/blockexchange/engine/testblockexc.nim +++ b/tests/codex/blockexchange/engine/testblockexc.nim @@ -120,9 +120,8 @@ suite "NetworkStore engine - 2 nodes": .engine .taskQueue .pushOrUpdateNoWait(peerCtx1).isOk - await sleepAsync(100.millis) - check nodeCmps1.localStore.hasBlock(blk.cid) + check eventually nodeCmps1.localStore.hasBlock(blk.cid) test "Should get blocks from remote": let blocks = await allFinished( @@ -148,11 +147,10 @@ suite "NetworkStore engine - 2 nodes": let blocks = await allFinished( blocks2.mapIt( nodeCmps1.networkStore.getBlock(it.cid) )) - await sleepAsync(100.millis) + let channel = !peerCtx1.paymentChannel + let wallet = nodeCmps2.wallet - let - channel = !peerCtx1.paymentChannel - check nodeCmps2.wallet.balance(channel, Asset) > 0 + check eventually wallet.balance(channel, Asset) > 0 suite "NetworkStore - multiple nodes": let diff --git a/tests/codex/helpers.nim b/tests/codex/helpers.nim index 9f57fd9c..7dc22877 100644 --- a/tests/codex/helpers.nim +++ b/tests/codex/helpers.nim @@ -9,8 +9,9 @@ import pkg/codex/rng import ./helpers/nodeutils import ./helpers/randomchunker import ./helpers/mockdiscovery +import ./helpers/eventually -export randomchunker, nodeutils, mockdiscovery +export randomchunker, nodeutils, mockdiscovery, eventually # NOTE: The meaning of equality for blocks # is changed here, because blocks are now `ref` diff --git a/tests/codex/helpers/eventually.nim b/tests/codex/helpers/eventually.nim new file mode 100644 index 00000000..bbeef3be --- /dev/null +++ b/tests/codex/helpers/eventually.nim @@ -0,0 +1,13 @@ +import pkg/chronos + +template eventually*(condition: untyped, timeout = 5.seconds): bool = + proc loop: Future[bool] {.async.} = + let start = Moment.now() + while true: + if condition: + return true + if Moment.now() > (start + timeout): + return false + else: + await sleepAsync(1.millis) + await loop()