From 7ffae54dbb9386edc39615c62e2fce54334b2f72 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Wed, 14 Feb 2024 23:19:05 -0700 Subject: [PATCH] compile output test --- src/apatheia/jobs.nim | 45 ++++++++++++++++++++++++++++--------------- tests/tjobs.nim | 10 +++++----- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/apatheia/jobs.nim b/src/apatheia/jobs.nim index 9ff678d..77cb761 100644 --- a/src/apatheia/jobs.nim +++ b/src/apatheia/jobs.nim @@ -66,39 +66,55 @@ proc newJobQueue*[T](maxItems: int = 0, taskpool: Taskpool = Taskpool.new()): Jo result = JobQueue[T](queue: newSignalQueue[(uint, T)](maxItems), taskpool: taskpool, running: true) asyncSpawn(processJobs(result)) +template checkJobResultType(exp: typed) = + static: + echo "CHECKJOBRESULTTYPE:: ", $typeof(exp), " => ", typeof(exp) is void + assert typeof(exp) is void + exp + macro submitMacro(tp: untyped, jobs: untyped, exp: untyped): untyped = ## modifies the call expression to include the job queue and ## the job id parameters - let jobRes = genSym(nskLet, "jobRes") - let futName = genSym(nskLet, "fut") + # let jobRes = genSym(nskLet, "jobRes") + # let futName = genSym(nskLet, "fut") + let jobRes = ident("jobRes") + let futName = ident("fut") let nm = newLit(repr(exp)) - var fncall = nnkCall.newTree(exp[0]) - fncall.add(jobRes) - for p in exp[1..^1]: fncall.add(p) + var fncall = exp + exp.insert(1, jobRes) + result = quote do: - let (`jobRes`, `futName`) = createFuture(`jobs`, `nm`) - `jobs`.taskpool.spawn(`fncall`) - `futName` + block: + let (`jobRes`, `futName`) = createFuture(`jobs`, `nm`) + when typeof(`fncall`) isnot void: + {.error: "Apatheia jobs cannot return values. The given proc returns type: " & $(typeof(`fncall`)) & + " for call " & astToStr(`fncall`).} + `jobs`.taskpool.spawn(`fncall`) + `futName` - # echo "submit: res:\n", result.repr - # echo "" + echo "\nSUBMIT MACRO::\n", result.repr + echo "" + echo "\nSUBMIT MACRO::\n", result.treeRepr + echo "" template submit*[T](jobs: JobQueue[T], exp: untyped): Future[T] = submitMacro(T, jobs, exp) + when isMainModule: import os import chronos/threadsync import chronos/unittest2/asynctests import std/macros - proc addNumValues(jobResult: JobResult[float], vals: openArray[float]): float = + proc addNumValues(jobResult: JobResult[float], vals: openArray[float]) = os.sleep(100) - result = 0.0 + var res = 0.0 for x in vals: - result += x + res += x + discard jobResult.queue.send((jobResult.id, res,)) suite "async tests": @@ -108,8 +124,7 @@ when isMainModule: expandMacros: var jobs = newJobQueue[float](taskpool = tp) - let vals = @[1.0, 2.0] - let job = jobs.submit(addNumValues(vals)) + let job = jobs.submit(addNumValues([1.0, 2.0])) let res = await job check res == 3.0 diff --git a/tests/tjobs.nim b/tests/tjobs.nim index b85f495..5c336c5 100644 --- a/tests/tjobs.nim +++ b/tests/tjobs.nim @@ -17,16 +17,16 @@ proc addNums(jobResult: JobResult[float], a, b: float) = let res = addNumsRaw(a, b) discard jobResult.queue.send((jobResult.id, res,)) -proc addNumValues(jobResult: JobResult[float], vals: openArray[float]): float = - os.sleep(100) - result = 0.0 - for x in vals: - result += x +proc addNumsIncorrect(jobResult: JobResult[float], vals: openArray[float]): float = + discard suite "async tests": var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads. + asyncTest "cannot return value": + check not compiles(await jobs.submit(addNums(1.0, 2.0,))) + asyncTest "test": var jobs = newJobQueue[float](taskpool = tp)