Re-add connection manager tests (#441)

* use table.getOrDefault()

* re-add missing connection manager tests
This commit is contained in:
Dmitriy Ryajov 2020-11-17 18:48:26 -06:00 committed by GitHub
parent 74acd0a33a
commit 8c8d73380f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 100 deletions

View File

@ -160,7 +160,7 @@ proc contains*(c: ConnManager, conn: Connection): bool =
if isNil(conn.peerInfo):
return
return conn in c.conns[conn.peerInfo.peerId]
return conn in c.conns.getOrDefault(conn.peerInfo.peerId)
proc contains*(c: ConnManager, peerId: PeerID): bool =
peerId in c.conns

View File

@ -1,10 +1,11 @@
import unittest
import unittest, sequtils
import chronos
import ../libp2p/[connmanager,
stream/connection,
crypto/crypto,
muxers/muxer,
peerinfo]
peerinfo,
errors]
import helpers
@ -23,7 +24,7 @@ suite "Connection Manager":
teardown:
checkTrackers()
test "add and retrive a connection":
asyncTest "add and retrieve a connection":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
@ -31,11 +32,13 @@ suite "Connection Manager":
connMngr.storeConn(conn)
check conn in connMngr
let peerConn = connMngr.selectConn(peer)
let peerConn = connMngr.selectConn(peer.peerId)
check peerConn == conn
check peerConn.dir == Direction.In
test "add and retrieve a muxer":
await connMngr.close()
asyncTest "add and retrieve a muxer":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
@ -49,7 +52,9 @@ suite "Connection Manager":
let peerMuxer = connMngr.selectMuxer(conn)
check peerMuxer == muxer
test "get conn with direction":
await connMngr.close()
asyncTest "get conn with direction":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn1 = Connection.init(peer, Direction.Out)
@ -60,15 +65,16 @@ suite "Connection Manager":
check conn1 in connMngr
check conn2 in connMngr
let outConn = connMngr.selectConn(peer, Direction.Out)
let inConn = connMngr.selectConn(peer, Direction.In)
let outConn = connMngr.selectConn(peer.peerId, Direction.Out)
let inConn = connMngr.selectConn(peer.peerId, Direction.In)
check outConn != inConn
check outConn.dir == Direction.Out
check inConn.dir == Direction.In
test "get muxed stream for peer":
proc test() {.async.} =
await connMngr.close()
asyncTest "get muxed stream for peer":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
@ -81,14 +87,14 @@ suite "Connection Manager":
connMngr.storeMuxer(muxer)
check muxer in connMngr
let stream = await connMngr.getMuxedStream(peer)
let stream = await connMngr.getMuxedStream(peer.peerId)
check not(isNil(stream))
check stream.peerInfo == peer
waitFor(test())
await connMngr.close()
await stream.close()
test "get stream from directed connection":
proc test() {.async.} =
asyncTest "get stream from directed connection":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
@ -101,13 +107,15 @@ suite "Connection Manager":
connMngr.storeMuxer(muxer)
check muxer in connMngr
check not(isNil((await connMngr.getMuxedStream(peer, Direction.In))))
check isNil((await connMngr.getMuxedStream(peer, Direction.Out)))
let stream1 = await connMngr.getMuxedStream(peer.peerId, Direction.In)
check not(isNil(stream1))
let stream2 = await connMngr.getMuxedStream(peer.peerId, Direction.Out)
check isNil(stream2)
waitFor(test())
await connMngr.close()
await stream1.close()
test "get stream from any connection":
proc test() {.async.} =
asyncTest "get stream from any connection":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
@ -120,30 +128,38 @@ suite "Connection Manager":
connMngr.storeMuxer(muxer)
check muxer in connMngr
check not(isNil((await connMngr.getMuxedStream(conn))))
let stream = await connMngr.getMuxedStream(conn)
check not(isNil(stream))
waitFor(test())
await connMngr.close()
await stream.close()
test "should raise on too many connections":
proc test() =
asyncTest "should raise on too many connections":
let connMngr = ConnManager.init(1)
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
connMngr.storeConn(Connection.init(peer, Direction.In))
connMngr.storeConn(Connection.init(peer, Direction.In))
connMngr.storeConn(Connection.init(peer, Direction.In))
let conns = @[
Connection.init(peer, Direction.In),
Connection.init(peer, Direction.In)]
expect TooManyConnections:
test()
connMngr.storeConn(conns[0])
connMngr.storeConn(conns[1])
test "cleanup on connection close":
proc test() {.async.} =
await connMngr.close()
await allFuturesThrowing(
allFutures(conns.mapIt( it.close() )))
asyncTest "cleanup on connection close":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
let conn = Connection.init(peer, Direction.In)
let muxer = new Muxer
muxer.connection = conn
muxer.connection = conn
connMngr.storeConn(conn)
connMngr.storeMuxer(muxer)
@ -156,10 +172,9 @@ suite "Connection Manager":
check conn notin connMngr
check muxer notin connMngr
waitFor(test())
await connMngr.close()
test "drop connections for peer":
proc test() {.async.} =
asyncTest "drop connections for peer":
let connMngr = ConnManager.init()
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
@ -177,14 +192,13 @@ suite "Connection Manager":
check conn in connMngr
check muxer in connMngr
check not(isNil(connMngr.selectConn(peer, dir)))
check not(isNil(connMngr.selectConn(peer.peerId, dir)))
check peer in connMngr.peers
await connMngr.dropPeer(peer)
check peer.peerId in connMngr
await connMngr.dropPeer(peer.peerId)
check peer notin connMngr.peers
check isNil(connMngr.selectConn(peer, Direction.In))
check isNil(connMngr.selectConn(peer, Direction.Out))
check connMngr.peers.len == 0
check peer.peerId notin connMngr
check isNil(connMngr.selectConn(peer.peerId, Direction.In))
check isNil(connMngr.selectConn(peer.peerId, Direction.Out))
waitFor(test())
await connMngr.close()

View File

@ -19,6 +19,7 @@ import testtransport,
testmultistream,
testbufferstream,
testidentify,
testconnmngr,
testswitch,
testnoise,
testpeerinfo,