2021-05-21 16:27:01 +00:00
|
|
|
import sequtils
|
2022-09-22 19:55:59 +00:00
|
|
|
import stew/results
|
2020-07-17 15:36:48 +00:00
|
|
|
import chronos
|
|
|
|
import ../libp2p/[connmanager,
|
|
|
|
stream/connection,
|
|
|
|
crypto/crypto,
|
|
|
|
muxers/muxer,
|
2020-11-18 00:48:26 +00:00
|
|
|
peerinfo,
|
|
|
|
errors]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
import helpers
|
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
proc getConnection(peerId: PeerId, dir: Direction = Direction.In): Connection =
|
|
|
|
return Connection.new(peerId, dir, Opt.none(MultiAddress))
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
type
|
|
|
|
TestMuxer = ref object of Muxer
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId: PeerId
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
method newStream*(
|
|
|
|
m: TestMuxer,
|
|
|
|
name: string = "",
|
|
|
|
lazy: bool = false):
|
|
|
|
Future[Connection] {.async, gcsafe.} =
|
2022-09-22 19:55:59 +00:00
|
|
|
result = getConnection(m.peerId, Direction.Out)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
suite "Connection Manager":
|
|
|
|
teardown:
|
2020-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "add and retrieve a connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
check conn in connMngr
|
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerConn = connMngr.selectConn(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
check peerConn == conn
|
|
|
|
check peerConn.dir == Direction.In
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2020-12-03 01:24:48 +00:00
|
|
|
asyncTest "shouldn't allow a closed connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-12-03 01:24:48 +00:00
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "shouldn't allow an EOFed connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-12-03 01:24:48 +00:00
|
|
|
conn.isEof = true
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await connMngr.close()
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "add and retrieve a muxer":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
|
|
|
|
|
|
|
let peerMuxer = connMngr.selectMuxer(conn)
|
|
|
|
check peerMuxer == muxer
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2020-12-03 01:24:48 +00:00
|
|
|
asyncTest "shouldn't allow a muxer for an untracked connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-12-03 01:24:48 +00:00
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await muxer.close()
|
|
|
|
await connMngr.close()
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get conn with direction":
|
2023-01-23 22:28:39 +00:00
|
|
|
# This would work with 1 as well cause of a bug in connmanager that will get fixed soon
|
|
|
|
let connMngr = ConnManager.new(maxConnsPerPeer = 2)
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn1 = getConnection(peerId, Direction.Out)
|
|
|
|
let conn2 = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
connMngr.storeConn(conn1)
|
|
|
|
connMngr.storeConn(conn2)
|
|
|
|
check conn1 in connMngr
|
|
|
|
check conn2 in connMngr
|
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
let outConn = connMngr.selectConn(peerId, Direction.Out)
|
|
|
|
let inConn = connMngr.selectConn(peerId, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
check outConn != inConn
|
|
|
|
check outConn.dir == Direction.Out
|
|
|
|
check inConn.dir == Direction.In
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get muxed stream for peer":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
2021-09-08 09:07:46 +00:00
|
|
|
muxer.peerId = peerId
|
2020-11-18 00:48:26 +00:00
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
let stream = await connMngr.getStream(peerId)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream))
|
2021-09-08 09:07:46 +00:00
|
|
|
check stream.peerId == peerId
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get stream from directed connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
2021-09-08 09:07:46 +00:00
|
|
|
muxer.peerId = peerId
|
2020-11-18 00:48:26 +00:00
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
let stream1 = await connMngr.getStream(peerId, Direction.In)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream1))
|
2021-09-08 09:07:46 +00:00
|
|
|
let stream2 = await connMngr.getStream(peerId, Direction.Out)
|
2020-11-18 00:48:26 +00:00
|
|
|
check isNil(stream2)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream1.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get stream from any connection":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
2021-09-08 09:07:46 +00:00
|
|
|
muxer.peerId = peerId
|
2020-11-18 00:48:26 +00:00
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
let stream = await connMngr.getStream(conn)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "should raise on too many connections":
|
2023-01-23 22:28:39 +00:00
|
|
|
let connMngr = ConnManager.new(maxConnsPerPeer = 0)
|
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
|
|
|
|
|
|
|
connMngr.storeConn(getConnection(peerId))
|
|
|
|
|
|
|
|
let conns = @[
|
|
|
|
getConnection(peerId),
|
|
|
|
getConnection(peerId)]
|
|
|
|
|
|
|
|
expect TooManyConnectionsError:
|
|
|
|
connMngr.storeConn(conns[0])
|
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFutures(conns.mapIt( it.close() )))
|
|
|
|
|
|
|
|
asyncTest "expect connection from peer":
|
|
|
|
# FIXME This should be 1 instead of 0, it will get fixed soon
|
|
|
|
let connMngr = ConnManager.new(maxConnsPerPeer = 0)
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2020-11-18 00:48:26 +00:00
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
connMngr.storeConn(getConnection(peerId))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let conns = @[
|
2022-09-22 19:55:59 +00:00
|
|
|
getConnection(peerId),
|
|
|
|
getConnection(peerId)]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-01-21 04:00:24 +00:00
|
|
|
expect TooManyConnectionsError:
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conns[0])
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
let waitedConn1 = connMngr.expectConnection(peerId)
|
|
|
|
|
|
|
|
expect LPError:
|
|
|
|
discard await connMngr.expectConnection(peerId)
|
|
|
|
|
|
|
|
await waitedConn1.cancelAndWait()
|
|
|
|
let
|
|
|
|
waitedConn2 = connMngr.expectConnection(peerId)
|
|
|
|
waitedConn3 = connMngr.expectConnection(PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet())
|
|
|
|
conn = getConnection(peerId)
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
check (await waitedConn2) == conn
|
|
|
|
|
|
|
|
expect TooManyConnectionsError:
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conns[1])
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-01-23 22:28:39 +00:00
|
|
|
checkExpiring: waitedConn3.cancelled()
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFutures(conns.mapIt( it.close() )))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "cleanup on connection close":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId)
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new Muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
muxer.connection = conn
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check conn in connMngr
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await conn.close()
|
|
|
|
await sleepAsync(10.millis)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check conn notin connMngr
|
|
|
|
check muxer notin connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "drop connections for peer":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new()
|
2021-09-08 09:07:46 +00:00
|
|
|
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
for i in 0..<2:
|
|
|
|
let dir = if i mod 2 == 0:
|
|
|
|
Direction.In else:
|
|
|
|
Direction.Out
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = getConnection(peerId, dir)
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
|
|
|
|
check conn in connMngr
|
|
|
|
check muxer in connMngr
|
2021-09-08 09:07:46 +00:00
|
|
|
check not(isNil(connMngr.selectConn(peerId, dir)))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
check peerId in connMngr
|
|
|
|
await connMngr.dropPeer(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
check peerId notin connMngr
|
|
|
|
check isNil(connMngr.selectConn(peerId, Direction.In))
|
|
|
|
check isNil(connMngr.selectConn(peerId, Direction.Out))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
asyncTest "track total incoming connection limits":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxConnections = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2022-08-03 14:48:19 +00:00
|
|
|
check await connMngr.getIncomingSlot().withTimeout(10.millis)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should timeout adding a connection over the limit
|
2022-08-03 14:48:19 +00:00
|
|
|
check not(await connMngr.getIncomingSlot().withTimeout(10.millis))
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track total outgoing connection limits":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxConnections = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should throw adding a connection over the limit
|
|
|
|
expect TooManyConnectionsError:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track both incoming and outgoing total connections limits - fail on incoming":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxConnections = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should timeout adding a connection over the limit
|
2022-08-03 14:48:19 +00:00
|
|
|
check not(await connMngr.getIncomingSlot().withTimeout(10.millis))
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track both incoming and outgoing total connections limits - fail on outgoing":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxConnections = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2022-08-03 14:48:19 +00:00
|
|
|
check await connMngr.getIncomingSlot().withTimeout(10.millis)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should throw adding a connection over the limit
|
|
|
|
expect TooManyConnectionsError:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track max incoming connection limits":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxIn = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2022-08-03 14:48:19 +00:00
|
|
|
check await connMngr.getIncomingSlot().withTimeout(10.millis)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
2022-08-03 14:48:19 +00:00
|
|
|
check not(await connMngr.getIncomingSlot().withTimeout(10.millis))
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track max outgoing connection limits":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxOut = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should throw adding a connection over the limit
|
|
|
|
expect TooManyConnectionsError:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track incoming max connections limits - fail on incoming":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxOut = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should timeout adding a connection over the limit
|
2022-08-03 14:48:19 +00:00
|
|
|
check not(await connMngr.getIncomingSlot().withTimeout(10.millis))
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "track incoming max connections limits - fail on outgoing":
|
2021-10-25 08:26:32 +00:00
|
|
|
let connMngr = ConnManager.new(maxIn = 3)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
var conns: seq[Connection]
|
|
|
|
for i in 0..<3:
|
2022-08-03 14:48:19 +00:00
|
|
|
check await connMngr.getIncomingSlot().withTimeout(10.millis)
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
# should throw adding a connection over the limit
|
|
|
|
expect TooManyConnectionsError:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot()
|
2021-01-28 03:27:33 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
2022-02-24 16:31:47 +00:00
|
|
|
|
|
|
|
asyncTest "allow force dial":
|
|
|
|
let connMngr = ConnManager.new(maxConnections = 2)
|
|
|
|
|
|
|
|
var conns: seq[Connection]
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot(true)
|
2022-02-24 16:31:47 +00:00
|
|
|
|
|
|
|
# should throw adding a connection over the limit
|
|
|
|
expect TooManyConnectionsError:
|
2023-01-05 14:02:52 +00:00
|
|
|
discard connMngr.getOutgoingSlot(false)
|
2022-02-24 16:31:47 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
2022-08-03 14:48:19 +00:00
|
|
|
|
|
|
|
asyncTest "release slot on connection end":
|
|
|
|
let connMngr = ConnManager.new(maxConnections = 3)
|
|
|
|
|
|
|
|
var conns: seq[Connection]
|
|
|
|
for i in 0..<3:
|
2023-01-05 14:02:52 +00:00
|
|
|
let slot = connMngr.getOutgoingSlot()
|
2022-08-03 14:48:19 +00:00
|
|
|
|
|
|
|
let conn =
|
2022-09-22 19:55:59 +00:00
|
|
|
getConnection(
|
2022-08-03 14:48:19 +00:00
|
|
|
PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet(),
|
|
|
|
Direction.In)
|
|
|
|
|
|
|
|
slot.trackConnection(conn)
|
|
|
|
conns.add(conn)
|
|
|
|
|
|
|
|
# should be full now
|
|
|
|
let incomingSlot = connMngr.getIncomingSlot()
|
|
|
|
|
|
|
|
check (await incomingSlot.withTimeout(10.millis)) == false
|
|
|
|
|
2022-02-24 16:31:47 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFutures(conns.mapIt( it.close() )))
|
2022-08-03 14:48:19 +00:00
|
|
|
|
|
|
|
check await incomingSlot.withTimeout(10.millis)
|
|
|
|
|
|
|
|
await connMngr.close()
|