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)
|
2023-09-05 10:41:52 +00:00
|
|
|
|
|
|
|
import ../chronos/unittest2/asynctests
|
2018-05-23 11:03:39 +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 "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
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
teardown:
|
|
|
|
checkLeaks()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveStreamClient(server: StreamServer,
|
2023-11-15 08:38:48 +00:00
|
|
|
transp: StreamTransport) {.async: (raises: []).} =
|
2019-03-30 22:31:10 +00:00
|
|
|
discard
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveCustomStreamClient(server: StreamServer,
|
2023-11-15 08:38:48 +00:00
|
|
|
transp: StreamTransport) {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
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()
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert exc.msg
|
|
|
|
|
2018-05-23 11:03:39 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc serveUdataStreamClient(server: StreamServer,
|
2023-11-15 08:38:48 +00:00
|
|
|
transp: StreamTransport) {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
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()
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert exc.msg
|
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
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
asyncTest "Stream Server start/stop test":
|
2019-03-30 22:31:10 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:31354")
|
|
|
|
var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server1.start()
|
|
|
|
server1.stop()
|
|
|
|
server1.close()
|
2023-09-05 10:41:52 +00:00
|
|
|
await server1.join()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server2.start()
|
|
|
|
server2.stop()
|
|
|
|
server2.close()
|
2023-09-05 10:41:52 +00:00
|
|
|
await server2.join()
|
2018-06-10 23:08:17 +00:00
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
asyncTest "Stream Server stop without start test":
|
|
|
|
var ta = initTAddress("127.0.0.1:0")
|
2019-05-09 17:59:15 +00:00
|
|
|
var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
2023-09-05 10:41:52 +00:00
|
|
|
ta = server1.localAddress()
|
2019-05-09 17:59:15 +00:00
|
|
|
server1.stop()
|
|
|
|
server1.close()
|
2023-09-05 10:41:52 +00:00
|
|
|
|
|
|
|
await server1.join()
|
2019-05-09 17:59:15 +00:00
|
|
|
var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr})
|
|
|
|
server2.stop()
|
|
|
|
server2.close()
|
2023-09-05 10:41:52 +00:00
|
|
|
await server2.join()
|
|
|
|
|
|
|
|
asyncTest "Stream Server inherited object test":
|
|
|
|
var server = CustomServer()
|
|
|
|
server.test1 = "TEST"
|
|
|
|
var ta = initTAddress("127.0.0.1:0")
|
|
|
|
var pserver = createStreamServer(ta, serveCustomStreamClient, {ReuseAddr},
|
|
|
|
child = server,
|
|
|
|
init = customServerTransport)
|
|
|
|
check:
|
|
|
|
pserver == server
|
2019-05-09 17:59:15 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
var transp = CustomTransport()
|
|
|
|
transp.test = "CLIENT"
|
|
|
|
server.start()
|
2023-09-05 10:41:52 +00:00
|
|
|
var ptransp = await connect(server.localAddress(), child = transp)
|
2019-03-30 22:31:10 +00:00
|
|
|
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
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
check:
|
|
|
|
server.test1 == "CONNECTION"
|
|
|
|
server.test2 == "CUSTOM"
|
|
|
|
|
|
|
|
asyncTest "StreamServer[T] test":
|
|
|
|
var co = CustomData()
|
|
|
|
co.test = "CUSTOMDATA"
|
|
|
|
var ta = initTAddress("127.0.0.1:0")
|
|
|
|
var server = createStreamServer(ta, serveUdataStreamClient, {ReuseAddr},
|
|
|
|
udata = co)
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
server.start()
|
2023-09-05 10:41:52 +00:00
|
|
|
var transp = await connect(server.localAddress())
|
2019-03-30 22:31:10 +00:00
|
|
|
var msg = "TEST\r\n"
|
|
|
|
discard await transp.write(msg)
|
|
|
|
var line = await transp.readLine()
|
2023-09-05 10:41:52 +00:00
|
|
|
check:
|
|
|
|
line == "TESTCUSTOMDATA"
|
2019-03-30 22:31:10 +00:00
|
|
|
transp.close()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
asyncTest "Backlog and connect cancellation":
|
|
|
|
var ta = initTAddress("127.0.0.1:0")
|
|
|
|
var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr}, backlog = 1)
|
|
|
|
ta = server1.localAddress()
|
|
|
|
|
|
|
|
var clients: seq[Future[StreamTransport]]
|
|
|
|
for i in 0..<10:
|
|
|
|
clients.add(connect(server1.localAddress))
|
|
|
|
|
|
|
|
# Check for leaks in cancellation / connect when server is not accepting
|
|
|
|
for c in clients:
|
|
|
|
if not c.finished:
|
|
|
|
await c.cancelAndWait()
|
|
|
|
else:
|
|
|
|
# The backlog connection "should" end up here
|
|
|
|
try:
|
|
|
|
await c.read().closeWait()
|
|
|
|
except CatchableError:
|
|
|
|
discard
|
2018-06-11 19:54:08 +00:00
|
|
|
|
2023-09-05 10:41:52 +00:00
|
|
|
server1.close()
|
|
|
|
await server1.join()
|