From 4ee1b4a66dc2f446fdc3b42dce07970d1ffc25f0 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sat, 4 Apr 2020 20:45:00 -0600 Subject: [PATCH] connection closing tests --- libp2p/switch.nim | 2 -- tests/testconnection.nim | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/testconnection.nim diff --git a/libp2p/switch.nim b/libp2p/switch.nim index c566aae..4c33f4f 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -286,14 +286,12 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} = proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} = try: - dumpNumberOfInstances() await s.upgradeIncoming(conn) # perform upgrade on incoming connection except CatchableError as exc: trace "Exception occurred in Switch.start", exc = exc.msg finally: await conn.close() await s.cleanupConn(conn) - dumpNumberOfInstances() var startFuts: seq[Future[void]] for t in s.transports: # for each transport diff --git a/tests/testconnection.nim b/tests/testconnection.nim new file mode 100644 index 0000000..0dfb3f0 --- /dev/null +++ b/tests/testconnection.nim @@ -0,0 +1,50 @@ +import unittest +import chronos, nimcrypto/utils +import ../libp2p/[connection, + stream/lpstream, + stream/bufferstream] + +suite "Connection": + test "close": + proc test(): Future[bool] {.async.} = + var conn = newConnection(newBufferStream()) + await conn.close() + check: + conn.closed == true + + result = true + + check: + waitFor(test()) == true + + test "parent close": + proc test(): Future[bool] {.async.} = + var buf = newBufferStream() + var conn = newConnection(buf) + + await conn.close() + check: + conn.closed == true + buf.closed == true + + await sleepAsync(1.seconds) + result = true + + check: + waitFor(test()) == true + + test "child close": + proc test(): Future[bool] {.async.} = + var buf = newBufferStream() + var conn = newConnection(buf) + + await buf.close() + check: + conn.closed == true + buf.closed == true + + await sleepAsync(1.seconds) + result = true + + check: + waitFor(test()) == true