summary refs log tree commit diff stats
path: root/tests/async/tasyncall.nim
blob: 3c318dbf71a00195d260fb621b5d1f0867914b33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.
discard """
  exitcode: 0
"""
import times, sequtils
import asyncdispatch

const
  taskCount = 10
  sleepDuration = 50

proc futureWithValue(x: int): Future[int] {.async.} =
  await sleepAsync(sleepDuration)
  return x

proc futureWithoutValue() {.async.} =
  await sleepAsync(sleepDuration)

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)

proc testVarargs(x, y, z: int): seq[int] =
  let
    a = futureWithValue(x)
    b = futureWithValue(y)
    c = futureWithValue(z)

  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

    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()
    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 noIntFuturesFut.finished and not noIntFuturesFut.failed
    doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed
    doAssert noIntFuturesFut.read() == @[]