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
|
type
|
||||||
PDispatcherBase = ref object of RootRef
|
PDispatcherBase = ref object of RootRef
|
||||||
timers: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
timers*: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
||||||
callbacks: Deque[proc ()]
|
callbacks*: Deque[proc ()]
|
||||||
|
|
||||||
proc processTimers(p: PDispatcherBase) {.inline.} =
|
proc processTimers(p: PDispatcherBase) {.inline.} =
|
||||||
#Process just part if timers at a step
|
#Process just part if timers at a step
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,19 @@ discard """
|
||||||
file: "tasyncall.nim"
|
file: "tasyncall.nim"
|
||||||
exitcode: 0
|
exitcode: 0
|
||||||
"""
|
"""
|
||||||
import times, sequtils
|
import times, sequtils, unittest
|
||||||
import asyncdispatch
|
import asyncdispatch
|
||||||
|
|
||||||
const
|
const
|
||||||
taskCount = 10
|
taskCount = 10
|
||||||
sleepDuration = 500
|
sleepDuration = 50
|
||||||
|
|
||||||
proc futureWithValue(x: int): Future[int] {.async.} =
|
proc futureWithValue(x: int): Future[int] {.async.} =
|
||||||
await sleepAsync(sleepDuration)
|
await sleepAsync(sleepDuration)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
proc futureWithoutValue() {.async.} =
|
proc futureWithoutValue() {.async.} =
|
||||||
await sleepAsync(1000)
|
await sleepAsync(sleepDuration)
|
||||||
|
|
||||||
proc testFuturesWithValue(x: int): seq[int] =
|
proc testFuturesWithValue(x: int): seq[int] =
|
||||||
var tasks = newSeq[Future[int]](taskCount)
|
var tasks = newSeq[Future[int]](taskCount)
|
||||||
|
|
@ -40,38 +40,39 @@ proc testVarargs(x, y, z: int): seq[int] =
|
||||||
|
|
||||||
result = waitFor all(a, b, c)
|
result = waitFor all(a, b, c)
|
||||||
|
|
||||||
block:
|
suite "tasyncall":
|
||||||
let
|
test "testFuturesWithValue":
|
||||||
startTime = cpuTime()
|
let
|
||||||
results = testFuturesWithValue(42)
|
startTime = cpuTime()
|
||||||
expected = repeat(42, taskCount)
|
results = testFuturesWithValue(42)
|
||||||
execTime = cpuTime() - startTime
|
expected = repeat(42, taskCount)
|
||||||
|
execTime = cpuTime() - startTime
|
||||||
|
|
||||||
doAssert execTime * 1000 < taskCount * sleepDuration
|
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||||
doAssert results == expected
|
doAssert results == expected
|
||||||
|
|
||||||
block:
|
test "testFuturesWithoutValues":
|
||||||
let startTime = cpuTime()
|
let startTime = cpuTime()
|
||||||
testFuturesWithoutValues()
|
testFuturesWithoutValues()
|
||||||
let execTime = cpuTime() - startTime
|
let execTime = cpuTime() - startTime
|
||||||
|
|
||||||
doAssert execTime * 1000 < taskCount * sleepDuration
|
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||||
|
|
||||||
block:
|
test "testVarargs":
|
||||||
let
|
let
|
||||||
startTime = cpuTime()
|
startTime = cpuTime()
|
||||||
results = testVarargs(1, 2, 3)
|
results = testVarargs(1, 2, 3)
|
||||||
expected = @[1, 2, 3]
|
expected = @[1, 2, 3]
|
||||||
execTime = cpuTime() - startTime
|
execTime = cpuTime() - startTime
|
||||||
|
|
||||||
doAssert execTime * 100 < taskCount * sleepDuration
|
doAssert execTime * 100 < taskCount * sleepDuration
|
||||||
doAssert results == expected
|
doAssert results == expected
|
||||||
|
|
||||||
block:
|
test "all on seq[Future]":
|
||||||
let
|
let
|
||||||
noIntFuturesFut = all(newSeq[Future[int]]())
|
noIntFuturesFut = all(newSeq[Future[int]]())
|
||||||
noVoidFuturesFut = all(newSeq[Future[void]]())
|
noVoidFuturesFut = all(newSeq[Future[void]]())
|
||||||
|
|
||||||
doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed
|
doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed
|
||||||
doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed
|
doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed
|
||||||
doAssert noIntFuturesFut.read() == @[]
|
doAssert noIntFuturesFut.read() == @[]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue