apatheia/tests/tasyncsEx2.nim

38 lines
724 B
Nim
Raw Normal View History

2024-02-09 19:17:50 -07:00
import std/os
import chronos
import chronos/threadsync
import chronos/unittest2/asynctests
import taskpools
2024-02-09 21:25:34 -07:00
import apatheia/queues
2024-02-09 19:17:50 -07:00
## todo: setup basic async + threadsignal + taskpools example here
##
type
ThreadArg = object
doneSig: ThreadSignalPtr
value: float
2024-02-09 21:44:07 -07:00
proc addNums(a, b: float, queue: SignalQueue[float]) =
2024-02-09 19:17:50 -07:00
os.sleep(500)
2024-02-09 21:44:07 -07:00
discard queue.send(a + b)
2024-02-09 19:17:50 -07:00
suite "async tests":
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
2024-02-09 21:44:07 -07:00
var queue = newSignalQueue[float]()
2024-02-09 19:17:50 -07:00
asyncTest "test":
2024-02-09 21:44:07 -07:00
tp.spawn addNums(1.0, 2.0, queue)
2024-02-09 19:17:50 -07:00
# await sleepAsync(100.milliseconds)
2024-02-09 21:44:07 -07:00
await wait(queue).wait(1500.milliseconds)
2024-02-09 19:17:50 -07:00
2024-02-09 21:44:07 -07:00
# echo "\nRES: ", args.value
2024-02-09 19:17:50 -07:00
check true