diff --git a/chronos/unittest2/asynctests.nim b/chronos/unittest2/asynctests.nim index bc703b7..758e0a6 100644 --- a/chronos/unittest2/asynctests.nim +++ b/chronos/unittest2/asynctests.nim @@ -21,9 +21,9 @@ template asyncTest*(name: string, body: untyped): untyped = template checkLeaks*(name: string): untyped = let counter = getTrackerCounter(name) - if counter.opened != counter.closed: - echo "[" & name & "] opened = ", counter.opened, - ", closed = ", counter.closed + checkpoint: + "[" & name & "] opened = " & $counter.opened & + ", closed = " & $ counter.closed check counter.opened == counter.closed template checkLeaks*(): untyped = diff --git a/tests/testserver.nim b/tests/testserver.nim index e7e834e..a63c9df 100644 --- a/tests/testserver.nim +++ b/tests/testserver.nim @@ -5,8 +5,8 @@ # Licensed under either of # Apache License, version 2.0, (LICENSE-APACHEv2) # MIT license (LICENSE-MIT) -import unittest2 -import ../chronos + +import ../chronos/unittest2/asynctests {.used.} @@ -23,6 +23,9 @@ suite "Server's test suite": CustomData = ref object test: string + teardown: + checkLeaks() + proc serveStreamClient(server: StreamServer, transp: StreamTransport) {.async.} = discard @@ -54,37 +57,47 @@ suite "Server's test suite": transp.test = "CUSTOM" result = cast[StreamTransport](transp) - proc test1(): bool = + asyncTest "Stream Server start/stop test": var ta = initTAddress("127.0.0.1:31354") var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr}) server1.start() server1.stop() server1.close() - waitFor server1.join() + await server1.join() + var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr}) server2.start() server2.stop() server2.close() - waitFor server2.join() - result = true + await server2.join() - proc test5(): bool = - var ta = initTAddress("127.0.0.1:31354") + asyncTest "Stream Server stop without start test": + var ta = initTAddress("127.0.0.1:0") var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr}) + ta = server1.localAddress() server1.stop() server1.close() - waitFor server1.join() + + await server1.join() var server2 = createStreamServer(ta, serveStreamClient, {ReuseAddr}) server2.stop() server2.close() - waitFor server2.join() - result = true + 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 - proc client1(server: CustomServer, ta: TransportAddress) {.async.} = var transp = CustomTransport() transp.test = "CLIENT" server.start() - var ptransp = await connect(ta, child = transp) + var ptransp = await connect(server.localAddress(), child = transp) var etransp = cast[CustomTransport](ptransp) doAssert(etransp.test == "CLIENT") var msg = "TEST\r\n" @@ -96,44 +109,48 @@ suite "Server's test suite": server.close() await server.join() - proc client2(server: StreamServer, - ta: TransportAddress): Future[bool] {.async.} = + 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) + server.start() - var transp = await connect(ta) + var transp = await connect(server.localAddress()) var msg = "TEST\r\n" discard await transp.write(msg) var line = await transp.readLine() - result = (line == "TESTCUSTOMDATA") + check: + line == "TESTCUSTOMDATA" transp.close() server.stop() server.close() await server.join() - 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) - doAssert(not isNil(pserver)) - waitFor client1(server, ta) - result = (server.test1 == "CONNECTION") and (server.test2 == "CUSTOM") + asyncTest "Backlog and connect cancellation": + var ta = initTAddress("127.0.0.1:0") + var server1 = createStreamServer(ta, serveStreamClient, {ReuseAddr}, backlog = 1) + ta = server1.localAddress() - 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) + 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 - test "Stream Server start/stop test": - check test1() == true - test "Stream Server stop without start test": - check test5() == true - test "Stream Server inherited object test": - check test3() == true - test "StreamServer[T] test": - check test4() == true + server1.close() + await server1.join()