2023-05-18 08:24:17 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2023-03-30 22:16:39 +00:00
|
|
|
import std/[sequtils,tables]
|
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
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc getMuxer(peerId: PeerId, dir: Direction = Direction.In): Muxer =
|
|
|
|
return Muxer(connection: Connection.new(peerId, dir, Opt.none(MultiAddress)))
|
2022-09-22 19:55:59 +00:00
|
|
|
|
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):
|
2023-12-05 07:05:32 +00:00
|
|
|
Future[Connection] {.async.} =
|
2023-03-08 11:30:19 +00:00
|
|
|
result = Connection.new(m.peerId, Direction.Out, Opt.none(MultiAddress))
|
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
|
|
|
|
2023-03-08 11:30:19 +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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let mux = getMuxer(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(mux)
|
|
|
|
check mux in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let peerMux = connMngr.selectMuxer(peerId)
|
|
|
|
check peerMux == mux
|
|
|
|
check peerMux.connection.dir == Direction.In
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2023-03-30 22:16:39 +00:00
|
|
|
asyncTest "get all connections":
|
|
|
|
let connMngr = ConnManager.new()
|
|
|
|
|
|
|
|
let peers = toSeq(0..<2).mapIt(PeerId.random.tryGet())
|
|
|
|
let muxs = toSeq(0..<2).mapIt(getMuxer(peers[it]))
|
|
|
|
for mux in muxs: connMngr.storeMuxer(mux)
|
|
|
|
|
|
|
|
let conns = connMngr.getConnections()
|
|
|
|
let connsMux = toSeq(conns.values).mapIt(it[0])
|
|
|
|
check unorderedCompare(connsMux, muxs)
|
|
|
|
|
|
|
|
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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let mux = getMuxer(peerId)
|
|
|
|
await mux.connection.close()
|
2020-12-03 01:24:48 +00:00
|
|
|
|
|
|
|
expect CatchableError:
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(mux)
|
2020-12-03 01:24:48 +00:00
|
|
|
|
|
|
|
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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let mux = getMuxer(peerId)
|
|
|
|
mux.connection.isEof = true
|
2020-12-03 01:24:48 +00:00
|
|
|
|
|
|
|
expect CatchableError:
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(mux)
|
2020-12-03 01:24:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
await mux.close()
|
2020-12-03 01:24:48 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
asyncTest "shouldn't allow a muxer with no 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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxer = getMuxer(peerId)
|
|
|
|
let conn = muxer.connection
|
|
|
|
muxer.connection = nil
|
2020-12-03 01:24:48 +00:00
|
|
|
|
|
|
|
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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let mux1 = getMuxer(peerId, Direction.Out)
|
|
|
|
let mux2 = getMuxer(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(mux1)
|
|
|
|
connMngr.storeMuxer(mux2)
|
|
|
|
check mux1 in connMngr
|
|
|
|
check mux2 in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let outMux = connMngr.selectMuxer(peerId, Direction.Out)
|
|
|
|
let inMux = connMngr.selectMuxer(peerId, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
check outMux != inMux
|
|
|
|
check outMux == mux1
|
|
|
|
check inMux == mux2
|
|
|
|
check outMux.connection.dir == Direction.Out
|
|
|
|
check inMux.connection.dir == Direction.In
|
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 "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()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
2023-03-08 11:30:19 +00:00
|
|
|
let connection = Connection.new(peerId, Direction.In, Opt.none(MultiAddress))
|
2021-09-08 09:07:46 +00:00
|
|
|
muxer.peerId = peerId
|
2023-03-08 11:30:19 +00:00
|
|
|
muxer.connection = connection
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
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()
|
2023-03-08 11:30:19 +00:00
|
|
|
await connection.close()
|
2020-11-18 00:48:26 +00:00
|
|
|
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()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
2023-03-08 11:30:19 +00:00
|
|
|
let connection = Connection.new(peerId, Direction.In, Opt.none(MultiAddress))
|
2021-09-08 09:07:46 +00:00
|
|
|
muxer.peerId = peerId
|
2023-03-08 11:30:19 +00:00
|
|
|
muxer.connection = connection
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
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()
|
2023-03-08 11:30:19 +00:00
|
|
|
await connection.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()
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(getMuxer(peerId))
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxs = @[getMuxer(peerId)]
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
expect TooManyConnectionsError:
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(muxs[0])
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2023-03-08 11:30:19 +00:00
|
|
|
allFutures(muxs.mapIt( it.close() )))
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(getMuxer(peerId))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxs = @[
|
|
|
|
getMuxer(peerId),
|
|
|
|
getMuxer(peerId)]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2021-01-21 04:00:24 +00:00
|
|
|
expect TooManyConnectionsError:
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(muxs[0])
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2023-02-21 16:49:41 +00:00
|
|
|
let waitedConn1 = connMngr.expectConnection(peerId, In)
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2023-02-21 16:49:41 +00:00
|
|
|
expect AlreadyExpectingConnectionError:
|
|
|
|
discard await connMngr.expectConnection(peerId, In)
|
2023-01-23 22:28:39 +00:00
|
|
|
|
|
|
|
await waitedConn1.cancelAndWait()
|
|
|
|
let
|
2023-02-21 16:49:41 +00:00
|
|
|
waitedConn2 = connMngr.expectConnection(peerId, In)
|
|
|
|
waitedConn3 = connMngr.expectConnection(PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet(), In)
|
2023-03-08 11:30:19 +00:00
|
|
|
conn = getMuxer(peerId)
|
|
|
|
connMngr.storeMuxer(conn)
|
2023-01-23 22:28:39 +00:00
|
|
|
check (await waitedConn2) == conn
|
|
|
|
|
|
|
|
expect TooManyConnectionsError:
|
2023-03-08 11:30:19 +00:00
|
|
|
connMngr.storeMuxer(muxs[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
|
|
|
|
2024-02-09 10:51:27 +00:00
|
|
|
checkUntilTimeout: waitedConn3.cancelled()
|
2023-01-23 22:28:39 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await allFuturesThrowing(
|
2023-03-08 11:30:19 +00:00
|
|
|
allFutures(muxs.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()
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxer = getMuxer(peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeMuxer(muxer)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
await muxer.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2024-02-09 10:51:27 +00:00
|
|
|
checkUntilTimeout: 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
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxer = getMuxer(peerId, dir)
|
2020-11-18 00:48:26 +00:00
|
|
|
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
|
|
|
|
check muxer in connMngr
|
2023-03-08 11:30:19 +00:00
|
|
|
check not(isNil(connMngr.selectMuxer(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
|
|
|
|
2024-02-09 10:51:27 +00:00
|
|
|
checkUntilTimeout: peerId notin connMngr
|
2023-03-08 11:30:19 +00:00
|
|
|
check isNil(connMngr.selectMuxer(peerId, Direction.In))
|
|
|
|
check isNil(connMngr.selectMuxer(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
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
var muxs: seq[Muxer]
|
2022-08-03 14:48:19 +00:00
|
|
|
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
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxer =
|
|
|
|
getMuxer(
|
2022-08-03 14:48:19 +00:00
|
|
|
PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet(),
|
|
|
|
Direction.In)
|
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
slot.trackMuxer(muxer)
|
|
|
|
muxs.add(muxer)
|
2022-08-03 14:48:19 +00:00
|
|
|
|
|
|
|
# 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(
|
2023-03-08 11:30:19 +00:00
|
|
|
allFutures(muxs.mapIt( it.close() )))
|
2022-08-03 14:48:19 +00:00
|
|
|
|
|
|
|
check await incomingSlot.withTimeout(10.millis)
|
|
|
|
|
|
|
|
await connMngr.close()
|