From 7006c6c72786aca9c17a3e82e2ae4f51dbb53531 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 +++++++++++ tests/integration/testmarketplace.nim | 13 +++++++------ tests/integration/testproofs.nim | 5 +++-- tests/integration/testsales.nim | 3 ++- 4 files changed, 23 insertions(+), 9 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/testmarketplace.nim b/tests/integration/testmarketplace.nim index d66e7613..e06b0121 100644 --- a/tests/integration/testmarketplace.nim +++ b/tests/integration/testmarketplace.nim @@ -6,6 +6,7 @@ import ../contracts/deployment import ./marketplacesuite import ./twonodes import ./nodeconfigs +from ../helpers import safeEventually marketplacesuite "Marketplace": let marketplaceConfig = NodeConfigs( @@ -61,7 +62,7 @@ marketplacesuite "Marketplace": tolerance = ecTolerance, ) - check eventually( + check safeEventually( await client.purchaseStateIs(id, "started"), timeout = 10 * 60 * 1000 ) let purchase = (await client.getPurchase(id)).get @@ -108,7 +109,7 @@ marketplacesuite "Marketplace": tolerance = ecTolerance, ) - check eventually( + check safeEventually( await client.purchaseStateIs(id, "started"), timeout = 10 * 60 * 1000 ) let purchase = (await client.getPurchase(id)).get @@ -124,11 +125,11 @@ marketplacesuite "Marketplace": # 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 ) @@ -298,12 +299,12 @@ marketplacesuite "Marketplace payouts": 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/testproofs.nim b/tests/integration/testproofs.nim index c49b7b6f..c85c580e 100644 --- a/tests/integration/testproofs.nim +++ b/tests/integration/testproofs.nim @@ -7,6 +7,7 @@ import ../codex/helpers import ../examples import ./marketplacesuite import ./nodeconfigs +from ../helpers import safeEventually export logutils @@ -64,7 +65,7 @@ marketplacesuite "Hosts submit regular proofs": let slotSize = slotSize(blocks, ecNodes, ecTolerance) - check eventually( + check safeEventually( await client0.purchaseStateIs(purchaseId, "started"), timeout = expiry.int * 1000 ) @@ -140,7 +141,7 @@ marketplacesuite "Simulate invalid proofs": ) let requestId = (await client0.requestId(purchaseId)).get - check eventually( + check safeEventually( await client0.purchaseStateIs(purchaseId, "started"), timeout = expiry.int * 1000 ) diff --git a/tests/integration/testsales.nim b/tests/integration/testsales.nim index ef999990..d8ae9ce2 100644 --- a/tests/integration/testsales.nim +++ b/tests/integration/testsales.nim @@ -8,6 +8,7 @@ import ../contracts/time import ./codexconfig import ./codexclient import ./nodeconfigs +from ../helpers import safeEventually proc findItem[T](items: seq[T], item: T): ?!T = for tmp in items: @@ -141,7 +142,7 @@ multinodesuite "Sales": ) ).get - check eventually( + check safeEventually( await client.purchaseStateIs(id, "started"), timeout = 10 * 60 * 1000 ) let updatedAvailability =