2019-07-06 08:16:31 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
2023-06-05 11:02:13 +00:00
|
|
|
import std/[macros, strutils]
|
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
|
2019-07-06 08:16:31 +00:00
|
|
|
import ../chronos
|
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
{.used.}
|
2019-10-24 13:01:57 +00:00
|
|
|
|
2023-01-18 14:54:39 +00:00
|
|
|
type
|
|
|
|
RetValueType = proc(n: int): Future[int] {.async.}
|
2023-01-19 06:52:11 +00:00
|
|
|
RetImplicitVoidType = proc(n: int) {.async.}
|
2023-01-18 14:54:39 +00:00
|
|
|
RetVoidType = proc(n: int): Future[void] {.async.}
|
|
|
|
|
2019-07-06 08:16:31 +00:00
|
|
|
proc asyncRetValue(n: int): Future[int] {.async.} =
|
|
|
|
await sleepAsync(n.milliseconds)
|
|
|
|
result = n * 10
|
|
|
|
|
|
|
|
proc asyncRetVoid(n: int) {.async.} =
|
|
|
|
await sleepAsync(n.milliseconds)
|
|
|
|
|
|
|
|
proc asyncRetExceptionValue(n: int): Future[int] {.async.} =
|
|
|
|
await sleepAsync(n.milliseconds)
|
|
|
|
result = n * 10
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "Test exception")
|
|
|
|
|
|
|
|
proc asyncRetExceptionVoid(n: int) {.async.} =
|
|
|
|
await sleepAsync(n.milliseconds)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "Test exception")
|
|
|
|
|
|
|
|
proc testAwait(): Future[bool] {.async.} =
|
|
|
|
var res: int
|
2023-01-18 14:54:39 +00:00
|
|
|
|
2019-07-06 08:16:31 +00:00
|
|
|
await asyncRetVoid(100)
|
|
|
|
res = await asyncRetValue(100)
|
|
|
|
if res != 1000:
|
|
|
|
return false
|
|
|
|
if (await asyncRetValue(100)) != 1000:
|
|
|
|
return false
|
|
|
|
try:
|
|
|
|
await asyncRetExceptionVoid(100)
|
|
|
|
return false
|
|
|
|
except ValueError:
|
|
|
|
discard
|
|
|
|
res = 0
|
|
|
|
try:
|
2019-10-29 21:19:41 +00:00
|
|
|
discard await asyncRetExceptionValue(100)
|
2019-07-06 08:16:31 +00:00
|
|
|
return false
|
|
|
|
except ValueError:
|
|
|
|
discard
|
|
|
|
if res != 0:
|
|
|
|
return false
|
2023-01-18 14:54:39 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let fn: RetVoidType = asyncRetVoid
|
|
|
|
await fn(100)
|
2023-01-19 06:52:11 +00:00
|
|
|
block:
|
|
|
|
let fn: RetImplicitVoidType = asyncRetVoid
|
|
|
|
await fn(100)
|
2023-01-18 14:54:39 +00:00
|
|
|
block:
|
|
|
|
let fn: RetValueType = asyncRetValue
|
|
|
|
if (await fn(100)) != 1000:
|
|
|
|
return false
|
|
|
|
|
2019-07-06 08:16:31 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
proc testAwaitne(): Future[bool] {.async.} =
|
|
|
|
var res1: Future[void]
|
|
|
|
var res2: Future[int]
|
|
|
|
|
|
|
|
res1 = awaitne asyncRetVoid(100)
|
|
|
|
res2 = awaitne asyncRetValue(100)
|
|
|
|
if res1.failed():
|
|
|
|
return false
|
|
|
|
if res2.read() != 1000:
|
|
|
|
return false
|
|
|
|
|
|
|
|
res1 = awaitne asyncRetExceptionVoid(100)
|
|
|
|
if not(res1.failed()):
|
|
|
|
return false
|
|
|
|
|
|
|
|
res2 = awaitne asyncRetExceptionValue(100)
|
|
|
|
try:
|
2019-10-29 21:19:41 +00:00
|
|
|
discard res2.read()
|
2019-07-06 08:16:31 +00:00
|
|
|
return false
|
|
|
|
except ValueError:
|
|
|
|
discard
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
suite "Macro transformations test suite":
|
|
|
|
test "`await` command test":
|
|
|
|
check waitFor(testAwait()) == true
|
|
|
|
test "`awaitne` command test":
|
|
|
|
check waitFor(testAwaitne()) == true
|
2022-03-30 13:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
test "template async macro transformation":
|
|
|
|
template templatedAsync(name, restype: untyped): untyped =
|
|
|
|
proc name(): Future[restype] {.async.} = return @[4]
|
|
|
|
|
|
|
|
templatedAsync(testTemplate, seq[int])
|
|
|
|
check waitFor(testTemplate()) == @[4]
|
|
|
|
|
|
|
|
macro macroAsync(name, restype, innerrestype: untyped): untyped =
|
|
|
|
quote do:
|
|
|
|
proc `name`(): Future[`restype`[`innerrestype`]] {.async.} = return
|
|
|
|
|
|
|
|
type OpenObject = object
|
|
|
|
macroAsync(testMacro, seq, OpenObject)
|
|
|
|
check waitFor(testMacro()).len == 0
|
2022-05-05 09:05:04 +00:00
|
|
|
|
|
|
|
macro macroAsync2(name, restype, inner1, inner2, inner3, inner4: untyped): untyped =
|
|
|
|
quote do:
|
|
|
|
proc `name`(): Future[`restype`[`inner1`[`inner2`[`inner3`, `inner4`]]]] {.async.} = return
|
|
|
|
|
|
|
|
macroAsync2(testMacro2, seq, Opt, Result, OpenObject, cstring)
|
|
|
|
check waitFor(testMacro2()).len == 0
|
2022-07-18 21:30:27 +00:00
|
|
|
|
2023-06-05 11:02:13 +00:00
|
|
|
test "Future with generics":
|
|
|
|
proc gen(T: typedesc): Future[T] {.async.} =
|
|
|
|
proc testproc(): Future[T] {.async.} =
|
|
|
|
when T is void:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return default(T)
|
|
|
|
await testproc()
|
|
|
|
|
|
|
|
waitFor gen(void)
|
|
|
|
check:
|
|
|
|
waitFor(gen(int)) == default(int)
|
|
|
|
|
|
|
|
test "Implicit return":
|
|
|
|
proc implicit(): Future[int] {.async.} =
|
|
|
|
42
|
|
|
|
|
|
|
|
proc implicit2(): Future[int] {.async.} =
|
|
|
|
block:
|
|
|
|
42
|
|
|
|
|
|
|
|
proc implicit3(): Future[int] {.async.} =
|
|
|
|
try:
|
|
|
|
parseInt("error")
|
|
|
|
except ValueError:
|
|
|
|
42
|
|
|
|
|
|
|
|
proc implicit4(v: bool): Future[int] {.async.} =
|
|
|
|
case v
|
|
|
|
of false: 5
|
|
|
|
of true: 42
|
|
|
|
|
|
|
|
proc implicit5(v: bool): Future[int] {.async.} =
|
|
|
|
if v: 42
|
|
|
|
else: 5
|
|
|
|
|
|
|
|
proc implicit6(v: ref int): Future[int] {.async.} =
|
|
|
|
try:
|
|
|
|
parseInt("error")
|
|
|
|
except ValueError:
|
|
|
|
42
|
|
|
|
finally:
|
|
|
|
v[] = 42
|
|
|
|
|
|
|
|
proc implicit7(v: bool): Future[int] {.async.} =
|
|
|
|
case v
|
|
|
|
of false: return 33
|
|
|
|
of true: 42
|
|
|
|
|
|
|
|
proc implicit8(v: bool): Future[int] {.async.} =
|
|
|
|
case v
|
|
|
|
of false: await implicit7(v)
|
|
|
|
of true: 42
|
|
|
|
|
2023-07-03 22:11:08 +00:00
|
|
|
proc implicit9(): Future[int] {.async.} =
|
|
|
|
result = 42
|
|
|
|
result
|
|
|
|
|
2023-06-05 11:02:13 +00:00
|
|
|
let fin = new int
|
|
|
|
check:
|
|
|
|
waitFor(implicit()) == 42
|
|
|
|
waitFor(implicit2()) == 42
|
|
|
|
waitFor(implicit3()) == 42
|
|
|
|
waitFor(implicit4(true)) == 42
|
|
|
|
waitFor(implicit5(true)) == 42
|
|
|
|
waitFor(implicit5(false)) == 5
|
|
|
|
waitFor(implicit6(fin)) == 42
|
|
|
|
fin[] == 42
|
|
|
|
waitFor(implicit7(true)) == 42
|
|
|
|
waitFor(implicit7(false)) == 33
|
|
|
|
|
|
|
|
waitFor(implicit8(true)) == 42
|
|
|
|
waitFor(implicit8(false)) == 33
|
|
|
|
|
2023-07-03 22:11:08 +00:00
|
|
|
waitFor(implicit9()) == 42
|
|
|
|
|
2022-11-02 07:09:15 +00:00
|
|
|
suite "Closure iterator's exception transformation issues":
|
2022-07-18 21:30:27 +00:00
|
|
|
test "Nested defer/finally not called on return":
|
|
|
|
# issue #288
|
|
|
|
# fixed by https://github.com/nim-lang/Nim/pull/19933
|
|
|
|
var answer = 0
|
|
|
|
proc a {.async.} =
|
|
|
|
try:
|
|
|
|
try:
|
2022-11-02 07:09:15 +00:00
|
|
|
await sleepAsync(0.milliseconds)
|
2022-07-18 21:30:27 +00:00
|
|
|
return
|
|
|
|
finally:
|
|
|
|
answer = 32
|
|
|
|
finally:
|
|
|
|
answer.inc(10)
|
|
|
|
waitFor(a())
|
|
|
|
check answer == 42
|
2023-05-23 16:40:52 +00:00
|
|
|
|
|
|
|
test "raise-only":
|
|
|
|
# https://github.com/status-im/nim-chronos/issues/56
|
|
|
|
proc trySync() {.async.} =
|
|
|
|
return
|
|
|
|
|
|
|
|
proc x() {.async.} =
|
|
|
|
try:
|
|
|
|
await trySync()
|
|
|
|
return
|
|
|
|
except ValueError:
|
|
|
|
discard
|
|
|
|
|
|
|
|
raiseAssert "shouldn't reach"
|
|
|
|
|
|
|
|
waitFor(x())
|
|
|
|
|