From fcd86883edf8c3a94c43240c3c0be3ecd4e7ea8f Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Tue, 16 May 2017 14:36:21 +0100 Subject: [PATCH] Add waitFor on seq[Future], waitAll and more (#5189) * Switch to unittest and speedup * Make timers and callbacks fields public --- lib/pure/asyncdispatch.nim | 4 +-- tests/async/tasyncall.nim | 63 +++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index 1697384e00..8c1cf6b18f 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -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 diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim index 63b2945a64..7daecd9ef7 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 +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 + doAssert execTime * 1000 < taskCount * sleepDuration + doAssert results == expected -block: - let startTime = cpuTime() - testFuturesWithoutValues() - let execTime = cpuTime() - startTime + test "testFuturesWithoutValues": + let startTime = cpuTime() + testFuturesWithoutValues() + let execTime = cpuTime() - startTime - doAssert execTime * 1000 < taskCount * sleepDuration + doAssert execTime * 1000 < taskCount * sleepDuration -block: - let - startTime = cpuTime() - results = testVarargs(1, 2, 3) - expected = @[1, 2, 3] - execTime = cpuTime() - startTime + 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 + doAssert execTime * 100 < taskCount * sleepDuration + doAssert results == expected -block: - let - noIntFuturesFut = all(newSeq[Future[int]]()) - noVoidFuturesFut = all(newSeq[Future[void]]()) + 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() == @[] + doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed + doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed + doAssert noIntFuturesFut.read() == @[]