@@ -2,18 +2,26 @@ import type { PeerId } from "@libp2p/interface/peer-id";
2
2
import type { PeerInfo } from "@libp2p/interface/peer-info" ;
3
3
import { CustomEvent } from "@libp2p/interfaces/events" ;
4
4
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory" ;
5
- import { EPeersByDiscoveryEvents , LightNode , Tags } from "@waku/interfaces" ;
5
+ import { Multiaddr } from "@multiformats/multiaddr" ;
6
+ import {
7
+ EConnectionStateEvents ,
8
+ EPeersByDiscoveryEvents ,
9
+ LightNode ,
10
+ Protocols ,
11
+ Tags
12
+ } from "@waku/interfaces" ;
6
13
import { createLightNode } from "@waku/sdk" ;
7
14
import { expect } from "chai" ;
8
15
import sinon , { SinonSpy , SinonStub } from "sinon" ;
9
16
10
17
import { delay } from "../dist/delay.js" ;
11
- import { tearDownNodes } from "../src/index.js" ;
18
+ import { makeLogFileName , NimGoNode , tearDownNodes } from "../src/index.js" ;
12
19
13
20
const TEST_TIMEOUT = 10_000 ;
14
21
const DELAY_MS = 1_000 ;
15
22
16
23
describe ( "ConnectionManager" , function ( ) {
24
+ this . timeout ( 20_000 ) ;
17
25
let waku : LightNode ;
18
26
19
27
beforeEach ( async function ( ) {
@@ -156,6 +164,105 @@ describe("ConnectionManager", function () {
156
164
expect ( await peerConnectedPeerExchange ) . to . eq ( true ) ;
157
165
} ) ;
158
166
} ) ;
167
+
168
+ describe ( "peer:disconnect" , ( ) => {
169
+ it ( "should emit `waku:offline` event when all peers disconnect" , async function ( ) {
170
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
171
+ const peerIdPx2 = await createSecp256k1PeerId ( ) ;
172
+
173
+ await waku . libp2p . peerStore . save ( peerIdPx , {
174
+ tags : {
175
+ [ Tags . PEER_EXCHANGE ] : {
176
+ value : 50 ,
177
+ ttl : 1200000
178
+ }
179
+ }
180
+ } ) ;
181
+
182
+ await waku . libp2p . peerStore . save ( peerIdPx2 , {
183
+ tags : {
184
+ [ Tags . PEER_EXCHANGE ] : {
185
+ value : 50 ,
186
+ ttl : 1200000
187
+ }
188
+ }
189
+ } ) ;
190
+
191
+ waku . libp2p . dispatchEvent (
192
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
193
+ ) ;
194
+ waku . libp2p . dispatchEvent (
195
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx2 } )
196
+ ) ;
197
+
198
+ await delay ( 100 ) ;
199
+
200
+ let eventCount = 0 ;
201
+ const connectionStatus = new Promise < boolean > ( ( resolve ) => {
202
+ waku . connectionManager . addEventListener (
203
+ EConnectionStateEvents . CONNECTION_STATUS ,
204
+ ( { detail : status } ) => {
205
+ eventCount ++ ;
206
+ resolve ( status ) ;
207
+ }
208
+ ) ;
209
+ } ) ;
210
+
211
+ expect ( waku . isConnected ( ) ) . to . be . true ;
212
+
213
+ waku . libp2p . dispatchEvent (
214
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx } )
215
+ ) ;
216
+ waku . libp2p . dispatchEvent (
217
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx2 } )
218
+ ) ;
219
+
220
+ expect ( await connectionStatus ) . to . eq ( false ) ;
221
+ expect ( eventCount ) . to . be . eq ( 1 ) ;
222
+ } ) ;
223
+ it ( "isConnected should return false after all peers disconnect" , async function ( ) {
224
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
225
+ const peerIdPx2 = await createSecp256k1PeerId ( ) ;
226
+
227
+ await waku . libp2p . peerStore . save ( peerIdPx , {
228
+ tags : {
229
+ [ Tags . PEER_EXCHANGE ] : {
230
+ value : 50 ,
231
+ ttl : 1200000
232
+ }
233
+ }
234
+ } ) ;
235
+
236
+ await waku . libp2p . peerStore . save ( peerIdPx2 , {
237
+ tags : {
238
+ [ Tags . PEER_EXCHANGE ] : {
239
+ value : 50 ,
240
+ ttl : 1200000
241
+ }
242
+ }
243
+ } ) ;
244
+
245
+ waku . libp2p . dispatchEvent (
246
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
247
+ ) ;
248
+ waku . libp2p . dispatchEvent (
249
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx2 } )
250
+ ) ;
251
+
252
+ await delay ( 100 ) ;
253
+
254
+ expect ( waku . isConnected ( ) ) . to . be . true ;
255
+
256
+ waku . libp2p . dispatchEvent (
257
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx } )
258
+ ) ;
259
+ waku . libp2p . dispatchEvent (
260
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx2 } )
261
+ ) ;
262
+
263
+ expect ( waku . isConnected ( ) ) . to . be . false ;
264
+ } ) ;
265
+ } ) ;
159
266
} ) ;
160
267
161
268
describe ( "Dials" , ( ) => {
@@ -376,4 +483,111 @@ describe("ConnectionManager", function () {
376
483
} ) ;
377
484
} ) ;
378
485
} ) ;
486
+
487
+ describe ( "Connection state" , ( ) => {
488
+ this . timeout ( 20_000 ) ;
489
+ let nwaku1 : NimGoNode ;
490
+ let nwaku2 : NimGoNode ;
491
+ let nwaku1PeerId : Multiaddr ;
492
+ let nwaku2PeerId : Multiaddr ;
493
+
494
+ beforeEach ( async ( ) => {
495
+ this . timeout ( 20_000 ) ;
496
+ nwaku1 = new NimGoNode ( makeLogFileName ( this . ctx ) + "1" ) ;
497
+ nwaku2 = new NimGoNode ( makeLogFileName ( this . ctx ) + "2" ) ;
498
+ await nwaku1 . start ( {
499
+ filter : true
500
+ } ) ;
501
+
502
+ await nwaku2 . start ( {
503
+ filter : true
504
+ } ) ;
505
+
506
+ nwaku1PeerId = await nwaku1 . getMultiaddrWithId ( ) ;
507
+ nwaku2PeerId = await nwaku2 . getMultiaddrWithId ( ) ;
508
+ } ) ;
509
+
510
+ afterEach ( async ( ) => {
511
+ this . timeout ( 15000 ) ;
512
+ await tearDownNodes ( [ nwaku1 , nwaku2 ] , [ ] ) ;
513
+ } ) ;
514
+
515
+ it ( "should emit `waku:online` event only when first peer is connected" , async function ( ) {
516
+ this . timeout ( 20_000 ) ;
517
+
518
+ let eventCount = 0 ;
519
+ const connectionStatus = new Promise < boolean > ( ( resolve ) => {
520
+ waku . connectionManager . addEventListener (
521
+ EConnectionStateEvents . CONNECTION_STATUS ,
522
+ ( { detail : status } ) => {
523
+ eventCount ++ ;
524
+ resolve ( status ) ;
525
+ }
526
+ ) ;
527
+ } ) ;
528
+
529
+ // await waku.start();
530
+ await waku . dial ( nwaku1PeerId , [ Protocols . Filter ] ) ;
531
+ await waku . dial ( nwaku2PeerId , [ Protocols . Filter ] ) ;
532
+
533
+ await delay ( 250 ) ;
534
+
535
+ expect ( await connectionStatus ) . to . eq ( true ) ;
536
+ expect ( eventCount ) . to . be . eq ( 1 ) ;
537
+ } ) ;
538
+
539
+ it ( "isConnected should return true after first peer connects" , async function ( ) {
540
+ this . timeout ( 20_000 ) ;
541
+ expect ( waku . isConnected ( ) ) . to . be . false ;
542
+
543
+ // await waku.start();
544
+ await waku . dial ( nwaku1PeerId , [ Protocols . Filter ] ) ;
545
+ await waku . dial ( nwaku2PeerId , [ Protocols . Filter ] ) ;
546
+
547
+ await delay ( 250 ) ;
548
+
549
+ expect ( waku . isConnected ( ) ) . to . be . true ;
550
+ } ) ;
551
+
552
+ it ( "should emit `waku:offline` event only when all peers disconnect" , async function ( ) {
553
+ this . timeout ( 20_000 ) ;
554
+ expect ( waku . isConnected ( ) ) . to . be . false ;
555
+
556
+ await waku . dial ( nwaku1PeerId , [ Protocols . Filter ] ) ;
557
+ await waku . dial ( nwaku2PeerId , [ Protocols . Filter ] ) ;
558
+
559
+ await delay ( 250 ) ;
560
+
561
+ let eventCount = 0 ;
562
+ const connectionStatus = new Promise < boolean > ( ( resolve ) => {
563
+ waku . connectionManager . addEventListener (
564
+ EConnectionStateEvents . CONNECTION_STATUS ,
565
+ ( { detail : status } ) => {
566
+ eventCount ++ ;
567
+ resolve ( status ) ;
568
+ }
569
+ ) ;
570
+ } ) ;
571
+
572
+ await waku . libp2p . hangUp ( nwaku1PeerId ) ;
573
+ await waku . libp2p . hangUp ( nwaku2PeerId ) ;
574
+ expect ( await connectionStatus ) . to . eq ( false ) ;
575
+ expect ( eventCount ) . to . be . eq ( 1 ) ;
576
+ } ) ;
577
+
578
+ it ( "isConnected should return false after all peers disconnect" , async function ( ) {
579
+ this . timeout ( 20_000 ) ;
580
+ expect ( waku . isConnected ( ) ) . to . be . false ;
581
+
582
+ await waku . dial ( nwaku1PeerId , [ Protocols . Filter ] ) ;
583
+ await waku . dial ( nwaku2PeerId , [ Protocols . Filter ] ) ;
584
+
585
+ await delay ( 250 ) ;
586
+ expect ( waku . isConnected ( ) ) . to . be . true ;
587
+
588
+ await waku . libp2p . hangUp ( nwaku1PeerId ) ;
589
+ await waku . libp2p . hangUp ( nwaku2PeerId ) ;
590
+ expect ( waku . isConnected ( ) ) . to . be . false ;
591
+ } ) ;
592
+ } ) ;
379
593
} ) ;
0 commit comments