From 1663bd224fd5a45dc8a0ca169b7a786ef372e273 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Fri, 21 Mar 2025 14:14:48 +0100 Subject: [PATCH] Reduce the number of requests for asserts in tests --- tests/helpers.nim | 11 +++++++++++ .../integration/30_minutes/testmarketplace.nim.ignore | 9 +++++---- tests/integration/30_minutes/testproofs.nim.ignore | 1 + tests/integration/5_minutes/testsales.nim.ignore | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/helpers.nim b/tests/helpers.nim index b48b787e..4b556469 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -35,3 +35,14 @@ proc allFuturesThrowing*[T, E]( # https://github.com/nim-lang/Nim/issues/23432 futs: varargs[InternalRaisesFuture[T, E]] ): Future[void] = allFuturesThrowing(futs.mapIt(FutureBase(it))) + +template safeEventually*(expression: untyped, waitingTime = 250, timeout = 5000): bool = + proc safeEventually(): Future[bool] {.async.} = + let endTime = getTime() + initDuration(milliseconds = timeout) + while not expression: + if endTime < getTime(): + return false + await sleepAsync(waitingTime) + return true + + await safeEventually() diff --git a/tests/integration/30_minutes/testmarketplace.nim.ignore b/tests/integration/30_minutes/testmarketplace.nim.ignore index b04626c4..441626f0 100644 --- a/tests/integration/30_minutes/testmarketplace.nim.ignore +++ b/tests/integration/30_minutes/testmarketplace.nim.ignore @@ -6,6 +6,7 @@ import ../../contracts/deployment import ./../marketplacesuite import ../twonodes import ../nodeconfigs +from ../../helpers import safeEventually marketplacesuite(name = "Marketplace", stopOnRequestFail = true): let marketplaceConfig = NodeConfigs( @@ -122,11 +123,11 @@ marketplacesuite(name = "Marketplace", stopOnRequestFail = true): # Checking that the hosting node received reward for at least the time between let slotSize = slotSize(blocks, ecNodes, ecTolerance) let pricePerSlotPerSecond = minPricePerBytePerSecond * slotSize - check eventually (await token.balanceOf(hostAccount)) - startBalanceHost >= + check safeEventually (await token.balanceOf(hostAccount)) - startBalanceHost >= (duration - 5 * 60).u256 * pricePerSlotPerSecond * ecNodes.u256 # Checking that client node receives some funds back that were not used for the host nodes - check eventually( + check safeEventually( (await token.balanceOf(clientAccount)) - clientBalanceBeforeFinished > 0, timeout = 10 * 1000, # give client a bit of time to withdraw its funds ) @@ -296,12 +297,12 @@ marketplacesuite(name = "Marketplace payouts", stopOnRequestFail = true): let slotSize = slotSize(blocks, ecNodes, ecTolerance) let pricePerSlotPerSecond = minPricePerBytePerSecond * slotSize - check eventually ( + check safeEventually ( let endBalanceProvider = (await token.balanceOf(provider.ethAccount)) endBalanceProvider > startBalanceProvider and endBalanceProvider < startBalanceProvider + expiry.u256 * pricePerSlotPerSecond ) - check eventually( + check safeEventually( ( let endBalanceClient = (await token.balanceOf(client.ethAccount)) let endBalanceProvider = (await token.balanceOf(provider.ethAccount)) diff --git a/tests/integration/30_minutes/testproofs.nim.ignore b/tests/integration/30_minutes/testproofs.nim.ignore index b06e4d82..2a4c61fe 100644 --- a/tests/integration/30_minutes/testproofs.nim.ignore +++ b/tests/integration/30_minutes/testproofs.nim.ignore @@ -7,6 +7,7 @@ import ../../codex/helpers import ../../examples import ../marketplacesuite import ../nodeconfigs +from ../../helpers import safeEventually export logutils diff --git a/tests/integration/5_minutes/testsales.nim.ignore b/tests/integration/5_minutes/testsales.nim.ignore index 246d8fc7..025546ce 100644 --- a/tests/integration/5_minutes/testsales.nim.ignore +++ b/tests/integration/5_minutes/testsales.nim.ignore @@ -8,6 +8,7 @@ import ../../contracts/time import ../codexconfig import ../codexclient import ../nodeconfigs +from ../../helpers import safeEventually import ../marketplacesuite proc findItem[T](items: seq[T], item: T): ?!T =