2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-23 11:03:39 +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-05-23 11:03:39 +00:00
|
|
|
|
2019-10-24 13:01:57 +00:00
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
suite "Server's test suite":
|
|
|
|
type
|
|
|
|
CustomServer = ref object of StreamServer
|
|
|
|
test1: string
|
|
|
|
test2: string
|
|
|
|
test3: string
|
|
|
|
|
|
|
|
CustomTransport = ref object of StreamTransport
|
|
|
|
test: string
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
CustomData = ref object
|
|
|
|
test: string
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveStreamClient(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async.} =
|
|
|
|
discard
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveCustomStreamClient(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async.} =
|
|
|
|
var cserver = cast[CustomServer](server)
|
|
|
|
var ctransp = cast[CustomTransport](transp)
|
|
|
|
cserver.test1 = "CONNECTION"
|
|
|
|
cserver.test2 = ctransp.test
|
|
|
|
cserver.test3 = await transp.readLine()
|
|
|
|
var answer = "ANSWER\r\n"
|
|
|
|
discard await transp.write(answer)
|
|
|
|
transp.close()
|
|
|
|
await transp.join()
|
2018-05-23 11:03:39 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveUdataStreamClient(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async.} =
|
|
|
|
var udata = getUserData[CustomData](server)
|
|
|
|
var line = await transp.readLine()
|
|
|
|
var msg = line & udata.test & "\r\n"
|
|
|
|
discard await transp.write(msg)
|
|
|
|
transp.close()
|
|
|
|
await transp.join()
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc customServerTransport(server: StreamServer,
|
|
|
|
fd: AsyncFD): StreamTransport =
|
|
|
|
var transp = CustomTransport()
|
|
|
|
transp.test = "CUSTOM"
|
|
|
|
result = cast[StreamTransport](transp)
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc test1(): bool =
|
|
|
|
var ta = initTAddress("127.0.0.1:31354")
|
|
|
|
var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server1.start()
|
|
|
|
server1.stop()
|
|
|
|
server1.close()
|
|
|
|
waitFor server1.join()
|
|
|
|
var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server2.start()
|
|
|
|
server2.stop()
|
|
|
|
server2.close()
|
|
|
|
waitFor server2.join()
|
|
|
|
result = true
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2019-05-09 17:59:15 +00:00
|
|
|
proc test5(): bool =
|
|
|
|
var ta = initTAddress("127.0.0.1:31354")
|
|
|
|
var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server1.stop()
|
|
|
|
server1.close()
|
|
|
|
waitFor server1.join()
|
|
|
|
var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server2.stop()
|
|
|
|
server2.close()
|
|
|
|
waitFor server2.join()
|
|
|
|
result = true
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client1(server: CustomServer, ta: TransportAddress) {.async.} =
|
|
|
|
var transp = CustomTransport()
|
|
|
|
transp.test = "CLIENT"
|
|
|
|
server.start()
|
|
|
|
var ptransp = await connect(ta, child = transp)
|
|
|
|
var etransp = cast[CustomTransport](ptransp)
|
|
|
|
doAssert(etransp.test == "CLIENT")
|
|
|
|
var msg = "TEST\r\n"
|
|
|
|
discard await transp.write(msg)
|
|
|
|
var line = await transp.readLine()
|
2019-10-29 21:19:41 +00:00
|
|
|
doAssert(len(line) > 0)
|
2019-03-30 22:31:10 +00:00
|
|
|
transp.close()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2018-05-23 11:03:39 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client2(server: StreamServer,
|
|
|
|
ta: TransportAddress): Future[bool] {.async.} =
|
|
|
|
server.start()
|
|
|
|
var transp = await connect(ta)
|
|
|
|
var msg = "TEST\r\n"
|
|
|
|
discard await transp.write(msg)
|
|
|
|
var line = await transp.readLine()
|
|
|
|
result = (line == "TESTCUSTOMDATA")
|
|
|
|
transp.close()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc test3(): bool =
|
|
|
|
var server = CustomServer()
|
|
|
|
server.test1 = "TEST"
|
|
|
|
var ta = initTAddress("127.0.0.1:31354")
|
|
|
|
var pserver = createStreamServer(ta, serveCustomStreamClient, {ReuseAddr},
|
|
|
|
child = cast[StreamServer](server),
|
|
|
|
init = customServerTransport)
|
2019-10-29 21:19:41 +00:00
|
|
|
doAssert(not isNil(pserver))
|
2019-03-30 22:31:10 +00:00
|
|
|
waitFor client1(server, ta)
|
|
|
|
result = (server.test1 == "CONNECTION") and (server.test2 == "CUSTOM")
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc test4(): bool =
|
|
|
|
var co = CustomData()
|
|
|
|
co.test = "CUSTOMDATA"
|
|
|
|
var ta = initTAddress("127.0.0.1:31354")
|
|
|
|
var server = createStreamServer(ta, serveUdataStreamClient, {ReuseAddr},
|
|
|
|
udata = co)
|
|
|
|
result = waitFor client2(server, ta)
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
test "Stream Server start/stop test":
|
|
|
|
check test1() == true
|
2019-05-09 17:59:15 +00:00
|
|
|
test "Stream Server stop without start test":
|
|
|
|
check test5() == true
|
2019-03-30 22:31:10 +00:00
|
|
|
test "Stream Server inherited object test":
|
|
|
|
check test3() == true
|
|
|
|
test "StreamServer[T] test":
|
|
|
|
check test4() == true
|