summary refs log tree commit diff stats
path: root/tests/async/tasyncall.nim
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@suse.com>2017-05-16 14:36:21 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2017-05-16 14:36:21 +0100
commitfcd86883edf8c3a94c43240c3c0be3ecd4e7ea8f (patch)
tree34817d54658b1fd576e24c864df1a84b6d298fcc /tests/async/tasyncall.nim
parent80aa02e7f97b345a7a3673e47a9993f2ddb31dc6 (diff)
downloadNim-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/async/tasyncall.nim')
-rw-r--r--tests/async/tasyncall.nim77
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() == @[]