summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKonstantin Molchanov <Konstantin Molchanov>2016-05-31 23:49:59 +0400
committerKonstantin Molchanov <Konstantin Molchanov>2016-05-31 23:49:59 +0400
commit81c7be1b35da6a4f7a06b3f081a92b516ee34ee6 (patch)
treef1702d2f879127a06f6d8db4360f973b069f7272
parentf44e0653560ea180ea1d4200f358a641816cdf2b (diff)
downloadNim-81c7be1b35da6a4f7a06b3f081a92b516ee34ee6.tar.gz
Tests: async: Tests for `all` proc added.
-rw-r--r--tests/async/tasyncall.nim50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim
new file mode 100644
index 000000000..971122ad9
--- /dev/null
+++ b/tests/async/tasyncall.nim
@@ -0,0 +1,50 @@
+discard """
+  file: "tasyncall.nim"
+  exitcode: 0
+"""
+import times, sequtils
+import asyncdispatch
+
+const
+  taskCount = 10
+  sleepDuration = 500
+
+proc futureWithValue(x: int): Future[int] {.async.} =
+  await sleepAsync(sleepDuration)
+  return x
+
+proc futureWithoutValue() {.async.} =
+  await sleepAsync(1000)
+
+proc testFuturesWithValue(x: int): seq[int] =
+  var tasks = newSeq[Future[int]](taskCount)
+
+  for i in 0..<taskCount:
+    tasks[i] = futureWithValue(x)
+
+  result = waitFor all(tasks)
+
+proc testFuturesWithoutValues() =
+  var tasks = newSeq[Future[void]](taskCount)
+
+  for i in 0..<taskCount:
+    tasks[i] = futureWithoutValue()
+
+  waitFor all(tasks)
+
+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