Skip to content

Commit d28647a

Browse files
update tests for the new API
1 parent 2dabefc commit d28647a

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

packages/tests/tests/utils.spec.ts

+14-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
createDecoder,
66
createEncoder,
77
DefaultPubSubTopic,
8-
KeepAliveManager,
98
waitForRemotePeer
109
} from "@waku/core";
1110
import { LightNode } from "@waku/interfaces";
@@ -120,7 +119,7 @@ const TestCodec = "test/1";
120119

121120
describe("selectPeerForProtocol", () => {
122121
let peerStore: PeerStore;
123-
let getPingStub: sinon.SinonStub;
122+
let peerPings: Map<string, number>;
124123
const protocols = [TestCodec];
125124

126125
beforeEach(async function () {
@@ -129,7 +128,7 @@ describe("selectPeerForProtocol", () => {
129128
await waku.start();
130129
await delay(3000);
131130
peerStore = waku.libp2p.peerStore;
132-
getPingStub = sinon.stub(KeepAliveManager.getInstance(), "getPing");
131+
peerPings = new Map();
133132
});
134133

135134
afterEach(() => {
@@ -153,15 +152,11 @@ describe("selectPeerForProtocol", () => {
153152
}
154153
});
155154

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);
159158

160-
const result = await selectPeerForProtocol(
161-
peerStore,
162-
getPingStub,
163-
protocols
164-
);
159+
const result = await selectPeerForProtocol(peerStore, peerPings, protocols);
165160

166161
expect(result.peer).to.deep.equal(mockPeers[2]);
167162
expect(result.protocol).to.equal(TestCodec);
@@ -174,14 +169,14 @@ describe("selectPeerForProtocol", () => {
174169

175170
const result = await selectPeerForProtocol(
176171
peerStore,
177-
getPingStub,
172+
peerPings,
178173
protocols,
179174
targetPeer
180175
);
181176
expect(result.peer).to.deep.equal(mockPeer);
182177
});
183178

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 () {
185180
const peer1 = await createSecp256k1PeerId();
186181
const peer2 = await createSecp256k1PeerId();
187182
const peer3 = await createSecp256k1PeerId();
@@ -198,13 +193,11 @@ describe("selectPeerForProtocol", () => {
198193
}
199194
});
200195

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);
202199

203-
const result = await selectPeerForProtocol(
204-
peerStore,
205-
getPingStub,
206-
protocols
207-
);
200+
const result = await selectPeerForProtocol(peerStore, peerPings, protocols);
208201

209202
expect(mockPeers).to.deep.include(result.peer);
210203
});
@@ -225,7 +218,7 @@ describe("selectPeerForProtocol", () => {
225218
});
226219

227220
await expect(
228-
selectPeerForProtocol(peerStore, getPingStub, protocols)
221+
selectPeerForProtocol(peerStore, peerPings, protocols)
229222
).to.be.rejectedWith(
230223
`Failed to find known peer that registers protocols: ${protocols}`
231224
);
@@ -237,7 +230,7 @@ describe("selectPeerForProtocol", () => {
237230
sinon.stub(peerStore, "get").withArgs(targetPeer).resolves(mockPeer);
238231

239232
await expect(
240-
selectPeerForProtocol(peerStore, getPingStub, protocols, targetPeer)
233+
selectPeerForProtocol(peerStore, peerPings, protocols, targetPeer)
241234
).to.be.rejectedWith(
242235
`Peer does not register required protocols (${targetPeer.toString()}): ${protocols}`
243236
);

0 commit comments

Comments
 (0)