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): if isNil(conn.peerInfo):
return 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 = proc contains*(c: ConnManager, peerId: PeerID): bool =
peerId in c.conns peerId in c.conns

View File

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

View File

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