connection closing tests

This commit is contained in:
Dmitriy Ryajov 2020-04-04 20:45:00 -06:00
parent 5a657c9264
commit 4ee1b4a66d
2 changed files with 50 additions and 2 deletions

View File

@ -286,14 +286,12 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} = proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
try: try:
dumpNumberOfInstances()
await s.upgradeIncoming(conn) # perform upgrade on incoming connection await s.upgradeIncoming(conn) # perform upgrade on incoming connection
except CatchableError as exc: except CatchableError as exc:
trace "Exception occurred in Switch.start", exc = exc.msg trace "Exception occurred in Switch.start", exc = exc.msg
finally: finally:
await conn.close() await conn.close()
await s.cleanupConn(conn) await s.cleanupConn(conn)
dumpNumberOfInstances()
var startFuts: seq[Future[void]] var startFuts: seq[Future[void]]
for t in s.transports: # for each transport for t in s.transports: # for each transport

50
tests/testconnection.nim Normal file
View File

@ -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