5
5
createDecoder ,
6
6
createEncoder ,
7
7
DefaultPubSubTopic ,
8
- KeepAliveManager ,
9
8
waitForRemotePeer
10
9
} from "@waku/core" ;
11
10
import { LightNode } from "@waku/interfaces" ;
@@ -120,7 +119,7 @@ const TestCodec = "test/1";
120
119
121
120
describe ( "selectPeerForProtocol" , ( ) => {
122
121
let peerStore : PeerStore ;
123
- let getPingStub : sinon . SinonStub ;
122
+ let peerPings : Map < string , number > ;
124
123
const protocols = [ TestCodec ] ;
125
124
126
125
beforeEach ( async function ( ) {
@@ -129,7 +128,7 @@ describe("selectPeerForProtocol", () => {
129
128
await waku . start ( ) ;
130
129
await delay ( 3000 ) ;
131
130
peerStore = waku . libp2p . peerStore ;
132
- getPingStub = sinon . stub ( KeepAliveManager . getInstance ( ) , "getPing" ) ;
131
+ peerPings = new Map ( ) ;
133
132
} ) ;
134
133
135
134
afterEach ( ( ) => {
@@ -153,15 +152,11 @@ describe("selectPeerForProtocol", () => {
153
152
}
154
153
} ) ;
155
154
156
- getPingStub . withArgs ( peer1 ) . resolves ( 500 ) ;
157
- getPingStub . withArgs ( peer2 ) . resolves ( 1000 ) ;
158
- getPingStub . withArgs ( peer3 ) . resolves ( 100 ) ;
155
+ peerPings . set ( peer1 . toString ( ) , 500 ) ;
156
+ peerPings . set ( peer2 . toString ( ) , 1000 ) ;
157
+ peerPings . set ( peer3 . toString ( ) , 100 ) ;
159
158
160
- const result = await selectPeerForProtocol (
161
- peerStore ,
162
- getPingStub ,
163
- protocols
164
- ) ;
159
+ const result = await selectPeerForProtocol ( peerStore , peerPings , protocols ) ;
165
160
166
161
expect ( result . peer ) . to . deep . equal ( mockPeers [ 2 ] ) ;
167
162
expect ( result . protocol ) . to . equal ( TestCodec ) ;
@@ -174,14 +169,14 @@ describe("selectPeerForProtocol", () => {
174
169
175
170
const result = await selectPeerForProtocol (
176
171
peerStore ,
177
- getPingStub ,
172
+ peerPings ,
178
173
protocols ,
179
174
targetPeer
180
175
) ;
181
176
expect ( result . peer ) . to . deep . equal ( mockPeer ) ;
182
177
} ) ;
183
178
184
- it ( "should return a any peer when all peers have the same latency" , async function ( ) {
179
+ it ( "should return a random peer when all peers have the same latency" , async function ( ) {
185
180
const peer1 = await createSecp256k1PeerId ( ) ;
186
181
const peer2 = await createSecp256k1PeerId ( ) ;
187
182
const peer3 = await createSecp256k1PeerId ( ) ;
@@ -198,13 +193,11 @@ describe("selectPeerForProtocol", () => {
198
193
}
199
194
} ) ;
200
195
201
- getPingStub . resolves ( 500 ) ; // All peers have the same latency
196
+ peerPings . set ( peer1 . toString ( ) , 500 ) ;
197
+ peerPings . set ( peer2 . toString ( ) , 500 ) ;
198
+ peerPings . set ( peer3 . toString ( ) , 500 ) ;
202
199
203
- const result = await selectPeerForProtocol (
204
- peerStore ,
205
- getPingStub ,
206
- protocols
207
- ) ;
200
+ const result = await selectPeerForProtocol ( peerStore , peerPings , protocols ) ;
208
201
209
202
expect ( mockPeers ) . to . deep . include ( result . peer ) ;
210
203
} ) ;
@@ -225,7 +218,7 @@ describe("selectPeerForProtocol", () => {
225
218
} ) ;
226
219
227
220
await expect (
228
- selectPeerForProtocol ( peerStore , getPingStub , protocols )
221
+ selectPeerForProtocol ( peerStore , peerPings , protocols )
229
222
) . to . be . rejectedWith (
230
223
`Failed to find known peer that registers protocols: ${ protocols } `
231
224
) ;
@@ -237,7 +230,7 @@ describe("selectPeerForProtocol", () => {
237
230
sinon . stub ( peerStore , "get" ) . withArgs ( targetPeer ) . resolves ( mockPeer ) ;
238
231
239
232
await expect (
240
- selectPeerForProtocol ( peerStore , getPingStub , protocols , targetPeer )
233
+ selectPeerForProtocol ( peerStore , peerPings , protocols , targetPeer )
241
234
) . to . be . rejectedWith (
242
235
`Peer does not register required protocols (${ targetPeer . toString ( ) } ): ${ protocols } `
243
236
) ;
0 commit comments