diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/async/tfuturevar.nim | 47 | ||||
-rw-r--r-- | tests/async/tupcoming_async.nim | 13 |
2 files changed, 48 insertions, 12 deletions
diff --git a/tests/async/tfuturevar.nim b/tests/async/tfuturevar.nim new file mode 100644 index 000000000..73c0fddf7 --- /dev/null +++ b/tests/async/tfuturevar.nim @@ -0,0 +1,47 @@ +import asyncdispatch + +proc completeOnReturn(fut: FutureVar[string], x: bool) {.async.} = + if x: + fut.mget() = "" + fut.mget.add("foobar") + return + +proc completeOnImplicitReturn(fut: FutureVar[string], x: bool) {.async.} = + if x: + fut.mget() = "" + fut.mget.add("foobar") + +proc failureTest(fut: FutureVar[string], x: bool) {.async.} = + if x: + raise newException(Exception, "Test") + +proc manualComplete(fut: FutureVar[string], x: bool) {.async.} = + if x: + fut.mget() = "Hello World" + fut.complete() + return + +proc main() {.async.} = + var fut: FutureVar[string] + + fut = newFutureVar[string]() + await completeOnReturn(fut, true) + doAssert(fut.read() == "foobar") + + fut = newFutureVar[string]() + await completeOnImplicitReturn(fut, true) + doAssert(fut.read() == "foobar") + + fut = newFutureVar[string]() + let retFut = failureTest(fut, true) + yield retFut + doAssert(fut.read().isNil) + doAssert(fut.finished) + + fut = newFutureVar[string]() + await manualComplete(fut, true) + doAssert(fut.read() == "Hello World") + + +waitFor main() + diff --git a/tests/async/tupcoming_async.nim b/tests/async/tupcoming_async.nim index e53482dae..137794afd 100644 --- a/tests/async/tupcoming_async.nim +++ b/tests/async/tupcoming_async.nim @@ -44,19 +44,8 @@ when defined(upcoming): ev.setEvent() proc timerTest() = - var timeout = 200 - var errorRate = 40.0 - var start = epochTime() waitFor(waitTimer(200)) - var finish = epochTime() - var lowlimit = float(timeout) - float(timeout) * errorRate / 100.0 - var highlimit = float(timeout) + float(timeout) * errorRate / 100.0 - var elapsed = (finish - start) * 1_000 # convert to milliseconds - if elapsed >= lowlimit and elapsed < highlimit: - echo "OK" - else: - echo "timerTest: Timeout = " & $(elapsed) & ", but must be inside of [" & - $lowlimit & ", " & $highlimit & ")" + echo "OK" proc eventTest() = var event = newAsyncEvent() |