2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-22 23:28:16 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
import unittest2
|
2023-09-15 16:38:39 +00:00
|
|
|
import stew/results
|
2023-02-09 04:08:47 +00:00
|
|
|
import ../chronos, ../chronos/unittest2/asynctests
|
2018-05-22 23:28:16 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
{.used.}
|
2019-10-24 13:01:57 +00:00
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
type
|
|
|
|
TestFooConnection* = ref object
|
|
|
|
id*: int
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
suite "Future[T] behavior test suite":
|
|
|
|
proc testFuture1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(0.milliseconds)
|
2018-05-22 23:28:16 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testFuture2(): Future[int] {.async.} =
|
|
|
|
return 1
|
2018-05-22 23:28:16 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testFuture3(): Future[int] {.async.} =
|
|
|
|
result = await testFuture2()
|
2018-05-22 23:28:16 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testFuture100(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "Async undefined behavior (#7758) test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var fut = testFuture1()
|
|
|
|
poll()
|
|
|
|
poll()
|
|
|
|
if not fut.finished:
|
|
|
|
poll()
|
2021-06-04 10:28:04 +00:00
|
|
|
check: fut.finished
|
2019-03-30 22:31:10 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "Immediately completed asynchronous procedure test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var fut = testFuture3()
|
2021-06-04 10:28:04 +00:00
|
|
|
check: fut.finished
|
2019-03-30 22:31:10 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "Future[T] callbacks are invoked in reverse order (#7197) test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var testResult = ""
|
|
|
|
var fut = testFuture1()
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "1"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "2"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "3"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "4"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "5"
|
|
|
|
discard waitFor(fut)
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut.finished
|
|
|
|
testResult == "12345"
|
|
|
|
|
|
|
|
test "Future[T] callbacks not changing order after removeCallback()":
|
2019-03-30 22:31:10 +00:00
|
|
|
var testResult = ""
|
|
|
|
var fut = testFuture1()
|
|
|
|
proc cb1(udata: pointer) =
|
|
|
|
testResult &= "1"
|
|
|
|
proc cb2(udata: pointer) =
|
|
|
|
testResult &= "2"
|
|
|
|
proc cb3(udata: pointer) =
|
|
|
|
testResult &= "3"
|
|
|
|
proc cb4(udata: pointer) =
|
|
|
|
testResult &= "4"
|
|
|
|
proc cb5(udata: pointer) =
|
|
|
|
testResult &= "5"
|
|
|
|
fut.addCallback cb1
|
|
|
|
fut.addCallback cb2
|
|
|
|
fut.addCallback cb3
|
|
|
|
fut.addCallback cb4
|
|
|
|
fut.addCallback cb5
|
|
|
|
fut.removeCallback cb3
|
|
|
|
discard waitFor(fut)
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut.finished
|
|
|
|
testResult == "1245"
|
2019-03-30 22:31:10 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "wait[T]() test":
|
|
|
|
block:
|
|
|
|
## Test for not immediately completed future and timeout = -1
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture1(), InfiniteDuration)
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
block:
|
|
|
|
## Test for immediately completed future and timeout = -1
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture2(), InfiniteDuration)
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
block:
|
|
|
|
## Test for not immediately completed future and timeout = 0
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture1(), 0.milliseconds)
|
|
|
|
false
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
## Test for immediately completed future and timeout = 0
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture2(), 0.milliseconds)
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
## Test for future which cannot be completed in timeout period
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture100(), 50.milliseconds)
|
|
|
|
false
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
## Test for future which will be completed before timeout exceeded.
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
discard await wait(testFuture100(), 500.milliseconds)
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
asyncTest "Discarded result Future[T] test":
|
2019-07-04 12:04:59 +00:00
|
|
|
var completedFutures = 0
|
|
|
|
|
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client4() {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client5() {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client1f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f() {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f() {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
2020-09-03 11:37:53 +00:00
|
|
|
discard client1()
|
|
|
|
discard client1f()
|
|
|
|
discard client2()
|
|
|
|
discard client2f()
|
|
|
|
discard client3()
|
|
|
|
discard client3f()
|
|
|
|
discard client4()
|
|
|
|
discard client4f()
|
|
|
|
discard client5()
|
|
|
|
discard client5f()
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
check completedFutures == 10
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "allFutures(zero) test":
|
2019-07-04 12:04:59 +00:00
|
|
|
var tseq = newSeq[Future[int]]()
|
|
|
|
var fut = allFutures(tseq)
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut.finished
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "allFutures(varargs) test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var completedFutures = 0
|
|
|
|
|
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient4() {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient5() {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient1f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient2f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient3f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient4f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient5f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client4(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client5(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client1f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(vlient1(), vlient2(), vlient3(), vlient4(), vlient5())
|
|
|
|
check completedFutures == 5
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
completedFutures = 0
|
2021-06-04 10:28:04 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(vlient1(), vlient1f(), vlient2(), vlient2f(), vlient3(),
|
|
|
|
vlient3f(), vlient4(), vlient4f(), vlient5(), vlient5f())
|
|
|
|
check completedFutures == 10
|
2019-03-30 22:31:10 +00:00
|
|
|
|
|
|
|
completedFutures = 0
|
2023-02-09 04:08:47 +00:00
|
|
|
|
|
|
|
await allFutures(client1(), client2(), client3(), client4(), client5())
|
|
|
|
check completedFutures == 5
|
2019-03-30 22:31:10 +00:00
|
|
|
|
|
|
|
completedFutures = 0
|
2023-02-09 04:08:47 +00:00
|
|
|
|
|
|
|
await allFutures(client1(), client1f(), client2(), client2f(), client3(),
|
|
|
|
client3f(), client4(), client4f(), client5(), client5f())
|
|
|
|
check completedFutures == 10
|
|
|
|
|
|
|
|
asyncTest "allFutures(varargs) test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var completedFutures = 0
|
|
|
|
var vfutures = newSeq[Future[void]]()
|
|
|
|
var nfutures = newSeq[Future[int]]()
|
|
|
|
|
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient4() {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient5() {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient1f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient2f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient3f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient4f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient5f() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client4(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client5(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client1f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
vfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
vfutures.add(vlient1())
|
|
|
|
vfutures.add(vlient2())
|
|
|
|
vfutures.add(vlient3())
|
|
|
|
vfutures.add(vlient4())
|
|
|
|
vfutures.add(vlient5())
|
2019-03-15 00:54:16 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(vfutures)
|
2019-03-30 22:31:10 +00:00
|
|
|
# 5 * 10 completed futures = 50
|
2023-02-09 04:08:47 +00:00
|
|
|
check completedFutures == 50
|
2019-03-30 22:31:10 +00:00
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
vfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
vfutures.add(vlient1())
|
|
|
|
vfutures.add(vlient1f())
|
|
|
|
vfutures.add(vlient2())
|
|
|
|
vfutures.add(vlient2f())
|
|
|
|
vfutures.add(vlient3())
|
|
|
|
vfutures.add(vlient3f())
|
|
|
|
vfutures.add(vlient4())
|
|
|
|
vfutures.add(vlient4f())
|
|
|
|
vfutures.add(vlient5())
|
|
|
|
vfutures.add(vlient5f())
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(vfutures)
|
2019-03-30 22:31:10 +00:00
|
|
|
# 10 * 10 completed futures = 100
|
2023-02-09 04:08:47 +00:00
|
|
|
check completedFutures == 100
|
2019-03-30 22:31:10 +00:00
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
nfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
nfutures.add(client1())
|
|
|
|
nfutures.add(client2())
|
|
|
|
nfutures.add(client3())
|
|
|
|
nfutures.add(client4())
|
|
|
|
nfutures.add(client5())
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(nfutures)
|
2019-07-04 12:04:59 +00:00
|
|
|
# 5 * 10 completed futures = 50
|
2023-02-09 04:08:47 +00:00
|
|
|
check completedFutures == 50
|
2019-03-30 22:31:10 +00:00
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
nfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
nfutures.add(client1())
|
|
|
|
nfutures.add(client1f())
|
|
|
|
nfutures.add(client2())
|
|
|
|
nfutures.add(client2f())
|
|
|
|
nfutures.add(client3())
|
|
|
|
nfutures.add(client3f())
|
|
|
|
nfutures.add(client4())
|
|
|
|
nfutures.add(client4f())
|
|
|
|
nfutures.add(client5())
|
|
|
|
nfutures.add(client5f())
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(nfutures)
|
2019-03-30 22:31:10 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
# 10 * 10 completed futures = 100
|
2023-02-09 04:08:47 +00:00
|
|
|
check completedFutures == 100
|
2021-06-04 10:28:04 +00:00
|
|
|
|
|
|
|
test "allFutures() already completed test":
|
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
var fut = allFutures(client1(), client2())
|
|
|
|
check:
|
|
|
|
fut.finished()
|
|
|
|
not(fut.failed())
|
2019-03-30 22:31:10 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "allFinished() already completed test":
|
2020-04-06 10:56:24 +00:00
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
var fut = allFinished(client1(), client2())
|
|
|
|
check:
|
|
|
|
fut.finished()
|
|
|
|
not(fut.failed())
|
|
|
|
len(fut.read()) == 2
|
2020-04-06 10:56:24 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "one(zero) test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var tseq = newSeq[Future[int]]()
|
2019-07-04 12:04:59 +00:00
|
|
|
var fut = one(tseq)
|
2021-06-04 10:28:04 +00:00
|
|
|
check: fut.finished and fut.failed
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "one(varargs) test":
|
2019-06-04 16:51:35 +00:00
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 10
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = 20
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = 30
|
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var fut11 = vlient1()
|
|
|
|
var fut12 = vlient2()
|
|
|
|
var fut13 = vlient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res1 = await one(fut11, fut12, fut13)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var fut21 = vlient2()
|
|
|
|
var fut22 = vlient1()
|
|
|
|
var fut23 = vlient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res2 = await one(fut21, fut22, fut23)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var fut31 = vlient3()
|
|
|
|
var fut32 = vlient2()
|
|
|
|
var fut33 = vlient1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res3 = await one(fut31, fut32, fut33)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut11 == res1
|
|
|
|
fut22 == res2
|
|
|
|
fut33 == res3
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var cut11 = client1()
|
|
|
|
var cut12 = client2()
|
|
|
|
var cut13 = client3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res4 = await one(cut11, cut12, cut13)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var cut21 = client2()
|
|
|
|
var cut22 = client1()
|
|
|
|
var cut23 = client3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res5 = await one(cut21, cut22, cut23)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var cut31 = client3()
|
|
|
|
var cut32 = client2()
|
|
|
|
var cut33 = client1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res6 = await one(cut31, cut32, cut33)
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
cut11 == res4
|
|
|
|
cut22 == res5
|
|
|
|
cut33 == res6
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "one(seq) test":
|
2019-06-04 16:51:35 +00:00
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 10
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = 20
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = 30
|
|
|
|
|
|
|
|
var v10 = vlient1()
|
|
|
|
var v11 = vlient2()
|
|
|
|
var v12 = vlient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res1 = await one(@[v10, v11, v12])
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2019-06-04 16:51:35 +00:00
|
|
|
var v20 = vlient2()
|
|
|
|
var v21 = vlient1()
|
|
|
|
var v22 = vlient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res2 = await one(@[v20, v21, v22])
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2019-06-04 16:51:35 +00:00
|
|
|
var v30 = vlient3()
|
|
|
|
var v31 = vlient2()
|
|
|
|
var v32 = vlient1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res3 = await one(@[v30, v31, v32])
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
res1 == v10
|
|
|
|
res2 == v21
|
|
|
|
res3 == v32
|
2019-07-04 12:04:59 +00:00
|
|
|
|
|
|
|
var c10 = client1()
|
|
|
|
var c11 = client2()
|
|
|
|
var c12 = client3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res4 = await one(@[c10, c11, c12])
|
2019-07-04 12:04:59 +00:00
|
|
|
|
|
|
|
var c20 = client2()
|
|
|
|
var c21 = client1()
|
|
|
|
var c22 = client3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res5 = await one(@[c20, c21, c22])
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2019-07-04 12:04:59 +00:00
|
|
|
var c30 = client3()
|
|
|
|
var c31 = client2()
|
|
|
|
var c32 = client1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res6 = await one(@[c30, c31, c32])
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
res4 == c10
|
|
|
|
res5 == c21
|
|
|
|
res6 == c32
|
2019-06-04 16:51:35 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "one(completed) test":
|
2020-04-06 10:56:24 +00:00
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 3
|
|
|
|
|
|
|
|
var f10 = client1()
|
|
|
|
var f20 = client2()
|
|
|
|
var f30 = client3()
|
|
|
|
var fut1 = one(f30, f10, f20)
|
|
|
|
var f11 = client1()
|
|
|
|
var f21 = client2()
|
|
|
|
var f31 = client3()
|
|
|
|
var fut2 = one(f31, f21, f11)
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut1.finished()
|
|
|
|
not(fut1.failed())
|
|
|
|
fut1.read() == f10
|
|
|
|
fut2.finished()
|
|
|
|
not(fut2.failed())
|
|
|
|
fut2.read() == f21
|
2020-04-06 10:56:24 +00:00
|
|
|
|
2024-03-06 05:42:22 +00:00
|
|
|
asyncTest "one() exception effect":
|
|
|
|
proc checkraises() {.async: (raises: [CancelledError]).} =
|
|
|
|
let f = Future[void].Raising([CancelledError]).init()
|
|
|
|
f.complete()
|
|
|
|
one(f).cancelSoon()
|
|
|
|
|
|
|
|
await checkraises()
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "or() test":
|
2020-04-21 04:07:49 +00:00
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
|
|
|
|
proc client3() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4() {.async.} =
|
|
|
|
await sleepAsync(400.milliseconds)
|
|
|
|
if true:
|
2023-02-09 04:08:47 +00:00
|
|
|
raise newException(IOError, "")
|
2020-04-21 04:07:49 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
proc client5() {.async.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc client6() {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client7() {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(IOError, "")
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client1() or client2()
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client2() or client1()
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client1() or client4()
|
|
|
|
true
|
|
|
|
except IOError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client2() or client4()
|
|
|
|
true
|
|
|
|
except IOError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client4() or client2()
|
|
|
|
true
|
|
|
|
except IOError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client1() or client3()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client3() or client1()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client3() or client4()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client4() or client3()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client5() or client6()
|
|
|
|
true
|
|
|
|
except ValueError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client6() or client5()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client6() or client7()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except IOError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client7() or client6()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
false
|
|
|
|
except IOError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
asyncTest "or() already completed test":
|
2020-04-21 04:07:49 +00:00
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
result = 1
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
proc client3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 3
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client1() or client2()
|
|
|
|
true
|
|
|
|
except ValueError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
var fut1 = client1()
|
|
|
|
var fut2 = client3()
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await fut1 or fut2
|
|
|
|
true
|
|
|
|
except ValueError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
let discarded {.used.} = await fut2
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
var fut1 = client2()
|
|
|
|
var fut2 = client3()
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await fut1 or fut2
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
let discarded {.used.} = await fut2
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await client2() or client1()
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
var fut1 = client3()
|
|
|
|
var fut2 = client1()
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await fut1 or fut2
|
|
|
|
true
|
|
|
|
except ValueError:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
let discarded {.used.} = await fut1
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
var fut1 = client3()
|
|
|
|
var fut2 = client2()
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await fut1 or fut2
|
|
|
|
false
|
|
|
|
except ValueError:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
let discarded {.used.} = await fut1
|
|
|
|
check res
|
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
asyncTest "tryCancel() async procedure test":
|
2019-06-26 12:36:01 +00:00
|
|
|
var completed = 0
|
|
|
|
|
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
await client1()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client3() {.async.} =
|
|
|
|
await client2()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client4() {.async.} =
|
|
|
|
await client3()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
var fut = client4()
|
2023-09-15 16:38:39 +00:00
|
|
|
discard fut.tryCancel()
|
2019-06-26 12:36:01 +00:00
|
|
|
|
|
|
|
# Future must not be cancelled immediately, because it has many nested
|
|
|
|
# futures.
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
not fut.cancelled()
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
expect(CancelledError):
|
2023-02-09 04:08:47 +00:00
|
|
|
await fut
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
check completed == 0
|
2021-06-04 10:28:04 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "cancelAndWait() test":
|
2019-06-26 12:36:01 +00:00
|
|
|
var completed = 0
|
|
|
|
|
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
await client1()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client3() {.async.} =
|
|
|
|
await client2()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client4() {.async.} =
|
|
|
|
await client3()
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
var fut = client4()
|
2023-02-09 04:08:47 +00:00
|
|
|
await cancelAndWait(fut)
|
|
|
|
check fut.cancelled()
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "Break cancellation propagation test":
|
2019-06-26 12:36:01 +00:00
|
|
|
var completed = 0
|
|
|
|
|
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
try:
|
|
|
|
await client1()
|
|
|
|
except CancelledError:
|
|
|
|
discard
|
|
|
|
inc(completed)
|
|
|
|
|
|
|
|
var fut1 = client2()
|
|
|
|
var fut2 = client2()
|
2023-09-15 16:38:39 +00:00
|
|
|
discard fut1.tryCancel()
|
2023-02-09 04:08:47 +00:00
|
|
|
await fut1
|
|
|
|
await cancelAndWait(fut2)
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
not fut1.cancelled()
|
|
|
|
not fut2.cancelled()
|
|
|
|
completed == 2
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "Cancellation callback test":
|
2019-06-26 12:36:01 +00:00
|
|
|
var completed = 0
|
|
|
|
var cancelled = 0
|
|
|
|
|
|
|
|
proc client1(duration: Duration): Future[void] =
|
|
|
|
## Suspends the execution of the current async procedure for the next
|
|
|
|
## ``duration`` time.
|
|
|
|
var retFuture = newFuture[void]()
|
|
|
|
let moment = Moment.fromNow(duration)
|
|
|
|
|
|
|
|
proc completion(data: pointer) {.gcsafe.} =
|
|
|
|
inc(completed)
|
|
|
|
if not(retFuture.finished()):
|
|
|
|
retFuture.complete()
|
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
2019-06-26 12:36:01 +00:00
|
|
|
inc(cancelled)
|
|
|
|
if not(retFuture.finished()):
|
|
|
|
removeTimer(moment, completion, cast[pointer](retFuture))
|
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
retFuture.cancelCallback = cancellation
|
2020-01-27 18:28:44 +00:00
|
|
|
discard setTimer(moment, completion, cast[pointer](retFuture))
|
2019-06-26 12:36:01 +00:00
|
|
|
return retFuture
|
|
|
|
|
|
|
|
var fut = client1(100.milliseconds)
|
2023-09-15 16:38:39 +00:00
|
|
|
discard fut.tryCancel()
|
2023-02-09 04:08:47 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut.cancelled()
|
|
|
|
completed == 0
|
|
|
|
cancelled == 1
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "Cancellation wait() test":
|
2020-01-27 18:28:44 +00:00
|
|
|
var neverFlag1, neverFlag2, neverFlag3: bool
|
|
|
|
var waitProc1, waitProc2: bool
|
|
|
|
proc neverEndingProc(): Future[void] =
|
|
|
|
var res = newFuture[void]()
|
|
|
|
proc continuation(udata: pointer) {.gcsafe.} =
|
|
|
|
neverFlag2 = true
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
neverFlag3 = true
|
|
|
|
res.addCallback(continuation)
|
|
|
|
res.cancelCallback = cancellation
|
|
|
|
result = res
|
|
|
|
neverFlag1 = true
|
|
|
|
|
|
|
|
proc waitProc() {.async.} =
|
|
|
|
try:
|
|
|
|
await wait(neverEndingProc(), 100.milliseconds)
|
|
|
|
except CancelledError:
|
|
|
|
waitProc1 = true
|
2023-02-09 04:08:47 +00:00
|
|
|
except CatchableError:
|
|
|
|
doAssert(false)
|
2020-01-27 18:28:44 +00:00
|
|
|
finally:
|
|
|
|
waitProc2 = true
|
|
|
|
|
|
|
|
var fut = waitProc()
|
|
|
|
await cancelAndWait(fut)
|
2023-02-09 04:08:47 +00:00
|
|
|
check:
|
2023-05-25 15:31:35 +00:00
|
|
|
fut.state == FutureState.Completed
|
2023-02-09 04:08:47 +00:00
|
|
|
neverFlag1 and neverFlag2 and neverFlag3 and waitProc1 and waitProc2
|
2020-01-27 18:28:44 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "Cancellation withTimeout() test":
|
2020-01-27 18:28:44 +00:00
|
|
|
var neverFlag1, neverFlag2, neverFlag3: bool
|
|
|
|
var waitProc1, waitProc2: bool
|
|
|
|
proc neverEndingProc(): Future[void] =
|
|
|
|
var res = newFuture[void]()
|
|
|
|
proc continuation(udata: pointer) {.gcsafe.} =
|
|
|
|
neverFlag2 = true
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
neverFlag3 = true
|
|
|
|
res.addCallback(continuation)
|
|
|
|
res.cancelCallback = cancellation
|
|
|
|
neverFlag1 = true
|
2023-09-15 16:38:39 +00:00
|
|
|
res
|
2020-01-27 18:28:44 +00:00
|
|
|
|
|
|
|
proc withTimeoutProc() {.async.} =
|
|
|
|
try:
|
|
|
|
discard await withTimeout(neverEndingProc(), 100.milliseconds)
|
|
|
|
doAssert(false)
|
|
|
|
except CancelledError:
|
|
|
|
waitProc1 = true
|
2023-02-09 04:08:47 +00:00
|
|
|
except CatchableError:
|
|
|
|
doAssert(false)
|
2020-01-27 18:28:44 +00:00
|
|
|
finally:
|
|
|
|
waitProc2 = true
|
|
|
|
|
|
|
|
var fut = withTimeoutProc()
|
|
|
|
await cancelAndWait(fut)
|
2023-02-09 04:08:47 +00:00
|
|
|
check:
|
2023-05-25 15:31:35 +00:00
|
|
|
fut.state == FutureState.Completed
|
2023-02-09 04:08:47 +00:00
|
|
|
neverFlag1 and neverFlag2 and neverFlag3 and waitProc1 and waitProc2
|
2020-01-27 18:28:44 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "Cancellation race test":
|
2020-07-03 12:03:59 +00:00
|
|
|
var someFut = newFuture[void]()
|
|
|
|
|
|
|
|
proc raceProc(): Future[void] {.async.} =
|
|
|
|
await someFut
|
|
|
|
|
|
|
|
var raceFut1 = raceProc()
|
|
|
|
someFut.complete()
|
|
|
|
await cancelAndWait(raceFut1)
|
|
|
|
|
|
|
|
someFut = newFuture[void]()
|
|
|
|
var raceFut2 = raceProc()
|
|
|
|
someFut.fail(newException(ValueError, ""))
|
|
|
|
await cancelAndWait(raceFut2)
|
|
|
|
|
|
|
|
someFut = newFuture[void]()
|
|
|
|
var raceFut3 = raceProc()
|
2023-09-15 16:38:39 +00:00
|
|
|
discard someFut.tryCancel()
|
2020-07-03 12:03:59 +00:00
|
|
|
await cancelAndWait(raceFut3)
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
check:
|
2023-09-15 16:38:39 +00:00
|
|
|
raceFut1.state == FutureState.Completed
|
|
|
|
raceFut2.state == FutureState.Failed
|
2023-02-09 04:08:47 +00:00
|
|
|
raceFut3.state == FutureState.Cancelled
|
2020-09-01 18:41:18 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "asyncSpawn() test":
|
2020-09-01 18:41:18 +00:00
|
|
|
proc completeTask1() {.async.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc completeTask2() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc errorTask() {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc cancelTask() {.async.} =
|
|
|
|
await sleepAsync(10.seconds)
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
var fut1 = completeTask1()
|
|
|
|
var fut2 = completeTask2()
|
|
|
|
asyncSpawn fut1
|
|
|
|
asyncSpawn fut2
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
if not(fut1.finished()) or not(fut2.finished()):
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
if fut1.failed() or fut1.cancelled() or fut2.failed() or
|
|
|
|
fut2.cancelled():
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
asyncSpawn errorTask()
|
|
|
|
false
|
|
|
|
except FutureDefect:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
block:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
var fut = cancelTask()
|
|
|
|
await cancelAndWait(fut)
|
|
|
|
asyncSpawn fut
|
|
|
|
false
|
|
|
|
except FutureDefect:
|
|
|
|
true
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
2020-09-01 18:41:18 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "location test":
|
2020-09-03 11:37:53 +00:00
|
|
|
# WARNING: This test is very sensitive to line numbers and module name.
|
2024-03-06 05:42:22 +00:00
|
|
|
template start(): int =
|
|
|
|
instantiationInfo().line
|
2020-09-03 11:37:53 +00:00
|
|
|
|
2024-03-06 05:42:22 +00:00
|
|
|
const first = start()
|
2023-10-17 12:18:14 +00:00
|
|
|
proc macroFuture() {.async.} =
|
|
|
|
let someVar {.used.} = 5 # LINE POSITION 1
|
2020-09-03 11:37:53 +00:00
|
|
|
let someOtherVar {.used.} = 4
|
|
|
|
if true:
|
2023-10-17 12:18:14 +00:00
|
|
|
let otherVar {.used.} = 3 # LINE POSITION 2
|
2020-09-03 11:37:53 +00:00
|
|
|
|
|
|
|
template templateFuture(): untyped =
|
|
|
|
newFuture[void]("template")
|
|
|
|
|
|
|
|
proc procFuture(): Future[void] =
|
2023-02-09 04:08:47 +00:00
|
|
|
newFuture[void]("procedure") # LINE POSITION 5
|
2020-09-03 11:37:53 +00:00
|
|
|
|
|
|
|
var fut1 = macroFuture()
|
2023-02-09 04:08:47 +00:00
|
|
|
var fut2 = templateFuture() # LINE POSITION 3
|
2020-09-03 11:37:53 +00:00
|
|
|
var fut3 = procFuture()
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
fut2.complete() # LINE POSITION 4
|
|
|
|
fut3.complete() # LINE POSITION 6
|
2020-09-03 11:37:53 +00:00
|
|
|
|
2023-08-09 14:27:17 +00:00
|
|
|
{.push warning[Deprecated]: off.} # testing backwards compatibility interface
|
2020-09-03 11:37:53 +00:00
|
|
|
let loc10 = fut1.location[0]
|
|
|
|
let loc11 = fut1.location[1]
|
|
|
|
let loc20 = fut2.location[0]
|
|
|
|
let loc21 = fut2.location[1]
|
|
|
|
let loc30 = fut3.location[0]
|
|
|
|
let loc31 = fut3.location[1]
|
2023-08-09 14:27:17 +00:00
|
|
|
{.pop.}
|
2020-09-03 11:37:53 +00:00
|
|
|
|
|
|
|
proc chk(loc: ptr SrcLoc, file: string, line: int,
|
|
|
|
procedure: string): bool =
|
|
|
|
if len(procedure) == 0:
|
|
|
|
(loc.line == line) and ($loc.file == file)
|
|
|
|
else:
|
|
|
|
(loc.line == line) and ($loc.file == file) and
|
|
|
|
(loc.procedure == procedure)
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
2024-03-06 05:42:22 +00:00
|
|
|
chk(loc10, "testfut.nim", first + 2, "macroFuture")
|
|
|
|
chk(loc11, "testfut.nim", first + 5, "")
|
|
|
|
chk(loc20, "testfut.nim", first + 14, "template")
|
|
|
|
chk(loc21, "testfut.nim", first + 17, "")
|
|
|
|
chk(loc30, "testfut.nim", first + 11, "procedure")
|
|
|
|
chk(loc31, "testfut.nim", first + 18, "")
|
2023-02-09 04:08:47 +00:00
|
|
|
|
|
|
|
asyncTest "withTimeout(fut) should wait cancellation test":
|
2020-11-17 09:59:02 +00:00
|
|
|
proc futureNeverEnds(): Future[void] =
|
|
|
|
newFuture[void]("neverending.future")
|
|
|
|
|
|
|
|
proc futureOneLevelMore() {.async.} =
|
|
|
|
await futureNeverEnds()
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var fut = futureOneLevelMore()
|
|
|
|
try:
|
|
|
|
let res = await withTimeout(fut, 100.milliseconds)
|
|
|
|
# Because `fut` is never-ending Future[T], `withTimeout` should return
|
|
|
|
# `false` but it also has to wait until `fut` is cancelled.
|
|
|
|
if not(res) and fut.cancelled():
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
except CatchableError:
|
|
|
|
false
|
|
|
|
check res
|
|
|
|
|
|
|
|
asyncTest "wait(fut) should wait cancellation test":
|
2020-11-17 09:59:02 +00:00
|
|
|
proc futureNeverEnds(): Future[void] =
|
|
|
|
newFuture[void]("neverending.future")
|
|
|
|
|
|
|
|
proc futureOneLevelMore() {.async.} =
|
|
|
|
await futureNeverEnds()
|
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
var fut = futureOneLevelMore()
|
|
|
|
let res =
|
2020-11-17 09:59:02 +00:00
|
|
|
try:
|
|
|
|
await wait(fut, 100.milliseconds)
|
2023-02-09 04:08:47 +00:00
|
|
|
false
|
2020-11-17 09:59:02 +00:00
|
|
|
except AsyncTimeoutError:
|
|
|
|
# Because `fut` is never-ending Future[T], `wait` should raise
|
|
|
|
# `AsyncTimeoutError`, but only after `fut` is cancelled.
|
|
|
|
if fut.cancelled():
|
2023-02-09 04:08:47 +00:00
|
|
|
true
|
2020-11-17 09:59:02 +00:00
|
|
|
else:
|
2023-02-09 04:08:47 +00:00
|
|
|
false
|
2020-11-17 09:59:02 +00:00
|
|
|
except CatchableError:
|
2023-02-09 04:08:47 +00:00
|
|
|
false
|
2020-11-17 09:59:02 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
check res
|
2020-11-17 09:59:02 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
test "race(zero) test":
|
2020-11-26 22:50:55 +00:00
|
|
|
var tseq = newSeq[FutureBase]()
|
|
|
|
var fut1 = race(tseq)
|
2023-11-20 10:04:28 +00:00
|
|
|
check:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/22964
|
|
|
|
not compiles(block:
|
|
|
|
var fut2 = race())
|
|
|
|
not compiles(block:
|
|
|
|
var fut3 = race([]))
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
fut1.failed()
|
2023-11-20 10:04:28 +00:00
|
|
|
# fut2.failed()
|
|
|
|
# fut3.failed()
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "race(varargs) test":
|
2020-11-26 22:50:55 +00:00
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
|
|
|
|
proc ilient1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 10
|
|
|
|
|
|
|
|
proc ilient2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = 20
|
|
|
|
|
|
|
|
proc ilient3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = 30
|
|
|
|
|
|
|
|
proc slient1(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = "sclient1"
|
|
|
|
|
|
|
|
proc slient2(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = "sclient2"
|
|
|
|
|
|
|
|
proc slient3(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = "sclient3"
|
|
|
|
|
|
|
|
var fut11 = vlient1()
|
|
|
|
var fut12 = ilient2()
|
|
|
|
var fut13 = slient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res1 = await race(fut11, fut12, fut13)
|
|
|
|
check FutureBase(fut11) == res1
|
|
|
|
await allFutures(fut12, fut13)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var fut21 = vlient2()
|
|
|
|
var fut22 = ilient1()
|
|
|
|
var fut23 = slient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res2 = await race(fut21, fut22, fut23)
|
|
|
|
check FutureBase(fut22) == res2
|
|
|
|
await allFutures(fut21, fut23)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var fut31 = vlient3()
|
|
|
|
var fut32 = ilient2()
|
|
|
|
var fut33 = slient1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res3 = await race(fut31, fut32, fut33)
|
|
|
|
check FutureBase(fut33) == res3
|
|
|
|
await allFutures(fut31, fut32)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var fut41 = vlient1()
|
|
|
|
var fut42 = slient2()
|
|
|
|
var fut43 = ilient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res4 = await race(fut41, fut42, fut43)
|
|
|
|
check FutureBase(fut41) == res4
|
|
|
|
await allFutures(fut42, fut43)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "race(seq) test":
|
2020-11-26 22:50:55 +00:00
|
|
|
proc vlient1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
|
|
|
|
proc ilient1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = 10
|
|
|
|
|
|
|
|
proc ilient2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = 20
|
|
|
|
|
|
|
|
proc ilient3(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = 30
|
|
|
|
|
|
|
|
proc slient1(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = "slient1"
|
|
|
|
|
|
|
|
proc slient2(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
result = "slient2"
|
|
|
|
|
|
|
|
proc slient3(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
result = "slient3"
|
|
|
|
|
|
|
|
var v10 = vlient1()
|
|
|
|
var v11 = ilient2()
|
|
|
|
var v12 = slient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res1 = await race(@[FutureBase(v10), FutureBase(v11), FutureBase(v12)])
|
|
|
|
check res1 == FutureBase(v10)
|
|
|
|
await allFutures(v11, v12)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var v20 = vlient2()
|
|
|
|
var v21 = ilient1()
|
|
|
|
var v22 = slient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res2 = await race(@[FutureBase(v20), FutureBase(v21), FutureBase(v22)])
|
|
|
|
check res2 == FutureBase(v21)
|
|
|
|
await allFutures(v20, v22)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var v30 = vlient3()
|
|
|
|
var v31 = ilient2()
|
|
|
|
var v32 = slient1()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res3 = await race(@[FutureBase(v30), FutureBase(v31), FutureBase(v32)])
|
|
|
|
check res3 == FutureBase(v32)
|
|
|
|
await allFutures(v30, v31)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
|
|
|
var v40 = vlient1()
|
|
|
|
var v41 = slient2()
|
|
|
|
var v42 = ilient3()
|
2023-02-09 04:08:47 +00:00
|
|
|
var res4 = await race(@[FutureBase(v40), FutureBase(v41), FutureBase(v42)])
|
|
|
|
check res4 == FutureBase(v40)
|
|
|
|
await allFutures(v41, v42)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
asyncTest "race() already completed test":
|
2020-11-26 22:50:55 +00:00
|
|
|
proc client1(): Future[int] {.async.} =
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = "client3"
|
|
|
|
|
|
|
|
var f10 = client1()
|
|
|
|
var f20 = client2()
|
|
|
|
var f30 = client3()
|
|
|
|
var fut1 = race(f30, f10, f20)
|
|
|
|
var f11 = client1()
|
|
|
|
var f21 = client2()
|
|
|
|
var f31 = client3()
|
|
|
|
var fut2 = race(f31, f21, f11)
|
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
2023-05-25 15:31:35 +00:00
|
|
|
fut1.completed() and fut1.read() == FutureBase(f10)
|
|
|
|
fut2.completed() and fut2.read() == FutureBase(f21)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await allFutures(f20, f30, f11, f31)
|
|
|
|
|
|
|
|
asyncTest "race() cancellation test":
|
2020-11-26 22:50:55 +00:00
|
|
|
proc client1() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(200.milliseconds)
|
|
|
|
return 10
|
|
|
|
|
|
|
|
proc client3(): Future[string] {.async.} =
|
|
|
|
await sleepAsync(300.milliseconds)
|
|
|
|
return "client3"
|
|
|
|
|
|
|
|
var f1 = client1()
|
|
|
|
var f2 = client2()
|
|
|
|
var f3 = client3()
|
|
|
|
var fut = race(f1, f2, f3)
|
2023-02-09 04:08:47 +00:00
|
|
|
await cancelAndWait(fut)
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
2023-02-09 04:08:47 +00:00
|
|
|
not(f1.finished())
|
|
|
|
not(f2.finished())
|
|
|
|
not(f3.finished())
|
2020-11-26 22:50:55 +00:00
|
|
|
|
2023-02-09 04:08:47 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-07-04 12:04:59 +00:00
|
|
|
|
2021-06-04 10:28:04 +00:00
|
|
|
check:
|
|
|
|
f1.finished()
|
|
|
|
f2.finished()
|
|
|
|
f3.finished()
|
2019-06-26 12:36:01 +00:00
|
|
|
|
2024-03-06 05:42:22 +00:00
|
|
|
asyncTest "race() exception effect":
|
|
|
|
proc checkraises() {.async: (raises: [CancelledError]).} =
|
|
|
|
let f = Future[void].Raising([CancelledError]).init()
|
|
|
|
f.complete()
|
|
|
|
race(f).cancelSoon()
|
|
|
|
|
|
|
|
await checkraises()
|
|
|
|
|
2021-10-21 14:22:11 +00:00
|
|
|
test "Unsigned integer overflow test":
|
|
|
|
check:
|
|
|
|
0xFFFF_FFFF_FFFF_FFFF'u64 + 1'u64 == 0'u64
|
|
|
|
0xFFFF_FFFF'u32 + 1'u32 == 0'u32
|
|
|
|
|
|
|
|
when sizeof(uint) == 8:
|
|
|
|
check 0xFFFF_FFFF_FFFF_FFFF'u + 1'u == 0'u
|
|
|
|
else:
|
|
|
|
check 0xFFFF_FFFF'u + 1'u == 0'u
|
|
|
|
|
|
|
|
var v1_64 = 0xFFFF_FFFF_FFFF_FFFF'u64
|
|
|
|
var v2_64 = 0xFFFF_FFFF_FFFF_FFFF'u64
|
|
|
|
var v1_32 = 0xFFFF_FFFF'u32
|
|
|
|
var v2_32 = 0xFFFF_FFFF'u32
|
|
|
|
inc(v1_64)
|
|
|
|
inc(v1_32)
|
|
|
|
check:
|
|
|
|
v1_64 == 0'u64
|
|
|
|
v2_64 + 1'u64 == 0'u64
|
|
|
|
v1_32 == 0'u32
|
|
|
|
v2_32 + 1'u32 == 0'u32
|
|
|
|
|
|
|
|
when sizeof(uint) == 8:
|
|
|
|
var v1_u = 0xFFFF_FFFF_FFFF_FFFF'u
|
|
|
|
var v2_u = 0xFFFF_FFFF_FFFF_FFFF'u
|
|
|
|
inc(v1_u)
|
|
|
|
check:
|
|
|
|
v1_u == 0'u
|
|
|
|
v2_u + 1'u == 0'u
|
|
|
|
else:
|
|
|
|
var v1_u = 0xFFFF_FFFF'u
|
|
|
|
var v2_u = 0xFFFF_FFFF'u
|
|
|
|
inc(v1_u)
|
|
|
|
check:
|
|
|
|
v1_u == 0'u
|
|
|
|
v2_u + 1'u == 0'u
|
2023-09-15 16:38:39 +00:00
|
|
|
|
|
|
|
asyncTest "wait() cancellation undefined behavior test #1":
|
|
|
|
proc testInnerFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await fooFut
|
|
|
|
return TestFooConnection()
|
|
|
|
|
|
|
|
proc testFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
let connection =
|
|
|
|
try:
|
|
|
|
let res = await testInnerFoo(fooFut).wait(10.seconds)
|
|
|
|
Result[TestFooConnection, int].ok(res)
|
|
|
|
except CancelledError:
|
|
|
|
Result[TestFooConnection, int].err(0)
|
|
|
|
except CatchableError:
|
|
|
|
Result[TestFooConnection, int].err(1)
|
|
|
|
check connection.isOk()
|
|
|
|
|
|
|
|
var future = newFuture[void]("last.child.future")
|
|
|
|
var someFut = testFoo(future)
|
|
|
|
future.complete()
|
|
|
|
discard someFut.tryCancel()
|
|
|
|
await someFut
|
|
|
|
|
|
|
|
asyncTest "wait() cancellation undefined behavior test #2":
|
|
|
|
proc testInnerFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await fooFut
|
|
|
|
return TestFooConnection()
|
|
|
|
|
|
|
|
proc testMiddleFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await testInnerFoo(fooFut)
|
|
|
|
|
|
|
|
proc testFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
let connection =
|
|
|
|
try:
|
|
|
|
let res = await testMiddleFoo(fooFut).wait(10.seconds)
|
|
|
|
Result[TestFooConnection, int].ok(res)
|
|
|
|
except CancelledError:
|
|
|
|
Result[TestFooConnection, int].err(0)
|
|
|
|
except CatchableError:
|
|
|
|
Result[TestFooConnection, int].err(1)
|
|
|
|
check connection.isOk()
|
|
|
|
|
|
|
|
var future = newFuture[void]("last.child.future")
|
|
|
|
var someFut = testFoo(future)
|
|
|
|
future.complete()
|
|
|
|
discard someFut.tryCancel()
|
|
|
|
await someFut
|
|
|
|
|
2024-03-05 16:34:53 +00:00
|
|
|
asyncTest "wait() should allow cancellation test (depends on race())":
|
|
|
|
proc testFoo(): Future[bool] {.async.} =
|
|
|
|
let
|
|
|
|
resFut = sleepAsync(2.seconds).wait(3.seconds)
|
|
|
|
timeFut = sleepAsync(1.seconds)
|
|
|
|
cancelFut = cancelAndWait(resFut)
|
|
|
|
discard await race(cancelFut, timeFut)
|
|
|
|
if cancelFut.finished():
|
|
|
|
return (resFut.cancelled() and cancelFut.completed())
|
|
|
|
false
|
|
|
|
|
|
|
|
check (await testFoo()) == true
|
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
asyncTest "withTimeout() cancellation undefined behavior test #1":
|
|
|
|
proc testInnerFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await fooFut
|
|
|
|
return TestFooConnection()
|
|
|
|
|
|
|
|
proc testFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
let connection =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
checkFut = testInnerFoo(fooFut)
|
|
|
|
res = await withTimeout(checkFut, 10.seconds)
|
|
|
|
if res:
|
|
|
|
Result[TestFooConnection, int].ok(checkFut.value)
|
|
|
|
else:
|
|
|
|
Result[TestFooConnection, int].err(0)
|
|
|
|
except CancelledError:
|
|
|
|
Result[TestFooConnection, int].err(1)
|
|
|
|
except CatchableError:
|
|
|
|
Result[TestFooConnection, int].err(2)
|
|
|
|
check connection.isOk()
|
|
|
|
|
|
|
|
var future = newFuture[void]("last.child.future")
|
|
|
|
var someFut = testFoo(future)
|
|
|
|
future.complete()
|
|
|
|
discard someFut.tryCancel()
|
|
|
|
await someFut
|
|
|
|
|
|
|
|
asyncTest "withTimeout() cancellation undefined behavior test #2":
|
|
|
|
proc testInnerFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await fooFut
|
|
|
|
return TestFooConnection()
|
|
|
|
|
|
|
|
proc testMiddleFoo(fooFut: Future[void]): Future[TestFooConnection] {.
|
|
|
|
async.} =
|
|
|
|
await testInnerFoo(fooFut)
|
|
|
|
|
|
|
|
proc testFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
let connection =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
checkFut = testMiddleFoo(fooFut)
|
|
|
|
res = await withTimeout(checkFut, 10.seconds)
|
|
|
|
if res:
|
|
|
|
Result[TestFooConnection, int].ok(checkFut.value)
|
|
|
|
else:
|
|
|
|
Result[TestFooConnection, int].err(0)
|
|
|
|
except CancelledError:
|
|
|
|
Result[TestFooConnection, int].err(1)
|
|
|
|
except CatchableError:
|
|
|
|
Result[TestFooConnection, int].err(2)
|
|
|
|
check connection.isOk()
|
|
|
|
|
|
|
|
var future = newFuture[void]("last.child.future")
|
|
|
|
var someFut = testFoo(future)
|
|
|
|
future.complete()
|
|
|
|
discard someFut.tryCancel()
|
|
|
|
await someFut
|
|
|
|
|
2024-03-05 16:34:53 +00:00
|
|
|
asyncTest "withTimeout() should allow cancellation test (depends on race())":
|
|
|
|
proc testFoo(): Future[bool] {.async.} =
|
|
|
|
let
|
|
|
|
resFut = sleepAsync(2.seconds).withTimeout(3.seconds)
|
|
|
|
timeFut = sleepAsync(1.seconds)
|
|
|
|
cancelFut = cancelAndWait(resFut)
|
|
|
|
discard await race(cancelFut, timeFut)
|
|
|
|
if cancelFut.finished():
|
|
|
|
return (resFut.cancelled() and cancelFut.completed())
|
|
|
|
false
|
|
|
|
|
|
|
|
check (await testFoo()) == true
|
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
asyncTest "Cancellation behavior test":
|
|
|
|
proc testInnerFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
await fooFut
|
|
|
|
|
|
|
|
proc testMiddleFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
await testInnerFoo(fooFut)
|
|
|
|
|
|
|
|
proc testOuterFoo(fooFut: Future[void]) {.async.} =
|
|
|
|
await testMiddleFoo(fooFut)
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Future
|
|
|
|
let future = newFuture[void]("last.child.pending.future")
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of completed Future
|
|
|
|
let future = newFuture[void]("last.child.completed.future")
|
|
|
|
future.complete()
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == false
|
|
|
|
future.completed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of failed Future
|
|
|
|
let future = newFuture[void]("last.child.failed.future")
|
|
|
|
future.fail(newException(ValueError, "ABCD"))
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == false
|
|
|
|
future.failed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of already cancelled Future
|
|
|
|
let future = newFuture[void]("last.child.cancelled.future")
|
|
|
|
future.cancelAndSchedule()
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of Pending->Pending->Pending->Pending sequence
|
|
|
|
let future = newFuture[void]("last.child.pending.future")
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of Pending->Pending->Pending->Completed sequence
|
|
|
|
let future = newFuture[void]("last.child.completed.future")
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
future.complete()
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == false
|
|
|
|
testFut.completed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of Pending->Pending->Pending->Failed sequence
|
|
|
|
let future = newFuture[void]("last.child.failed.future")
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
future.fail(newException(ValueError, "ABCD"))
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == false
|
|
|
|
testFut.failed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of Pending->Pending->Pending->Cancelled sequence
|
|
|
|
let future = newFuture[void]("last.child.cancelled.future")
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
future.cancelAndSchedule()
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Future, when automatic scheduling disabled
|
|
|
|
let future = newFuture[void]("last.child.pending.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
discard
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
let cancelFut = cancelAndWait(future)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
check:
|
|
|
|
cancelFut.finished() == false
|
|
|
|
future.cancelled() == false
|
|
|
|
# Now we manually changing Future's state, so `cancelAndWait` could
|
|
|
|
# finish
|
|
|
|
future.complete()
|
|
|
|
await cancelFut
|
|
|
|
check:
|
|
|
|
cancelFut.finished() == true
|
|
|
|
future.cancelled() == false
|
|
|
|
future.finished() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Future, which will fail Future on cancellation,
|
|
|
|
# when automatic scheduling disabled
|
|
|
|
let future = newFuture[void]("last.child.completed.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.complete()
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == false
|
|
|
|
future.completed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Future, which will fail Future on cancellation,
|
|
|
|
# when automatic scheduling disabled
|
|
|
|
let future = newFuture[void]("last.child.failed.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.fail(newException(ValueError, "ABCD"))
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == false
|
|
|
|
future.failed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Future, which will fail Future on cancellation,
|
|
|
|
# when automatic scheduling disabled
|
|
|
|
let future = newFuture[void]("last.child.cancelled.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.cancelAndSchedule()
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
await cancelAndWait(future)
|
|
|
|
check:
|
|
|
|
future.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Pending->Pending->Pending->Pending, when
|
|
|
|
# automatic scheduling disabled and Future do nothing in cancellation
|
|
|
|
# callback
|
|
|
|
let future = newFuture[void]("last.child.pending.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
discard
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
let cancelFut = cancelAndWait(testFut)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
check:
|
|
|
|
cancelFut.finished() == false
|
|
|
|
testFut.cancelled() == false
|
|
|
|
future.cancelled() == false
|
|
|
|
# Now we manually changing Future's state, so `cancelAndWait` could
|
|
|
|
# finish
|
|
|
|
future.complete()
|
|
|
|
await cancelFut
|
|
|
|
check:
|
|
|
|
cancelFut.finished() == true
|
|
|
|
future.cancelled() == false
|
|
|
|
future.finished() == true
|
|
|
|
testFut.cancelled() == false
|
|
|
|
testFut.finished() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Pending->Pending->Pending->Pending, when
|
|
|
|
# automatic scheduling disabled and Future completes in cancellation
|
|
|
|
# callback
|
|
|
|
let future = newFuture[void]("last.child.pending.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.complete()
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == false
|
|
|
|
testFut.finished() == true
|
|
|
|
future.cancelled() == false
|
|
|
|
future.finished() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Pending->Pending->Pending->Pending, when
|
|
|
|
# automatic scheduling disabled and Future fails in cancellation callback
|
|
|
|
let future = newFuture[void]("last.child.pending.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.fail(newException(ValueError, "ABCD"))
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == false
|
|
|
|
testFut.failed() == true
|
|
|
|
future.cancelled() == false
|
|
|
|
future.failed() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Cancellation of pending Pending->Pending->Pending->Pending, when
|
|
|
|
# automatic scheduling disabled and Future fails in cancellation callback
|
|
|
|
let future = newFuture[void]("last.child.pending.future",
|
|
|
|
{FutureFlag.OwnCancelSchedule})
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
future.cancelAndSchedule()
|
|
|
|
future.cancelCallback = cancellation
|
|
|
|
# Note, future will never be finished in such case, until we manually not
|
|
|
|
# finish it
|
|
|
|
let testFut = testOuterFoo(future)
|
|
|
|
await cancelAndWait(testFut)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
check:
|
|
|
|
testFut.cancelled() == true
|
|
|
|
future.cancelled() == true
|
|
|
|
|
|
|
|
test "Issue #334 test":
|
|
|
|
proc test(): bool =
|
|
|
|
var testres = ""
|
|
|
|
|
|
|
|
proc a() {.async.} =
|
|
|
|
try:
|
|
|
|
await sleepAsync(seconds(1))
|
|
|
|
except CatchableError as exc:
|
|
|
|
testres.add("A")
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
proc b() {.async.} =
|
|
|
|
try:
|
|
|
|
await a()
|
|
|
|
except CatchableError as exc:
|
|
|
|
testres.add("B")
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
proc c() {.async.} =
|
|
|
|
try:
|
|
|
|
echo $(await b().withTimeout(seconds(2)))
|
|
|
|
except CatchableError as exc:
|
|
|
|
testres.add("C")
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
let x = c()
|
|
|
|
x.cancelSoon()
|
|
|
|
|
|
|
|
try:
|
|
|
|
waitFor x
|
|
|
|
except CatchableError:
|
|
|
|
testres.add("D")
|
|
|
|
|
|
|
|
testres.add("E")
|
|
|
|
|
|
|
|
waitFor sleepAsync(milliseconds(100))
|
|
|
|
|
|
|
|
testres == "ABCDE"
|
|
|
|
|
|
|
|
check test() == true
|
|
|
|
|
|
|
|
asyncTest "cancelAndWait() should be able to cancel test":
|
|
|
|
proc test1() {.async.} =
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc test2() {.async.} =
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc test3() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
await noCancel sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc test4() {.async.} =
|
|
|
|
while true:
|
|
|
|
await noCancel sleepAsync(50.milliseconds)
|
|
|
|
await sleepAsync(0.milliseconds)
|
|
|
|
|
|
|
|
proc test5() {.async.} =
|
|
|
|
while true:
|
|
|
|
await sleepAsync(0.milliseconds)
|
|
|
|
await noCancel sleepAsync(50.milliseconds)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let future1 = test1()
|
|
|
|
await cancelAndWait(future1)
|
|
|
|
let future2 = test1()
|
|
|
|
await sleepAsync(10.milliseconds)
|
|
|
|
await cancelAndWait(future2)
|
|
|
|
check:
|
|
|
|
future1.cancelled() == true
|
|
|
|
future2.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
let future1 = test2()
|
|
|
|
await cancelAndWait(future1)
|
|
|
|
let future2 = test2()
|
|
|
|
await sleepAsync(10.milliseconds)
|
|
|
|
await cancelAndWait(future2)
|
|
|
|
check:
|
|
|
|
future1.cancelled() == true
|
|
|
|
future2.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
let future1 = test3()
|
|
|
|
await cancelAndWait(future1)
|
|
|
|
let future2 = test3()
|
|
|
|
await sleepAsync(10.milliseconds)
|
|
|
|
await cancelAndWait(future2)
|
|
|
|
check:
|
|
|
|
future1.cancelled() == true
|
|
|
|
future2.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
let future1 = test4()
|
|
|
|
await cancelAndWait(future1)
|
|
|
|
let future2 = test4()
|
|
|
|
await sleepAsync(333.milliseconds)
|
|
|
|
await cancelAndWait(future2)
|
|
|
|
check:
|
|
|
|
future1.cancelled() == true
|
|
|
|
future2.cancelled() == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
let future1 = test5()
|
|
|
|
await cancelAndWait(future1)
|
|
|
|
let future2 = test5()
|
|
|
|
await sleepAsync(333.milliseconds)
|
|
|
|
await cancelAndWait(future2)
|
|
|
|
check:
|
|
|
|
future1.cancelled() == true
|
|
|
|
future2.cancelled() == true
|
2023-11-19 17:29:09 +00:00
|
|
|
test "Sink with literals":
|
|
|
|
# https://github.com/nim-lang/Nim/issues/22175
|
|
|
|
let fut = newFuture[string]()
|
|
|
|
fut.complete("test")
|
|
|
|
check:
|
|
|
|
fut.value() == "test"
|