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