nim-chronos/tests/testsoon.nim

94 lines
2.9 KiB
Nim
Raw Normal View History

# 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)
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
import ../chronos
{.used.}
2019-10-24 13:01:57 +00:00
suite "callSoon() tests suite":
test "User-defined callback argument test":
proc test(): bool =
var soonTest = 0'u
proc callback(udata: pointer) {.gcsafe.} =
soonTest = soonTest xor cast[uint](udata)
callSoon(callback, cast[pointer](0x12345678'u))
callSoon(callback, cast[pointer](0x23456789'u))
callSoon(callback, cast[pointer](0x3456789A'u))
callSoon(callback, cast[pointer](0x456789AB'u))
callSoon(callback, cast[pointer](0x56789ABC'u))
callSoon(callback, cast[pointer](0x6789ABCD'u))
callSoon(callback, cast[pointer](0x789ABCDE'u))
callSoon(callback, cast[pointer](0x89ABCDEF'u))
callSoon(callback, cast[pointer](0x9ABCDEF1'u))
callSoon(callback, cast[pointer](0xABCDEF12'u))
callSoon(callback, cast[pointer](0xBCDEF123'u))
callSoon(callback, cast[pointer](0xCDEF1234'u))
callSoon(callback, cast[pointer](0xDEF12345'u))
callSoon(callback, cast[pointer](0xEF123456'u))
callSoon(callback, cast[pointer](0xF1234567'u))
callSoon(callback, cast[pointer](0x12345678'u))
## All callbacks must be processed exactly with 1 poll() call.
poll()
2018-05-25 20:00:32 +00:00
var values = [0x12345678'u, 0x23456789'u, 0x3456789A'u, 0x456789AB'u,
0x56789ABC'u, 0x6789ABCD'u, 0x789ABCDE'u, 0x89ABCDEF'u,
0x9ABCDEF1'u, 0xABCDEF12'u, 0xBCDEF123'u, 0xCDEF1234'u,
0xDEF12345'u, 0xEF123456'u, 0xF1234567'u, 0x12345678'u]
var expect = 0'u
for item in values:
expect = expect xor item
soonTest == expect
check test() == true
2018-05-25 20:00:32 +00:00
test "`Asynchronous dead end` #7193 test":
const CallSoonTests = 5
proc test() =
var
timeoutsTest1 = 0
timeoutsTest2 = 0
stopFlag = false
var callbackproc: proc(udata: pointer) {.gcsafe, raises: [].}
callbackproc = proc (udata: pointer) {.gcsafe, raises: [].} =
timeoutsTest2 += 1
if not(stopFlag):
callSoon(callbackproc)
proc testProc() {.async.} =
for i in 1 .. CallSoonTests:
await sleepAsync(10.milliseconds)
timeoutsTest1 += 1
callSoon(callbackproc)
waitFor(testProc())
stopFlag = true
poll()
check:
timeoutsTest1 == CallSoonTests
timeoutsTest2 > CallSoonTests * 2
test()
test "`callSoon() is not working prior getGlobalDispatcher()` #7192 test":
proc test(): bool =
var soonTest = 0
proc testCallback(udata: pointer) =
soonTest = 987654321
callSoon(testCallback)
poll()
soonTest == 987654321
check test() == true