2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-08-06 18:13:44 +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
|
2019-02-06 14:49:11 +00:00
|
|
|
import ../chronos
|
2018-08-06 18:13:44 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
{.used.}
|
2019-10-24 13:01:57 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
suite "Asynchronous issues test suite":
|
|
|
|
const HELLO_PORT = 45679
|
|
|
|
const TEST_MSG = "testmsg"
|
|
|
|
const MSG_LEN = TEST_MSG.len()
|
2023-09-15 16:38:39 +00:00
|
|
|
const TestsCount = 100
|
2018-08-06 18:13:44 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
type
|
|
|
|
CustomData = ref object
|
|
|
|
test: string
|
2018-08-06 18:13:44 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc udp4DataAvailable(transp: DatagramTransport,
|
2023-11-15 08:38:48 +00:00
|
|
|
remote: TransportAddress) {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
var udata = getUserData[CustomData](transp)
|
|
|
|
var expect = TEST_MSG
|
|
|
|
var data: seq[byte]
|
|
|
|
var datalen: int
|
|
|
|
transp.peekMessage(data, datalen)
|
|
|
|
if udata.test == "CHECK" and datalen == MSG_LEN and
|
|
|
|
equalMem(addr data[0], addr expect[0], datalen):
|
|
|
|
udata.test = "OK"
|
|
|
|
transp.close()
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert exc.msg
|
2018-08-06 18:13:44 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc issue6(): Future[bool] {.async.} =
|
|
|
|
var myself = initTAddress("127.0.0.1:" & $HELLO_PORT)
|
|
|
|
var data = CustomData()
|
|
|
|
data.test = "CHECK"
|
|
|
|
var dsock4 = newDatagramTransport(udp4DataAvailable, udata = data,
|
|
|
|
local = myself)
|
|
|
|
await dsock4.sendTo(myself, TEST_MSG, MSG_LEN)
|
|
|
|
await dsock4.join()
|
|
|
|
if data.test == "OK":
|
|
|
|
result = true
|
2018-08-06 18:13:44 +00:00
|
|
|
|
2020-01-27 18:28:44 +00:00
|
|
|
proc testWait(): Future[bool] {.async.} =
|
|
|
|
for i in 0 ..< TestsCount:
|
|
|
|
try:
|
|
|
|
await wait(sleepAsync(4.milliseconds), 4.milliseconds)
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
discard
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc testWithTimeout(): Future[bool] {.async.} =
|
|
|
|
for i in 0 ..< TestsCount:
|
|
|
|
discard await withTimeout(sleepAsync(4.milliseconds), 4.milliseconds)
|
|
|
|
result = true
|
|
|
|
|
2020-04-06 12:49:09 +00:00
|
|
|
proc testMultipleAwait(): Future[bool] {.async.} =
|
|
|
|
var promise = newFuture[void]()
|
|
|
|
var checkstr = ""
|
|
|
|
|
|
|
|
proc believers(name: string) {.async.} =
|
|
|
|
await promise
|
|
|
|
checkstr = checkstr & name
|
|
|
|
|
2021-05-07 20:52:24 +00:00
|
|
|
asyncSpawn believers("Foo")
|
|
|
|
asyncSpawn believers("Bar")
|
|
|
|
asyncSpawn believers("Baz")
|
2020-04-06 12:49:09 +00:00
|
|
|
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
promise.complete()
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
result = (checkstr == "FooBarBaz")
|
|
|
|
|
2020-06-24 10:03:36 +00:00
|
|
|
proc testDefer(): Future[bool] {.async.} =
|
|
|
|
proc someConnect() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc someClose() {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
proc testFooFails(): Future[bool] {.async.} =
|
|
|
|
await someConnect()
|
|
|
|
defer:
|
|
|
|
await someClose()
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc testFooSucceed(): Future[bool] {.async.} =
|
|
|
|
try:
|
|
|
|
await someConnect()
|
|
|
|
finally:
|
|
|
|
await someClose()
|
|
|
|
result = true
|
|
|
|
|
|
|
|
let r1 = await testFooFails()
|
|
|
|
let r2 = await testFooSucceed()
|
|
|
|
|
|
|
|
result = r1 and r2
|
|
|
|
|
2022-09-16 20:34:18 +00:00
|
|
|
proc createBigMessage(size: int): seq[byte] =
|
|
|
|
var message = "MESSAGE"
|
|
|
|
var res = newSeq[byte](size)
|
|
|
|
for i in 0 ..< len(result):
|
|
|
|
res[i] = byte(message[i mod len(message)])
|
|
|
|
res
|
|
|
|
|
|
|
|
proc testIndexError(): Future[bool] {.async.} =
|
|
|
|
var server = createStreamServer(initTAddress("127.0.0.1:0"),
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
let messageSize = DefaultStreamBufferSize * 4
|
|
|
|
var buffer = newSeq[byte](messageSize)
|
|
|
|
let msg = createBigMessage(messageSize)
|
|
|
|
let address = server.localAddress()
|
|
|
|
let afut = server.accept()
|
|
|
|
let outTransp = await connect(address)
|
|
|
|
let inpTransp = await afut
|
|
|
|
let bytesSent = await outTransp.write(msg)
|
|
|
|
check bytesSent == messageSize
|
2023-04-30 17:09:36 +00:00
|
|
|
var rfut {.used.} = inpTransp.readExactly(addr buffer[0], messageSize)
|
2022-09-16 20:34:18 +00:00
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
proc waiterProc(udata: pointer) {.raises: [], gcsafe.} =
|
2022-09-16 20:34:18 +00:00
|
|
|
try:
|
|
|
|
waitFor(sleepAsync(0.milliseconds))
|
2023-04-30 17:09:36 +00:00
|
|
|
except CatchableError:
|
2022-09-16 20:34:18 +00:00
|
|
|
raiseAssert "Unexpected exception happened"
|
2023-04-30 17:09:36 +00:00
|
|
|
let timer {.used.} = setTimer(Moment.fromNow(0.seconds), waiterProc, nil)
|
2022-09-16 20:34:18 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
|
|
|
await inpTransp.closeWait()
|
|
|
|
await outTransp.closeWait()
|
|
|
|
await server.closeWait()
|
|
|
|
return true
|
|
|
|
|
2024-03-05 16:33:46 +00:00
|
|
|
proc testOrDeadlock(): Future[bool] {.async.} =
|
|
|
|
proc f(): Future[void] {.async.} =
|
|
|
|
await sleepAsync(2.seconds) or sleepAsync(1.seconds)
|
|
|
|
let fx = f()
|
|
|
|
try:
|
|
|
|
await fx.cancelAndWait().wait(2.seconds)
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
test "Issue #6":
|
2020-01-27 18:28:44 +00:00
|
|
|
check waitFor(issue6()) == true
|
|
|
|
|
|
|
|
test "Callback-race double completion [wait()] test":
|
|
|
|
check waitFor(testWait()) == true
|
|
|
|
|
|
|
|
test "Callback-race double completion [withTimeout()] test":
|
|
|
|
check waitFor(testWithTimeout()) == true
|
2020-04-06 12:49:09 +00:00
|
|
|
|
|
|
|
test "Multiple await on single future test [Nim's issue #13889]":
|
|
|
|
check waitFor(testMultipleAwait()) == true
|
2020-06-24 10:03:36 +00:00
|
|
|
|
|
|
|
test "Defer for asynchronous procedures test [Nim's issue #13899]":
|
|
|
|
check waitFor(testDefer()) == true
|
2022-09-16 20:34:18 +00:00
|
|
|
|
|
|
|
test "IndexError crash test":
|
|
|
|
check waitFor(testIndexError()) == true
|
2024-03-05 16:33:46 +00:00
|
|
|
|
|
|
|
test "`or` deadlock [#516] test":
|
|
|
|
check waitFor(testOrDeadlock()) == true
|