-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtestconnmngr.nim
384 lines (269 loc) · 11.1 KB
/
testconnmngr.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{.used.}
# Nim-Libp2p
# Copyright (c) 2023-2024 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.
import std/[sequtils, tables]
import stew/results
import chronos
import
../libp2p/
[connmanager, stream/connection, crypto/crypto, muxers/muxer, peerinfo, errors]
import helpers
proc getMuxer(peerId: PeerId, dir: Direction = Direction.In): Muxer =
return Muxer(connection: Connection.new(peerId, dir, Opt.none(MultiAddress)))
type TestMuxer = ref object of Muxer
peerId: PeerId
method newStream*(
m: TestMuxer, name: string = "", lazy: bool = false
): Future[Connection] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} =
Connection.new(m.peerId, Direction.Out, Opt.none(MultiAddress))
suite "Connection Manager":
teardown:
checkTrackers()
asyncTest "add and retrieve a muxer":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let mux = getMuxer(peerId)
connMngr.storeMuxer(mux)
check mux in connMngr
let peerMux = connMngr.selectMuxer(peerId)
check peerMux == mux
check peerMux.connection.dir == Direction.In
await connMngr.close()
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()
asyncTest "shouldn't allow a closed connection":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let mux = getMuxer(peerId)
await mux.connection.close()
expect CatchableError:
connMngr.storeMuxer(mux)
await connMngr.close()
asyncTest "shouldn't allow an EOFed connection":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let mux = getMuxer(peerId)
mux.connection.isEof = true
expect CatchableError:
connMngr.storeMuxer(mux)
await mux.close()
await connMngr.close()
asyncTest "shouldn't allow a muxer with no connection":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let muxer = getMuxer(peerId)
let conn = muxer.connection
muxer.connection = nil
expect CatchableError:
connMngr.storeMuxer(muxer)
await conn.close()
await muxer.close()
await connMngr.close()
asyncTest "get conn with direction":
# This would work with 1 as well cause of a bug in connmanager that will get fixed soon
let connMngr = ConnManager.new(maxConnsPerPeer = 2)
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let mux1 = getMuxer(peerId, Direction.Out)
let mux2 = getMuxer(peerId)
connMngr.storeMuxer(mux1)
connMngr.storeMuxer(mux2)
check mux1 in connMngr
check mux2 in connMngr
let outMux = connMngr.selectMuxer(peerId, Direction.Out)
let inMux = connMngr.selectMuxer(peerId, Direction.In)
check outMux != inMux
check outMux == mux1
check inMux == mux2
check outMux.connection.dir == Direction.Out
check inMux.connection.dir == Direction.In
await connMngr.close()
asyncTest "get muxed stream for peer":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let muxer = new TestMuxer
let connection = Connection.new(peerId, Direction.In, Opt.none(MultiAddress))
muxer.peerId = peerId
muxer.connection = connection
connMngr.storeMuxer(muxer)
check muxer in connMngr
let stream = await connMngr.getStream(peerId)
check not (isNil(stream))
check stream.peerId == peerId
await connMngr.close()
await connection.close()
await stream.close()
asyncTest "get stream from directed connection":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let muxer = new TestMuxer
let connection = Connection.new(peerId, Direction.In, Opt.none(MultiAddress))
muxer.peerId = peerId
muxer.connection = connection
connMngr.storeMuxer(muxer)
check muxer in connMngr
let stream1 = await connMngr.getStream(peerId, Direction.In)
check not (isNil(stream1))
let stream2 = await connMngr.getStream(peerId, Direction.Out)
check isNil(stream2)
await connMngr.close()
await stream1.close()
await connection.close()
asyncTest "should raise on too many connections":
let connMngr = ConnManager.new(maxConnsPerPeer = 0)
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
connMngr.storeMuxer(getMuxer(peerId))
let muxs = @[getMuxer(peerId)]
expect TooManyConnectionsError:
connMngr.storeMuxer(muxs[0])
await connMngr.close()
await allFuturesThrowing(allFutures(muxs.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)
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
connMngr.storeMuxer(getMuxer(peerId))
let muxs = @[getMuxer(peerId), getMuxer(peerId)]
expect TooManyConnectionsError:
connMngr.storeMuxer(muxs[0])
let waitedConn1 = connMngr.expectConnection(peerId, In)
expect AlreadyExpectingConnectionError:
discard await connMngr.expectConnection(peerId, In)
await waitedConn1.cancelAndWait()
let
waitedConn2 = connMngr.expectConnection(peerId, In)
waitedConn3 = connMngr.expectConnection(
PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet(), In
)
conn = getMuxer(peerId)
connMngr.storeMuxer(conn)
check (await waitedConn2) == conn
expect TooManyConnectionsError:
connMngr.storeMuxer(muxs[1])
await connMngr.close()
checkUntilTimeout:
waitedConn3.cancelled()
await allFuturesThrowing(allFutures(muxs.mapIt(it.close())))
asyncTest "cleanup on connection close":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
let muxer = getMuxer(peerId)
connMngr.storeMuxer(muxer)
check muxer in connMngr
await muxer.close()
checkUntilTimeout:
muxer notin connMngr
await connMngr.close()
asyncTest "drop connections for peer":
let connMngr = ConnManager.new()
let peerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
for i in 0 ..< 2:
let dir = if i mod 2 == 0: Direction.In else: Direction.Out
let muxer = getMuxer(peerId, dir)
connMngr.storeMuxer(muxer)
check muxer in connMngr
check not (isNil(connMngr.selectMuxer(peerId, dir)))
check peerId in connMngr
await connMngr.dropPeer(peerId)
checkUntilTimeout:
peerId notin connMngr
check isNil(connMngr.selectMuxer(peerId, Direction.In))
check isNil(connMngr.selectMuxer(peerId, Direction.Out))
await connMngr.close()
asyncTest "track total incoming connection limits":
let connMngr = ConnManager.new(maxConnections = 3)
for i in 0 ..< 3:
check await connMngr.getIncomingSlot().withTimeout(10.millis)
# should timeout adding a connection over the limit
check not (await connMngr.getIncomingSlot().withTimeout(10.millis))
await connMngr.close()
asyncTest "track total outgoing connection limits":
let connMngr = ConnManager.new(maxConnections = 3)
for i in 0 ..< 3:
discard connMngr.getOutgoingSlot()
# should throw adding a connection over the limit
expect TooManyConnectionsError:
discard connMngr.getOutgoingSlot()
await connMngr.close()
asyncTest "track both incoming and outgoing total connections limits - fail on incoming":
let connMngr = ConnManager.new(maxConnections = 3)
for i in 0 ..< 3:
discard connMngr.getOutgoingSlot()
# should timeout adding a connection over the limit
check not (await connMngr.getIncomingSlot().withTimeout(10.millis))
await connMngr.close()
asyncTest "track both incoming and outgoing total connections limits - fail on outgoing":
let connMngr = ConnManager.new(maxConnections = 3)
for i in 0 ..< 3:
check await connMngr.getIncomingSlot().withTimeout(10.millis)
# should throw adding a connection over the limit
expect TooManyConnectionsError:
discard connMngr.getOutgoingSlot()
await connMngr.close()
asyncTest "track max incoming connection limits":
let connMngr = ConnManager.new(maxIn = 3)
for i in 0 ..< 3:
check await connMngr.getIncomingSlot().withTimeout(10.millis)
check not (await connMngr.getIncomingSlot().withTimeout(10.millis))
await connMngr.close()
asyncTest "track max outgoing connection limits":
let connMngr = ConnManager.new(maxOut = 3)
for i in 0 ..< 3:
discard connMngr.getOutgoingSlot()
# should throw adding a connection over the limit
expect TooManyConnectionsError:
discard connMngr.getOutgoingSlot()
await connMngr.close()
asyncTest "track incoming max connections limits - fail on incoming":
let connMngr = ConnManager.new(maxOut = 3)
for i in 0 ..< 3:
discard connMngr.getOutgoingSlot()
# should timeout adding a connection over the limit
check not (await connMngr.getIncomingSlot().withTimeout(10.millis))
await connMngr.close()
asyncTest "track incoming max connections limits - fail on outgoing":
let connMngr = ConnManager.new(maxIn = 3)
for i in 0 ..< 3:
check await connMngr.getIncomingSlot().withTimeout(10.millis)
# should throw adding a connection over the limit
expect TooManyConnectionsError:
discard connMngr.getOutgoingSlot()
await connMngr.close()
asyncTest "allow force dial":
let connMngr = ConnManager.new(maxConnections = 2)
for i in 0 ..< 3:
discard connMngr.getOutgoingSlot(true)
# should throw adding a connection over the limit
expect TooManyConnectionsError:
discard connMngr.getOutgoingSlot(false)
await connMngr.close()
asyncTest "release slot on connection end":
let connMngr = ConnManager.new(maxConnections = 3)
var muxs: seq[Muxer]
for i in 0 ..< 3:
let slot = connMngr.getOutgoingSlot()
let muxer = getMuxer(
PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet(),
Direction.In,
)
slot.trackMuxer(muxer)
muxs.add(muxer)
# should be full now
let incomingSlot = connMngr.getIncomingSlot()
check (await incomingSlot.withTimeout(10.millis)) == false
await allFuturesThrowing(allFutures(muxs.mapIt(it.close())))
check await incomingSlot.withTimeout(10.millis)
await connMngr.close()