diff options
author | Federico Ceratto <federico.ceratto@suse.com> | 2017-05-16 14:36:21 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2017-05-16 14:36:21 +0100 |
commit | fcd86883edf8c3a94c43240c3c0be3ecd4e7ea8f (patch) | |
tree | 34817d54658b1fd576e24c864df1a84b6d298fcc /tests | |
parent | 80aa02e7f97b345a7a3673e47a9993f2ddb31dc6 (diff) | |
download | Nim-fcd86883edf8c3a94c43240c3c0be3ecd4e7ea8f.tar.gz |
Add waitFor on seq[Future], waitAll and more (#5189)
* Switch to unittest and speedup * Make timers and callbacks fields public
Diffstat (limited to 'tests')
-rw-r--r-- | tests/async/tasyncall.nim | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim index 63b2945a6..7daecd9ef 100644 --- a/tests/async/tasyncall.nim +++ b/tests/async/tasyncall.nim @@ -2,19 +2,19 @@ discard """ file: "tasyncall.nim" exitcode: 0 """ -import times, sequtils +import times, sequtils, unittest 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,38 +40,39 @@ proc testVarargs(x, y, z: int): seq[int] = result = waitFor all(a, b, c) -block: - let - startTime = cpuTime() - results = testFuturesWithValue(42) - expected = repeat(42, taskCount) - execTime = cpuTime() - startTime - - doAssert execTime * 1000 < taskCount * sleepDuration - doAssert results == expected - -block: - let startTime = cpuTime() - testFuturesWithoutValues() - 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 noIntFuturesFut.finished and not noIntFuturesFut.failed - doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed - doAssert noIntFuturesFut.read() == @[] +suite "tasyncall": + test "testFuturesWithValue": + let + startTime = cpuTime() + results = testFuturesWithValue(42) + expected = repeat(42, taskCount) + execTime = cpuTime() - startTime + + doAssert execTime * 1000 < taskCount * sleepDuration + doAssert results == expected + + test "testFuturesWithoutValues": + let startTime = cpuTime() + testFuturesWithoutValues() + let execTime = cpuTime() - startTime + + doAssert execTime * 1000 < taskCount * sleepDuration + + test "testVarargs": + let + startTime = cpuTime() + results = testVarargs(1, 2, 3) + expected = @[1, 2, 3] + execTime = cpuTime() - startTime + + doAssert execTime * 100 < taskCount * sleepDuration + doAssert results == expected + + test "all on seq[Future]": + let + noIntFuturesFut = all(newSeq[Future[int]]()) + noVoidFuturesFut = all(newSeq[Future[void]]()) + + doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed + doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed + doAssert noIntFuturesFut.read() == @[] |