diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 55ccaf650..0002e39a4 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -38,6 +38,47 @@ suite "Switch": check tracker.isLeaked() == false test "e2e use switch dial proto string": + proc testSwitch() {.async, gcsafe.} = + let done = newFuture[void]() + proc handle(conn: Connection, proto: string) {.async, gcsafe.} = + try: + let msg = string.fromBytes(await conn.readLp(1024)) + check "Hello!" == msg + await conn.writeLp("Hello!") + finally: + await conn.close() + done.complete() + + let testProto = new TestProto + testProto.codec = TestCodec + testProto.handler = handle + + let switch1 = newStandardSwitch() + switch1.mount(testProto) + + let switch2 = newStandardSwitch() + var awaiters: seq[Future[void]] + awaiters.add(await switch1.start()) + awaiters.add(await switch2.start()) + + let conn = await switch2.dial(switch1.peerInfo, TestCodec) + await conn.writeLp("Hello!") + let msg = string.fromBytes(await conn.readLp(1024)) + check "Hello!" == msg + await conn.close() + + await all( + done.wait(5.seconds), #[if OK won't happen!!]# + switch1.stop(), + switch2.stop(), + ) + + # this needs to go at end + await all(awaiters) + + waitFor(testSwitch()) + + test "e2e should not leak bufferstreams and connections on channel close": proc testSwitch() {.async, gcsafe.} = let done = newFuture[void]() proc handle(conn: Connection, proto: string) {.async, gcsafe.} =