From 881a32f929d05969fcd5c18fb291adaca530efeb Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 7 Jul 2025 18:41:08 +0200 Subject: [PATCH] Stop the test when the request fails --- .../30_minutes/testmarketplace.nim | 14 +++-- tests/integration/30_minutes/testproofs.nim | 9 ++-- .../integration/30_minutes/testslotrepair.nim | 11 ++-- tests/integration/5_minutes/testsales.nim | 16 +++--- tests/integration/marketplacesuite.nim | 51 +++++++++++++++++++ 5 files changed, 82 insertions(+), 19 deletions(-) diff --git a/tests/integration/30_minutes/testmarketplace.nim b/tests/integration/30_minutes/testmarketplace.nim index 235abcdd..5afd627d 100644 --- a/tests/integration/30_minutes/testmarketplace.nim +++ b/tests/integration/30_minutes/testmarketplace.nim @@ -40,7 +40,8 @@ marketplacesuite(name = "Marketplace"): # As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues. await ethProvider.advanceTime(1.u256) - test "nodes negotiate contracts on the marketplace", marketplaceConfig: + test "nodes negotiate contracts on the marketplace", + marketplaceConfig, stopOnRequestFail = true: # host makes storage available let availability = ( await host.postAvailability( @@ -83,7 +84,7 @@ marketplacesuite(name = "Marketplace"): check slot.request.id == purchase.requestId test "node slots gets paid out and rest of tokens are returned to client", - marketplaceConfig: + marketplaceConfig, stopOnRequestFail = true: var providerRewardEvent = newAsyncEvent() var clientFundsEvent = newAsyncEvent() var transferEvent = newAsyncEvent() @@ -177,7 +178,8 @@ marketplacesuite(name = "Marketplace"): # .withLogFile() # .withLogTopics("marketplace", "sales", "statemachine","slotqueue", "reservations") .some, - ): + ), + stopOnRequestFail = true: var requestId: RequestId # We create an avavilability allowing the first SP to host the 3 slots. @@ -254,7 +256,8 @@ marketplacesuite(name = "Marketplace payouts"): # "node", "marketplace", "sales", "reservations", "node", "statemachine" # ) .some, - ): + ), + stopOnRequestFail = true: let client = clients()[0] let provider = providers()[0] let clientApi = client.client @@ -365,7 +368,8 @@ marketplacesuite(name = "Marketplace payouts"): # "node", "marketplace", "sales", "reservations", "statemachine" # ) .some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0] let provider0 = providers()[0] let provider1 = providers()[1] diff --git a/tests/integration/30_minutes/testproofs.nim b/tests/integration/30_minutes/testproofs.nim index d7772774..8b91050d 100644 --- a/tests/integration/30_minutes/testproofs.nim +++ b/tests/integration/30_minutes/testproofs.nim @@ -34,7 +34,8 @@ marketplacesuite(name = "Hosts submit regular proofs"): # .withLogFile() # uncomment to output log file to tests/integration/logs/ //_.log # .withLogTopics("marketplace", "sales", "reservations", "node", "clock") .some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0].client let expiry = 10.periods let duration = expiry + 5.periods @@ -118,7 +119,8 @@ marketplacesuite(name = "Simulate invalid proofs"): # uncomment to output log file to tests/integration/logs/ //_.log # .withLogTopics("validator", "onchain", "ethers", "clock") .some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0].client let expiry = 10.periods let duration = expiry + 10.periods @@ -162,7 +164,8 @@ marketplacesuite(name = "Simulate invalid proofs"): # .withLogFile() # uncomment to output log file to tests/integration/logs/ //_.log # .withLogTopics("validator", "onchain", "ethers", "clock") .some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0].client let expiry = 10.periods let duration = expiry + 10.periods diff --git a/tests/integration/30_minutes/testslotrepair.nim b/tests/integration/30_minutes/testslotrepair.nim index 9e1949d8..69e45ff7 100644 --- a/tests/integration/30_minutes/testslotrepair.nim +++ b/tests/integration/30_minutes/testslotrepair.nim @@ -4,7 +4,7 @@ import ../../contracts/time import ../../contracts/deployment import ../../codex/helpers import ../../examples -import ../marketplacesuite +import ../marketplacesuite as marketplacesuite import ../nodeconfigs export logutils @@ -75,7 +75,8 @@ marketplacesuite(name = "SP Slot Repair"): # .debug() # .withLogFile() # .withLogTopics("validator").some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0] let provider0 = providers()[0] let provider1 = providers()[1] @@ -154,7 +155,8 @@ marketplacesuite(name = "SP Slot Repair"): # .debug() .withLogFile() .withLogTopics("marketplace", "sales", "statemachine", "reservations").some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0] let provider0 = providers()[0] let provider1 = providers()[1] @@ -236,7 +238,8 @@ marketplacesuite(name = "SP Slot Repair"): # .withLogFile() # .withLogTopics("marketplace", "sales", "statemachine", "reservations") .some, - ): + ), + stopOnRequestFail = true: let client0 = clients()[0] let provider0 = providers()[0] let provider1 = providers()[1] diff --git a/tests/integration/5_minutes/testsales.nim b/tests/integration/5_minutes/testsales.nim index d8d5dcca..bd70685b 100644 --- a/tests/integration/5_minutes/testsales.nim +++ b/tests/integration/5_minutes/testsales.nim @@ -34,7 +34,7 @@ marketplacesuite(name = "Sales"): host = providers()[0].client client = clients()[0].client - test "node handles new storage availability", salesConfig: + test "node handles new storage availability", salesConfig, stopOnRequestFail = true: let availability1 = ( await host.postAvailability( totalSize = 1.uint64, @@ -53,7 +53,7 @@ marketplacesuite(name = "Sales"): ).get check availability1 != availability2 - test "node lists storage that is for sale", salesConfig: + test "node lists storage that is for sale", salesConfig, stopOnRequestFail = true: let availability = ( await host.postAvailability( totalSize = 1.uint64, @@ -64,7 +64,7 @@ marketplacesuite(name = "Sales"): ).get check availability in (await host.getAvailabilities()).get - test "updating availability", salesConfig: + test "updating availability", salesConfig, stopOnRequestFail = true: let availability = ( await host.postAvailability( totalSize = 140000.uint64, @@ -95,7 +95,8 @@ marketplacesuite(name = "Sales"): check updatedAvailability.enabled == false check updatedAvailability.until == until - test "updating availability - updating totalSize", salesConfig: + test "updating availability - updating totalSize", + salesConfig, stopOnRequestFail = true: let availability = ( await host.postAvailability( totalSize = 140000.uint64, @@ -112,7 +113,7 @@ marketplacesuite(name = "Sales"): check updatedAvailability.freeSize == 100000 test "updating availability - updating totalSize does not allow bellow utilized", - salesConfig: + salesConfig, stopOnRequestFail = true: let originalSize = 0xFFFFFF.uint64 let minPricePerBytePerSecond = 3.u256 let collateralPerByte = 1.u256 @@ -157,7 +158,8 @@ marketplacesuite(name = "Sales"): check newUpdatedAvailability.totalSize == originalSize + 20000 check newUpdatedAvailability.freeSize - updatedAvailability.freeSize == 20000 - test "updating availability fails with until negative", salesConfig: + test "updating availability fails with until negative", + salesConfig, stopOnRequestFail = true: let availability = ( await host.postAvailability( totalSize = 140000.uint64, @@ -174,7 +176,7 @@ marketplacesuite(name = "Sales"): (await response.body) == "Cannot set until to a negative value" test "returns an error when trying to update the until date before an existing a request is finished", - salesConfig: + salesConfig, stopOnRequestFail = true: let size = 0xFFFFFF.uint64 let duration = 20 * 60.uint64 let expiry = 10 * 60.uint64 diff --git a/tests/integration/marketplacesuite.nim b/tests/integration/marketplacesuite.nim index 6923f2f8..f226387a 100644 --- a/tests/integration/marketplacesuite.nim +++ b/tests/integration/marketplacesuite.nim @@ -23,6 +23,56 @@ template marketplacesuite*(name: string, body: untyped) = var token {.inject, used.}: Erc20Token var subscriptions: seq[Subscription] = @[] var tokenSubscription: Subscription + var requestFailedEvent: AsyncEvent + + template test(tname, startNodeConfigs, stopOnRequestFail, tbody) = + test tname, startNodeConfigs: + stopOnRequestFailed: + tbody + + template stopOnRequestFailed(tbody: untyped) = + let completed = newAsyncEvent() + let requestFailedEvent = newAsyncEvent() + + proc onRequestFailed(eventResult: ?!RequestFailed) {.raises: [].} = + assert not eventResult.isErr + requestFailedEvent.fire() + + let sub = await marketplace.subscribe(RequestFailed, onRequestFailed) + subscriptions.add(sub) + + let mainFut = ( + proc(): Future[void] {.async.} = + try: + tbody + completed.fire() + except CancelledError as e: + raise e + except CatchableError as e: + completed.fire() + raise e + )() + + let fastFailFut = ( + proc(): Future[void] {.async.} = + try: + await requestFailedEvent.wait().wait(timeout = chronos.seconds(60)) + completed.fire() + raise newException(TestFailedError, "storage request has failed") + except AsyncTimeoutError: + discard + )() + + await completed.wait().wait(timeout = chronos.seconds(60 * 30)) + + if not fastFailFut.completed: + await fastFailFut.cancelAndWait() + + if mainFut.failed: + raise mainFut.error + + if fastFailFut.failed: + raise fastFailFut.error proc check(cond: bool, reason = "Check failed"): void = if not cond: @@ -215,6 +265,7 @@ template marketplacesuite*(name: string, body: untyped) = period = config.proofs.period periodicity = Periodicity(seconds: period) subscriptions = @[] + requestFailedEvent = newAsyncEvent() teardown: for subscription in subscriptions: await subscription.unsubscribe()