add exception handling for Chronos V4
This commit is contained in:
parent
8e2f4e73b9
commit
12c356672d
|
@ -1,9 +1,19 @@
|
||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
|
import pkg/chronos/config
|
||||||
|
|
||||||
|
template eventuallyProcSignature(body: untyped): untyped =
|
||||||
|
when compiles(config.chronosHandleException):
|
||||||
|
proc eventually: Future[bool] {.async: (handleException: true,
|
||||||
|
raises: [AsyncExceptionError, CancelledError]).} =
|
||||||
|
body
|
||||||
|
else:
|
||||||
|
proc eventually: Future[bool] {.async.} =
|
||||||
|
body
|
||||||
|
|
||||||
template eventually*(expression: untyped, timeout=5000): bool =
|
template eventually*(expression: untyped, timeout=5000): bool =
|
||||||
bind Moment, now, milliseconds
|
bind Moment, now, milliseconds
|
||||||
|
|
||||||
proc eventually: Future[bool] {.async.} =
|
eventuallyProcSignature:
|
||||||
let endTime = Moment.now() + timeout.milliseconds
|
let endTime = Moment.now() + timeout.milliseconds
|
||||||
while not expression:
|
while not expression:
|
||||||
if endTime < Moment.now():
|
if endTime < Moment.now():
|
||||||
|
|
|
@ -9,3 +9,10 @@ suite "eventually and std/times":
|
||||||
|
|
||||||
test "compiles when both chronos and std/times are imported":
|
test "compiles when both chronos and std/times are imported":
|
||||||
check eventually true
|
check eventually true
|
||||||
|
|
||||||
|
test "compiles when the expression handed to eventually can raise Exception":
|
||||||
|
proc aProc(): bool {.raises: [Exception].} = raise newException(
|
||||||
|
Exception, "an exception")
|
||||||
|
check:
|
||||||
|
compiles:
|
||||||
|
discard eventually aProc()
|
||||||
|
|
Loading…
Reference in New Issue