diff options
Diffstat (limited to 'tests/async/tasyncall.nim')
-rw-r--r-- | tests/async/tasyncall.nim | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim index 60ba557cc..3c318dbf7 100644 --- a/tests/async/tasyncall.nim +++ b/tests/async/tasyncall.nim @@ -1,5 +1,4 @@ discard """ - file: "tasyncall.nim" exitcode: 0 """ import times, sequtils @@ -7,14 +6,14 @@ import asyncdispatch const taskCount = 10 - sleepDuration = 500 + sleepDuration = 50 proc futureWithValue(x: int): Future[int] {.async.} = await sleepAsync(sleepDuration) return x proc futureWithoutValue() {.async.} = - await sleepAsync(1000) + await sleepAsync(sleepDuration) proc testFuturesWithValue(x: int): seq[int] = var tasks = newSeq[Future[int]](taskCount) @@ -40,29 +39,55 @@ proc testVarargs(x, y, z: int): seq[int] = result = waitFor all(a, b, c) +proc testWithDupes() = + var + tasks = newSeq[Future[void]](taskCount) + fut = futureWithoutValue() + + for i in 0..<taskCount: + tasks[i] = fut + + waitFor all(tasks) + block: - let - startTime = cpuTime() - results = testFuturesWithValue(42) - expected = repeat(42, taskCount) - execTime = cpuTime() - startTime + let + startTime = cpuTime() + results = testFuturesWithValue(42) + expected = repeat(42, taskCount) + execTime = cpuTime() - startTime - doAssert execTime * 1000 < taskCount * sleepDuration - doAssert results == expected + doAssert execTime * 1000 < taskCount * sleepDuration + doAssert results == expected block: - let startTime = cpuTime() - testFuturesWithoutValues() - let execTime = cpuTime() - startTime + let startTime = cpuTime() + testFuturesWithoutValues() + let execTime = cpuTime() - startTime - doAssert execTime * 1000 < taskCount * sleepDuration + doAssert execTime * 1000 < taskCount * sleepDuration block: - let - startTime = cpuTime() - results = testVarargs(1, 2, 3) - expected = @[1, 2, 3] - execTime = cpuTime() - startTime + let startTime = cpuTime() + testWithDupes() + let execTime = cpuTime() - startTime + + doAssert execTime * 1000 < taskCount * sleepDuration + +block: + let + startTime = cpuTime() + results = testVarargs(1, 2, 3) + expected = @[1, 2, 3] + execTime = cpuTime() - startTime + + doAssert execTime * 100 < taskCount * sleepDuration + doAssert results == expected + +block: + let + noIntFuturesFut = all(newSeq[Future[int]]()) + noVoidFuturesFut = all(newSeq[Future[void]]()) - doAssert execTime * 100 < taskCount * sleepDuration - doAssert results == expected + doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed + doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed + doAssert noIntFuturesFut.read() == @[] |