From 4618f4c68f11a53c018a81962df697106cff99a7 Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Thu, 13 Jun 2024 11:07:36 +0200 Subject: [PATCH] fix(tests): flaky testdaemon (#1123) --- tests/asyncunit.nim | 26 -------------------------- tests/helpers.nim | 2 +- tests/testdaemon.nim | 44 +++++++------------------------------------- 3 files changed, 8 insertions(+), 64 deletions(-) diff --git a/tests/asyncunit.nim b/tests/asyncunit.nim index e7e38a3e9..e68bf9d29 100644 --- a/tests/asyncunit.nim +++ b/tests/asyncunit.nim @@ -28,29 +28,3 @@ template asyncTest*(name: string, body: untyped): untyped = body )() ) - -template flakyAsyncTest*(name: string, attempts: int, body: untyped): untyped = - test name: - var attemptNumber = 0 - while attemptNumber < attempts: - let isLastAttempt = attemptNumber == attempts - 1 - inc attemptNumber - try: - waitFor( - ( - proc() {.async.} = - body - )() - ) - except Exception as e: - if isLastAttempt: - raise e - else: - testStatusIMPL = TestStatus.FAILED - finally: - if not isLastAttempt: - if testStatusIMPL == TestStatus.FAILED: - # Retry - testStatusIMPL = TestStatus.OK - else: - break diff --git a/tests/helpers.nim b/tests/helpers.nim index 6269e4d51..3b8c90f2a 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -162,7 +162,7 @@ macro checkUntilCustomTimeout*(timeout: Duration, code: untyped): untyped = if `combinedBoolExpr`: return else: - await sleepAsync(1.millis) + await sleepAsync(100.millis) await checkExpiringInternal() diff --git a/tests/testdaemon.nim b/tests/testdaemon.nim index faeabe268..ceb7d44ff 100644 --- a/tests/testdaemon.nim +++ b/tests/testdaemon.nim @@ -38,41 +38,12 @@ proc connectStreamTest(): Future[bool] {.async.} = var stream = await api1.openStream(id2.peer, protos) let sent = await stream.transp.write(test & "\r\n") doAssert(sent == len(test) + 2) - var check = await wait(testFuture, 10.seconds) - doAssert(check == test) + doAssert((await wait(testFuture, 10.seconds)) == test) await stream.close() await api1.close() await api2.close() result = true -# proc provideCidTest(): Future[bool] {.async.} = -# var api1 = await newDaemonApi({DHTFull}) -# var api2 = await newDaemonApi({DHTFull}) -# var msg = "ethereum2-beacon-chain" -# var bmsg = cast[seq[byte]](msg) -# var mh = MultiHash.digest("sha2-256", bmsg) -# var cid = Cid.init(CIDv1, multiCodec("dag-pb"), mh) - -# var id1 = await api1.identity() -# var id2 = await api2.identity() - -# await api1.connect(id2.peer, id2.addresses) - -# while true: -# var peers = await api1.listPeers() -# if len(peers) > 0: -# break - -# await api1.dhtProvide(cid) -# var peers = await api2.dhtFindProviders(cid, 10) - -# if len(peers) == 1: -# if peers[0].peer == id1.peer: -# result = true - -# await api1.close() -# await api2.close() - proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} = var pubsubData = "TEST MESSAGE" var msgData = cast[seq[byte]](pubsubData) @@ -143,10 +114,9 @@ when isMainModule: test "Connect/Accept peer/stream test": check: waitFor(connectStreamTest()) == true - # test "Provide CID test": - # check: - # waitFor(provideCidTest()) == true - flakyAsyncTest "GossipSub test", attempts = 4: - check (await pubsubTest({PSGossipSub})) == true - flakyAsyncTest "FloodSub test", attempts = 4: - check (await pubsubTest({PSFloodSub})) == true + asyncTest "GossipSub test": + checkUntilTimeout: + (await pubsubTest({PSGossipSub})) + asyncTest "FloodSub test": + checkUntilTimeout: + (await pubsubTest({PSFloodSub}))