Mark Spanbroek 90c1b35b67 Explicitly import either asyncdispatch or chronos version
Reorganizes the code into separate versions for asyncdispatch and
chronos so that we no longer have to rely on hard-to-maintain code
that implicitly works with both asyncdispatch and chronos.

This is a backwards incompatible change.
2024-01-09 11:40:31 +01:00

75 lines
1.4 KiB
Nim

proc someAsyncProc {.async.} =
# perform some async operations using await
discard
suite "test async proc":
setup:
# invoke await in the test setup:
await someAsyncProc()
teardown:
# invoke await in the test teardown:
await someAsyncProc()
test "async test":
# invoke await in tests:
await someAsyncProc()
suite "setupAll and teardownAll":
setupAll:
# invoke await in the test setup:
await someAsyncProc()
teardownAll:
# invoke await in the test teardown:
await someAsyncProc()
test "async test":
# invoke await in tests:
await someAsyncProc()
from std/os import sleep
suite "eventually":
test "becomes true":
var tries = 0
proc becomesTrue: bool =
inc tries
tries == 3
check eventually becomesTrue()
test "becomes false after timeout":
proc remainsFalse: bool = false
check not eventually(remainsFalse(), timeout=100)
test "becomes true during timeout":
proc slowTrue: bool =
sleep(100)
true
check eventually(slowTrue(), timeout=50)
test "works with async procedures":
var x: int
proc slowProcedure {.async.} =
when compiles(await sleepAsync(100.milliseconds)):
await sleepAsync(100.milliseconds) # chronos
else:
await sleepAsync(100) # asyncdispatch
x = 42
let future = slowProcedure()
check eventually x == 42
await future