apatheia/tests/ttasks.nim

43 lines
1.0 KiB
Nim
Raw Normal View History

2024-02-13 22:04:31 -07:00
import std/os
import chronos
import chronos/threadsync
import chronos/unittest2/asynctests
import taskpools
import apatheia/queues
import apatheia/tasks
## todo: setup basic async + threadsignal + taskpools example here
##
proc addNums(a, b: float): float {.asyncTask.} =
2024-02-13 22:59:18 -07:00
os.sleep(100)
2024-02-13 22:04:31 -07:00
echo "adding: ", a, " + ", b
return a + b
2024-02-13 22:58:27 -07:00
proc addNumValues(vals: openArray[float]): float {.asyncTask.} =
2024-02-13 22:59:18 -07:00
os.sleep(100)
2024-02-13 22:58:27 -07:00
result = 0.0
for x in vals:
result += x
echo "adding sums: ", vals
2024-02-13 22:46:35 -07:00
suite "async tests":
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
2024-02-13 22:54:53 -07:00
2024-02-13 22:58:27 -07:00
asyncTest "test addNums":
var jobs = newJobQueue[float](taskpool = tp)
2024-02-13 22:46:35 -07:00
echo "\nstart"
let res = await jobs.submit(addNums(1.0, 2.0,))
echo "result: ", res.repr
check true
2024-02-13 22:58:27 -07:00
asyncTest "test addNumValues":
var jobs = newJobQueue[float](taskpool = tp)
echo "\nstart"
let args = @[1.0, 2.0, 3.0]
let res = await jobs.submit(addNumValues(args))
echo "result: ", res.repr
check true