mirror of https://github.com/waku-org/js-waku.git
remove old test for peerManager, fix check and spell
This commit is contained in:
parent
8a3337df89
commit
d4210c7d17
|
@ -116,6 +116,7 @@
|
||||||
"upgrader",
|
"upgrader",
|
||||||
"vacp",
|
"vacp",
|
||||||
"varint",
|
"varint",
|
||||||
|
"weboko",
|
||||||
"waku",
|
"waku",
|
||||||
"wakuconnect",
|
"wakuconnect",
|
||||||
"wakunode",
|
"wakunode",
|
||||||
|
|
|
@ -10,6 +10,8 @@ import { utf8ToBytes } from "@waku/utils/bytes";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
|
|
||||||
|
import { PeerManager } from "../peer_manager.js";
|
||||||
|
|
||||||
import { LightPush } from "./light_push.js";
|
import { LightPush } from "./light_push.js";
|
||||||
|
|
||||||
const PUBSUB_TOPIC = "/waku/2/rs/1/4";
|
const PUBSUB_TOPIC = "/waku/2/rs/1/4";
|
||||||
|
@ -156,7 +158,8 @@ function mockLightPush(options: MockLightPushOptions): LightPush {
|
||||||
return new LightPush(
|
return new LightPush(
|
||||||
{
|
{
|
||||||
configuredPubsubTopics: options.pubsubTopics || [PUBSUB_TOPIC]
|
configuredPubsubTopics: options.pubsubTopics || [PUBSUB_TOPIC]
|
||||||
} as ConnectionManager,
|
} as unknown as ConnectionManager,
|
||||||
|
{} as unknown as PeerManager,
|
||||||
options.libp2p
|
options.libp2p
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,148 +0,0 @@
|
||||||
import { Peer, PeerId } from "@libp2p/interface";
|
|
||||||
import { ConnectionManager, LightPushCodec } from "@waku/core";
|
|
||||||
import { BaseProtocol } from "@waku/core/lib/base_protocol";
|
|
||||||
import { Logger } from "@waku/utils";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import sinon from "sinon";
|
|
||||||
|
|
||||||
import { PeerManager } from "./peer_manager.js";
|
|
||||||
|
|
||||||
describe("PeerManager", () => {
|
|
||||||
let peerManager: PeerManager;
|
|
||||||
let mockConnectionManager: sinon.SinonStubbedInstance<ConnectionManager>;
|
|
||||||
let mockCore: sinon.SinonStubbedInstance<BaseProtocol>;
|
|
||||||
let mockLogger: any;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
mockConnectionManager = sinon.createStubInstance(ConnectionManager);
|
|
||||||
mockCore = sinon.createStubInstance(BaseProtocol);
|
|
||||||
mockLogger = {
|
|
||||||
info: sinon.stub(),
|
|
||||||
warn: sinon.stub(),
|
|
||||||
error: sinon.stub(),
|
|
||||||
debug: sinon.stub(),
|
|
||||||
extend: sinon.stub().returns({
|
|
||||||
info: sinon.stub(),
|
|
||||||
warn: sinon.stub(),
|
|
||||||
error: sinon.stub(),
|
|
||||||
debug: sinon.stub()
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
mockCore.multicodec = LightPushCodec;
|
|
||||||
|
|
||||||
peerManager = new PeerManager(
|
|
||||||
mockConnectionManager as any,
|
|
||||||
mockCore as any,
|
|
||||||
mockLogger as Logger
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
sinon.restore();
|
|
||||||
});
|
|
||||||
|
|
||||||
const createMockPeer = (id: string): Peer =>
|
|
||||||
({
|
|
||||||
id: {
|
|
||||||
toString: () => id
|
|
||||||
} as PeerId
|
|
||||||
}) as Peer;
|
|
||||||
|
|
||||||
describe("addPeer", () => {
|
|
||||||
it("should add a peer", async () => {
|
|
||||||
const peer = createMockPeer("peer1");
|
|
||||||
await peerManager.addPeer(peer);
|
|
||||||
|
|
||||||
expect(mockConnectionManager.attemptDial.calledWith(peer.id)).to.be.true;
|
|
||||||
expect(
|
|
||||||
mockLogger.info.calledWith(sinon.match(/Added and dialed peer: peer1/))
|
|
||||||
).to.be.true;
|
|
||||||
expect(await peerManager.getPeerCount()).to.equal(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("removePeer", () => {
|
|
||||||
it("should remove a peer", async () => {
|
|
||||||
const peer = createMockPeer("peer1");
|
|
||||||
await peerManager.addPeer(peer);
|
|
||||||
await peerManager.removePeer(peer.id);
|
|
||||||
|
|
||||||
expect(mockLogger.info.calledWith(sinon.match(/Removed peer: peer1/))).to
|
|
||||||
.be.true;
|
|
||||||
expect(await peerManager.getPeerCount()).to.equal(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("getPeerCount", () => {
|
|
||||||
it("should return the correct number of peers", async () => {
|
|
||||||
await peerManager.addPeer(createMockPeer("peer1"));
|
|
||||||
await peerManager.addPeer(createMockPeer("peer2"));
|
|
||||||
|
|
||||||
const count = await peerManager.getPeerCount();
|
|
||||||
expect(count).to.equal(2);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("hasPeers", () => {
|
|
||||||
it("should return true when peers exist", async () => {
|
|
||||||
await peerManager.addPeer(createMockPeer("peer1"));
|
|
||||||
const result = await peerManager.hasPeers();
|
|
||||||
expect(result).to.be.true;
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should return false when no peers exist", async () => {
|
|
||||||
const result = await peerManager.hasPeers();
|
|
||||||
expect(result).to.be.false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("removeExcessPeers", () => {
|
|
||||||
it("should remove the specified number of excess peers", async () => {
|
|
||||||
await peerManager.addPeer(createMockPeer("peer1"));
|
|
||||||
await peerManager.addPeer(createMockPeer("peer2"));
|
|
||||||
await peerManager.addPeer(createMockPeer("peer3"));
|
|
||||||
|
|
||||||
await peerManager.removeExcessPeers(2);
|
|
||||||
|
|
||||||
const count = await peerManager.getPeerCount();
|
|
||||||
expect(count).to.equal(1);
|
|
||||||
expect(mockLogger.info.calledWith(`Removing 2 excess peer(s)`)).to.be
|
|
||||||
.true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("findAndAddPeers", () => {
|
|
||||||
it("should find and add new peers", async () => {
|
|
||||||
const newPeers = [createMockPeer("peer1"), createMockPeer("peer2")];
|
|
||||||
mockCore.getPeers.resolves(newPeers);
|
|
||||||
|
|
||||||
const addedPeers = await peerManager.findAndAddPeers(2);
|
|
||||||
|
|
||||||
expect(addedPeers).to.have.lengthOf(2);
|
|
||||||
expect(mockConnectionManager.attemptDial.callCount).to.equal(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not add existing peers", async () => {
|
|
||||||
const existingPeer = createMockPeer("existing");
|
|
||||||
await peerManager.addPeer(existingPeer);
|
|
||||||
|
|
||||||
const newPeers = [existingPeer, createMockPeer("new")];
|
|
||||||
mockCore.getPeers.resolves(newPeers);
|
|
||||||
|
|
||||||
const addedPeers = await peerManager.findAndAddPeers(2);
|
|
||||||
|
|
||||||
expect(addedPeers).to.have.lengthOf(1);
|
|
||||||
expect(mockConnectionManager.attemptDial.callCount).to.equal(2); // Once for existing, once for new
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should log when no additional peers are found", async () => {
|
|
||||||
mockCore.getPeers.resolves([]);
|
|
||||||
|
|
||||||
await peerManager.findAndAddPeers(2);
|
|
||||||
|
|
||||||
expect(mockLogger.warn.calledWith("No additional peers found")).to.be
|
|
||||||
.true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue