Rewrite timer tests to handle timer imprecision on macOS

This commit is contained in:
Mark Spanbroek 2020-11-11 11:47:22 +01:00 committed by markspanbroek
parent e4a3a1d5e8
commit 138ef9dcb1
1 changed files with 11 additions and 18 deletions

View File

@ -10,42 +10,36 @@ suite "timeout":
body
Moment.now() - start
template checkDuration(duration, minimum: Duration, overshoot = 5.milliseconds) =
check duration > minimum
check duration < minimum + overshoot
asynctest "can expire":
let duration = measure:
let timeout = newTimeout()
timeout.set(10.milliseconds)
await timeout.expired()
checkDuration duration, 10.milliseconds
check duration > 10.milliseconds
asynctest "can be reset before expiry":
let timeout = newTimeout()
timeout.set(10.milliseconds)
let duration = measure:
let timeout = newTimeout()
timeout.set(10.milliseconds)
await sleepAsync(5.milliseconds)
timeout.set(10.milliseconds)
timeout.set(20.milliseconds)
await timeout.expired()
checkDuration duration, 15.milliseconds
check duration > 20.milliseconds
asynctest "can be reset after expiry":
let timeout = newTimeout()
timeout.set(10.milliseconds)
await timeout.expired()
let duration = measure:
let timeout = newTimeout()
timeout.set(10.milliseconds)
await timeout.expired()
timeout.set(10.milliseconds)
await timeout.expired()
checkDuration duration, 20.milliseconds
check duration > 10.milliseconds
asynctest "can be stopped":
let timeout = newTimeout()
timeout.set(10.milliseconds)
let expiry = timeout.expired()
await sleepAsync(5.milliseconds)
timeout.stop()
await sleepAsync(5.milliseconds)
await sleepAsync(10.milliseconds)
check not expiry.finished()
expiry.cancel()
@ -54,9 +48,8 @@ suite "timeout":
proc callback = called = true
let timeout = newTimeout(callback)
timeout.set(10.milliseconds)
await sleepAsync(5.milliseconds)
check not called
await sleepAsync(5.milliseconds)
await timeout.expired()
check called
timeout.stop()