mirror of
https://github.com/status-im/asynctest.git
synced 2025-02-22 01:28:16 +00:00
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.
75 lines
1.4 KiB
Nim
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
|