make switch tests less sensitive to time (#501)

* make switch tests less sensitive to time

* missing new line
This commit is contained in:
Giovanni Petrantoni 2021-01-12 10:26:48 +09:00 committed by GitHub
parent b902c030a0
commit 87be2c7f1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 12 deletions

View File

@ -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)

View File

@ -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)