From 87be2c7f1f4a161a6a2fb2cccc20062c9595f01f Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Tue, 12 Jan 2021 10:26:48 +0900 Subject: [PATCH] make switch tests less sensitive to time (#501) * make switch tests less sensitive to time * missing new line --- tests/helpers.nim | 14 ++++++++++++++ tests/testswitch.nim | 19 +++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/helpers.nim b/tests/helpers.nim index 20cb856..29096ed 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -98,3 +98,17 @@ proc newBufferStream*(writeHandler: WriteHandler): TestBufferStream = new result result.writeHandler = writeHandler result.initStream() + +proc checkExpiringInternal(cond: proc(): bool): Future[bool] {.async, gcsafe.} = + {.gcsafe.}: + let start = Moment.now() + while true: + if Moment.now() > (start + chronos.seconds(5)): + return false + elif cond(): + return true + else: + await sleepAsync(1.millis) + +template checkExpiring*(code: untyped): untyped = + checkExpiringInternal(proc(): bool = code) diff --git a/tests/testswitch.nim b/tests/testswitch.nim index b77cff9..205ac2c 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -228,8 +228,7 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -278,8 +277,7 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -334,8 +332,7 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -389,8 +386,7 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -444,8 +440,7 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -514,8 +509,8 @@ suite "Switch": check not switch2.isConnected(switch1.peerInfo) check not switch3.isConnected(switch1.peerInfo) - await sleepAsync(1.millis) - check not switch1.isConnected(switch2.peerInfo) + check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) + check await(checkExpiring((not switch1.isConnected(switch3.peerInfo)))) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName)