add exception handling for Chronos V4

This commit is contained in:
gmega 2024-01-11 16:54:33 -03:00 committed by Giuliano Mega
parent 8e2f4e73b9
commit 12c356672d
2 changed files with 18 additions and 1 deletions

View File

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

View File

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