Re-add connection manager tests (#441)
* use table.getOrDefault() * re-add missing connection manager tests
This commit is contained in:
parent
74acd0a33a
commit
8c8d73380f
|
@ -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
|
||||||
|
|
|
@ -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,15 +65,16 @@ 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.} =
|
|
||||||
|
asyncTest "get muxed stream for peer":
|
||||||
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)
|
||||||
|
@ -81,14 +87,14 @@ suite "Connection Manager":
|
||||||
connMngr.storeMuxer(muxer)
|
connMngr.storeMuxer(muxer)
|
||||||
check muxer in connMngr
|
check muxer in connMngr
|
||||||
|
|
||||||
let stream = await connMngr.getMuxedStream(peer)
|
let stream = await connMngr.getMuxedStream(peer.peerId)
|
||||||
check not(isNil(stream))
|
check not(isNil(stream))
|
||||||
check stream.peerInfo == peer
|
check stream.peerInfo == peer
|
||||||
|
|
||||||
waitFor(test())
|
await connMngr.close()
|
||||||
|
await stream.close()
|
||||||
|
|
||||||
test "get stream from directed connection":
|
asyncTest "get stream from directed connection":
|
||||||
proc test() {.async.} =
|
|
||||||
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)
|
||||||
|
@ -101,13 +107,15 @@ suite "Connection Manager":
|
||||||
connMngr.storeMuxer(muxer)
|
connMngr.storeMuxer(muxer)
|
||||||
check muxer in connMngr
|
check muxer in connMngr
|
||||||
|
|
||||||
check not(isNil((await connMngr.getMuxedStream(peer, Direction.In))))
|
let stream1 = await connMngr.getMuxedStream(peer.peerId, Direction.In)
|
||||||
check isNil((await connMngr.getMuxedStream(peer, Direction.Out)))
|
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":
|
asyncTest "get stream from any connection":
|
||||||
proc test() {.async.} =
|
|
||||||
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)
|
||||||
|
@ -120,30 +128,38 @@ suite "Connection Manager":
|
||||||
connMngr.storeMuxer(muxer)
|
connMngr.storeMuxer(muxer)
|
||||||
check muxer in connMngr
|
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":
|
asyncTest "should raise on too many connections":
|
||||||
proc test() =
|
|
||||||
let connMngr = ConnManager.init(1)
|
let connMngr = ConnManager.init(1)
|
||||||
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
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))
|
|
||||||
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.} =
|
|
||||||
|
await allFuturesThrowing(
|
||||||
|
allFutures(conns.mapIt( it.close() )))
|
||||||
|
|
||||||
|
asyncTest "cleanup on connection close":
|
||||||
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)
|
||||||
let muxer = new Muxer
|
let muxer = new Muxer
|
||||||
muxer.connection = conn
|
|
||||||
|
|
||||||
|
muxer.connection = conn
|
||||||
connMngr.storeConn(conn)
|
connMngr.storeConn(conn)
|
||||||
connMngr.storeMuxer(muxer)
|
connMngr.storeMuxer(muxer)
|
||||||
|
|
||||||
|
@ -156,10 +172,9 @@ suite "Connection Manager":
|
||||||
check conn notin connMngr
|
check conn notin connMngr
|
||||||
check muxer notin connMngr
|
check muxer notin connMngr
|
||||||
|
|
||||||
waitFor(test())
|
await connMngr.close()
|
||||||
|
|
||||||
test "drop connections for peer":
|
asyncTest "drop connections for peer":
|
||||||
proc test() {.async.} =
|
|
||||||
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())
|
||||||
|
|
||||||
|
@ -177,14 +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, dir)))
|
check not(isNil(connMngr.selectConn(peer.peerId, dir)))
|
||||||
|
|
||||||
check peer in connMngr.peers
|
check peer.peerId in connMngr
|
||||||
await connMngr.dropPeer(peer)
|
await connMngr.dropPeer(peer.peerId)
|
||||||
|
|
||||||
check peer notin connMngr.peers
|
check peer.peerId notin connMngr
|
||||||
check isNil(connMngr.selectConn(peer, Direction.In))
|
check isNil(connMngr.selectConn(peer.peerId, Direction.In))
|
||||||
check isNil(connMngr.selectConn(peer, Direction.Out))
|
check isNil(connMngr.selectConn(peer.peerId, Direction.Out))
|
||||||
check connMngr.peers.len == 0
|
|
||||||
|
|
||||||
waitFor(test())
|
await connMngr.close()
|
||||||
|
|
|
@ -19,6 +19,7 @@ import testtransport,
|
||||||
testmultistream,
|
testmultistream,
|
||||||
testbufferstream,
|
testbufferstream,
|
||||||
testidentify,
|
testidentify,
|
||||||
|
testconnmngr,
|
||||||
testswitch,
|
testswitch,
|
||||||
testnoise,
|
testnoise,
|
||||||
testpeerinfo,
|
testpeerinfo,
|
||||||
|
|
Loading…
Reference in New Issue