Skip to content

Commit

Permalink
add exception handling for Chronos V4
Browse files Browse the repository at this point in the history
  • Loading branch information
gmega committed Jan 11, 2024
1 parent 8e2f4e7 commit 7e99ef4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion asynctest/private/chronos/eventually.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
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 =
bind Moment, now, milliseconds

proc eventually: Future[bool] {.async.} =
eventuallyProcSignature:
let endTime = Moment.now() + timeout.milliseconds
while not expression:
if endTime < Moment.now():
Expand Down
7 changes: 7 additions & 0 deletions testmodules/chronosv4/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ suite "eventually and std/times":

test "compiles when both chronos and std/times are imported":
check eventually true

test "compiles when eventually expression can raise Exception":
proc aProc(): bool {.raises: [Exception].} = raise newException(
Exception, "an exception")
check:
compiles:
discard eventually aProc()

0 comments on commit 7e99ef4

Please sign in to comment.