1
1
import { CustomEvent } from "@libp2p/interfaces/events" ;
2
2
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory" ;
3
- import { ConnectionManager , KeepAliveOptions } from "@waku/core" ;
4
3
import { EPeersByDiscoveryEvents , LightNode , Tags } from "@waku/interfaces" ;
5
4
import { createLightNode } from "@waku/sdk" ;
6
5
import { expect } from "chai" ;
7
6
import sinon , { SinonSpy , SinonStub } from "sinon" ;
8
7
9
8
import { delay } from "../dist/delay.js" ;
10
9
11
- const KEEP_ALIVE_OPTIONS : KeepAliveOptions = {
12
- pingKeepAlive : 0 ,
13
- relayKeepAlive : 5 * 1000 ,
14
- } ;
15
10
const TEST_TIMEOUT = 10_000 ;
16
11
const DELAY_MS = 1_000 ;
17
12
18
13
describe ( "ConnectionManager" , function ( ) {
19
- let connectionManager : ConnectionManager | undefined ;
20
14
let waku : LightNode ;
21
- let peerId : string ;
22
15
23
16
beforeEach ( async function ( ) {
24
17
waku = await createLightNode ( ) ;
25
- peerId = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
26
- connectionManager = ConnectionManager . create (
27
- peerId ,
28
- waku . libp2p ,
29
- KEEP_ALIVE_OPTIONS
30
- ) ;
31
18
} ) ;
32
19
33
20
afterEach ( async ( ) => {
@@ -51,15 +38,17 @@ describe("ConnectionManager", function () {
51
38
} ) ;
52
39
53
40
const peerDiscoveryBootstrap = new Promise < boolean > ( ( resolve ) => {
54
- connectionManager ! . addEventListener (
41
+ waku . connectionManager . addEventListener (
55
42
EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP ,
56
43
( { detail : receivedPeerId } ) => {
57
44
resolve ( receivedPeerId . toString ( ) === peerIdBootstrap . toString ( ) ) ;
58
45
}
59
46
) ;
60
47
} ) ;
61
48
62
- waku . libp2p . dispatchEvent ( new CustomEvent ( "peer" , { detail : peerId } ) ) ;
49
+ waku . libp2p . dispatchEvent (
50
+ new CustomEvent ( "peer" , { detail : await createSecp256k1PeerId ( ) } )
51
+ ) ;
63
52
64
53
expect ( await peerDiscoveryBootstrap ) . to . eq ( true ) ;
65
54
} ) ;
@@ -77,7 +66,7 @@ describe("ConnectionManager", function () {
77
66
} ) ;
78
67
79
68
const peerDiscoveryPeerExchange = new Promise < boolean > ( ( resolve ) => {
80
- connectionManager ! . addEventListener (
69
+ waku . connectionManager . addEventListener (
81
70
EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
82
71
( { detail : receivedPeerId } ) => {
83
72
resolve ( receivedPeerId . toString ( ) === peerIdPx . toString ( ) ) ;
@@ -109,7 +98,7 @@ describe("ConnectionManager", function () {
109
98
} ) ;
110
99
111
100
const peerConnectedBootstrap = new Promise < boolean > ( ( resolve ) => {
112
- connectionManager ! . addEventListener (
101
+ waku . connectionManager . addEventListener (
113
102
EPeersByDiscoveryEvents . PEER_CONNECT_BOOTSTRAP ,
114
103
( { detail : receivedPeerId } ) => {
115
104
resolve ( receivedPeerId . toString ( ) === peerIdBootstrap . toString ( ) ) ;
@@ -136,7 +125,7 @@ describe("ConnectionManager", function () {
136
125
} ) ;
137
126
138
127
const peerConnectedPeerExchange = new Promise < boolean > ( ( resolve ) => {
139
- connectionManager ! . addEventListener (
128
+ waku . connectionManager . addEventListener (
140
129
EPeersByDiscoveryEvents . PEER_CONNECT_PEER_EXCHANGE ,
141
130
( { detail : receivedPeerId } ) => {
142
131
resolve ( receivedPeerId . toString ( ) === peerIdPx . toString ( ) ) ;
@@ -157,16 +146,25 @@ describe("ConnectionManager", function () {
157
146
let dialPeerStub : SinonStub ;
158
147
let getConnectionsStub : SinonStub ;
159
148
let getTagNamesForPeerStub : SinonStub ;
149
+ let waku : LightNode ;
160
150
161
- afterEach ( ( ) => {
151
+ this . beforeEach ( async function ( ) {
152
+ waku = await createLightNode ( ) ;
153
+ } ) ;
154
+
155
+ afterEach ( async ( ) => {
156
+ await waku . stop ( ) ;
162
157
sinon . restore ( ) ;
163
158
} ) ;
164
159
165
160
describe ( "attemptDial method" , function ( ) {
166
161
let attemptDialSpy : SinonSpy ;
167
162
168
163
beforeEach ( function ( ) {
169
- attemptDialSpy = sinon . spy ( connectionManager as any , "attemptDial" ) ;
164
+ attemptDialSpy = sinon . spy (
165
+ waku . connectionManager as any ,
166
+ "attemptDial"
167
+ ) ;
170
168
} ) ;
171
169
172
170
afterEach ( function ( ) {
@@ -196,14 +194,14 @@ describe("ConnectionManager", function () {
196
194
describe ( "dialPeer method" , function ( ) {
197
195
beforeEach ( function ( ) {
198
196
getConnectionsStub = sinon . stub (
199
- ( connectionManager as any ) . libp2p ,
197
+ ( waku . connectionManager as any ) . libp2p ,
200
198
"getConnections"
201
199
) ;
202
200
getTagNamesForPeerStub = sinon . stub (
203
- connectionManager as any ,
201
+ waku . connectionManager as any ,
204
202
"getTagNamesForPeer"
205
203
) ;
206
- dialPeerStub = sinon . stub ( connectionManager as any , "dialPeer" ) ;
204
+ dialPeerStub = sinon . stub ( waku . connectionManager as any , "dialPeer" ) ;
207
205
} ) ;
208
206
209
207
afterEach ( function ( ) {
0 commit comments