diff options
author | Araq <rumpf_a@web.de> | 2017-05-16 20:51:50 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2017-05-16 20:51:50 +0200 |
commit | 0f2648c56dec3681bb6e3b0d53c8a75f901c6b96 (patch) | |
tree | bf5d0761f2e9918e0082a8898624cb760204ec0b /tests/async | |
parent | 321d1f8b65c1cb41995ad033ea1a6a11bb22df8d (diff) | |
parent | 0613f08b2461c5ecb64518a65e20c3769938d21e (diff) | |
download | Nim-0f2648c56dec3681bb6e3b0d53c8a75f901c6b96.tar.gz |
Merge branch 'devel' of github.com:nim-lang/Nim into devel
Diffstat (limited to 'tests/async')
-rw-r--r-- | tests/async/tasyncall.nim | 77 | ||||
-rw-r--r-- | tests/async/tasyncfilewrite.nim | 17 |
2 files changed, 56 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() == @[] diff --git a/tests/async/tasyncfilewrite.nim b/tests/async/tasyncfilewrite.nim new file mode 100644 index 000000000..cda612bae --- /dev/null +++ b/tests/async/tasyncfilewrite.nim @@ -0,0 +1,17 @@ +discard """ + output: '''string 1 +string 2 +string 3''' +""" +# bug #5532 +import os, asyncfile, asyncdispatch + +removeFile("test.txt") +let f = openAsync("test.txt", fmWrite) +var futs = newSeq[Future[void]]() +for i in 1..3: + futs.add(f.write("string " & $i & "\n")) +waitFor(all(futs)) +f.close() +echo readFile("test.txt") + |