2020-07-06 06:33:13 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2020-Present
|
|
|
|
# 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
|
2020-07-06 06:33:13 +00:00
|
|
|
import ../chronos
|
|
|
|
|
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
|
|
|
suite "Asynchronous utilities test suite":
|
2020-07-08 16:48:01 +00:00
|
|
|
when defined(chronosFutureTracking):
|
2021-10-21 14:22:11 +00:00
|
|
|
proc getCount(): uint =
|
2020-07-08 16:48:01 +00:00
|
|
|
# This procedure counts number of Future[T] in double-linked list via list
|
|
|
|
# iteration.
|
2021-10-21 14:22:11 +00:00
|
|
|
var res = 0'u
|
2020-07-08 16:48:01 +00:00
|
|
|
for item in pendingFutures():
|
2021-10-21 14:22:11 +00:00
|
|
|
inc(res)
|
|
|
|
res
|
2020-07-06 06:33:13 +00:00
|
|
|
|
|
|
|
test "Future clean and leaks test":
|
2020-07-08 16:48:01 +00:00
|
|
|
when defined(chronosFutureTracking):
|
2021-10-21 14:22:11 +00:00
|
|
|
if pendingFuturesCount(WithoutFinished) == 0'u:
|
|
|
|
if pendingFuturesCount(OnlyFinished) > 0'u:
|
2020-07-08 16:48:01 +00:00
|
|
|
poll()
|
2021-10-21 14:22:11 +00:00
|
|
|
check pendingFuturesCount() == 0'u
|
2020-07-08 16:48:01 +00:00
|
|
|
else:
|
|
|
|
echo dumpPendingFutures()
|
|
|
|
check false
|
2020-07-06 06:33:13 +00:00
|
|
|
else:
|
2020-07-08 16:48:01 +00:00
|
|
|
skip()
|
2020-07-06 06:33:13 +00:00
|
|
|
|
|
|
|
test "FutureList basics test":
|
2020-07-08 16:48:01 +00:00
|
|
|
when defined(chronosFutureTracking):
|
|
|
|
var fut1 = newFuture[void]()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 1'u
|
|
|
|
pendingFuturesCount() == 1'u
|
2020-07-08 16:48:01 +00:00
|
|
|
var fut2 = newFuture[void]()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 2'u
|
|
|
|
pendingFuturesCount() == 2'u
|
2020-07-08 16:48:01 +00:00
|
|
|
var fut3 = newFuture[void]()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 3'u
|
|
|
|
pendingFuturesCount() == 3'u
|
2020-07-08 16:48:01 +00:00
|
|
|
fut1.complete()
|
|
|
|
poll()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 2'u
|
|
|
|
pendingFuturesCount() == 2'u
|
2020-07-08 16:48:01 +00:00
|
|
|
fut2.fail(newException(ValueError, ""))
|
|
|
|
poll()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 1'u
|
|
|
|
pendingFuturesCount() == 1'u
|
2020-07-08 16:48:01 +00:00
|
|
|
fut3.cancel()
|
|
|
|
poll()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 0'u64
|
|
|
|
pendingFuturesCount() == 0'u64
|
2020-07-08 16:48:01 +00:00
|
|
|
else:
|
|
|
|
skip()
|
2020-07-06 06:33:13 +00:00
|
|
|
|
|
|
|
test "FutureList async procedure test":
|
2020-07-08 16:48:01 +00:00
|
|
|
when defined(chronosFutureTracking):
|
|
|
|
proc simpleProc() {.async.} =
|
|
|
|
await sleepAsync(10.milliseconds)
|
2020-07-06 06:33:13 +00:00
|
|
|
|
2020-07-08 16:48:01 +00:00
|
|
|
var fut = simpleProc()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 2'u64
|
|
|
|
pendingFuturesCount() == 2'u64
|
2020-07-06 06:33:13 +00:00
|
|
|
|
2020-07-08 16:48:01 +00:00
|
|
|
waitFor fut
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 1'u64
|
|
|
|
pendingFuturesCount() == 1'u64
|
2020-07-06 06:33:13 +00:00
|
|
|
|
2020-07-08 16:48:01 +00:00
|
|
|
poll()
|
|
|
|
check:
|
2021-10-21 14:22:11 +00:00
|
|
|
getCount() == 0'u64
|
|
|
|
pendingFuturesCount() == 0'u64
|
2020-07-08 16:48:01 +00:00
|
|
|
else:
|
|
|
|
skip()
|