diff --git a/asynctest/private/asyncdispatch/eventually.nim b/asynctest/private/asyncdispatch/eventually.nim index 6b6df42..376601b 100644 --- a/asynctest/private/asyncdispatch/eventually.nim +++ b/asynctest/private/asyncdispatch/eventually.nim @@ -1,14 +1,14 @@ import std/asyncdispatch import std/times -template eventually*(expression: untyped, timeout=5000): bool = +template eventually*(expression: untyped, timeout=5000, pollInterval=1000): bool = proc eventually: Future[bool] {.async.} = let endTime = getTime() + initDuration(milliseconds=timeout) while not expression: if endTime < getTime(): return false - await sleepAsync(10) + await sleepAsync(pollInterval) return true await eventually() diff --git a/asynctest/private/chronos/eventually.nim b/asynctest/private/chronos/eventually.nim index 4d90bde..2738202 100644 --- a/asynctest/private/chronos/eventually.nim +++ b/asynctest/private/chronos/eventually.nim @@ -10,7 +10,7 @@ template eventuallyProcSignature(body: untyped): untyped = proc eventually: Future[bool] {.async.} = body -template eventually*(expression: untyped, timeout=5000): bool = +template eventually*(expression: untyped, timeout=5000, pollInterval=1000): bool = bind Moment, now, milliseconds eventuallyProcSignature: @@ -18,7 +18,7 @@ template eventually*(expression: untyped, timeout=5000): bool = while not expression: if endTime < Moment.now(): return false - await sleepAsync(10.milliseconds) + await sleepAsync(pollInterval.milliseconds) return true await eventually() diff --git a/testmodules/common/testbody.nim b/testmodules/common/testbody.nim index 1c4f725..0ffd7c0 100644 --- a/testmodules/common/testbody.nim +++ b/testmodules/common/testbody.nim @@ -42,13 +42,13 @@ suite "eventually": inc tries tries == 3 - check eventually becomesTrue() + check eventually(becomesTrue(), pollInterval=10) test "becomes false after timeout": proc remainsFalse: bool = false - check not eventually(remainsFalse(), timeout=100) + check not eventually(remainsFalse(), timeout=100, pollInterval=10) test "becomes true during timeout": @@ -56,7 +56,7 @@ suite "eventually": sleep(100) true - check eventually(slowTrue(), timeout=50) + check eventually(slowTrue(), timeout=50, pollInterval=10) test "works with async procedures": @@ -70,5 +70,21 @@ suite "eventually": x = 42 let future = slowProcedure() - check eventually x == 42 + check eventually(x == 42, pollInterval=10) await future + + test "respects poll interval": + var evaluations: int = 0 + + # If we try to access this from the closure, it will crash later with + # a segfault, so pass as var. + proc expensivePredicate(counter: var int): bool = + inc counter + return false + + check not eventually(evaluations.expensivePredicate(), pollInterval=100, timeout=500) + check 1 <= evaluations and evaluations <= 6 + + evaluations = 0 + check not eventually(evaluations.expensivePredicate(), pollInterval=10, timeout=500) + check 20 <= evaluations and evaluations <= 51