Add waitFor on seq[Future], waitAll and more (#5189)
* Switch to unittest and speedup * Make timers and callbacks fields public
This commit is contained in:
parent
80aa02e7f9
commit
fcd86883ed
2 changed files with 34 additions and 33 deletions
|
|
@ -163,8 +163,8 @@ include includes/asyncfutures
|
|||
|
||||
type
|
||||
PDispatcherBase = ref object of RootRef
|
||||
timers: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
||||
callbacks: Deque[proc ()]
|
||||
timers*: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
||||
callbacks*: Deque[proc ()]
|
||||
|
||||
proc processTimers(p: PDispatcherBase) {.inline.} =
|
||||
#Process just part if timers at a step
|
||||
|
|
|
|||
|
|
@ -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,7 +40,8 @@ proc testVarargs(x, y, z: int): seq[int] =
|
|||
|
||||
result = waitFor all(a, b, c)
|
||||
|
||||
block:
|
||||
suite "tasyncall":
|
||||
test "testFuturesWithValue":
|
||||
let
|
||||
startTime = cpuTime()
|
||||
results = testFuturesWithValue(42)
|
||||
|
|
@ -50,14 +51,14 @@ block:
|
|||
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||
doAssert results == expected
|
||||
|
||||
block:
|
||||
test "testFuturesWithoutValues":
|
||||
let startTime = cpuTime()
|
||||
testFuturesWithoutValues()
|
||||
let execTime = cpuTime() - startTime
|
||||
|
||||
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||
|
||||
block:
|
||||
test "testVarargs":
|
||||
let
|
||||
startTime = cpuTime()
|
||||
results = testVarargs(1, 2, 3)
|
||||
|
|
@ -67,7 +68,7 @@ block:
|
|||
doAssert execTime * 100 < taskCount * sleepDuration
|
||||
doAssert results == expected
|
||||
|
||||
block:
|
||||
test "all on seq[Future]":
|
||||
let
|
||||
noIntFuturesFut = all(newSeq[Future[int]]())
|
||||
noVoidFuturesFut = all(newSeq[Future[void]]())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue