Skip to content

Commit

Permalink
test(core-p2p): increase test coverage (#4004)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Sep 7, 2020
1 parent bf892b5 commit faaf4e1
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
49 changes: 49 additions & 0 deletions __tests__/unit/core-p2p/socket-server/routes/blocks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Container } from "@packages/core-kernel";

import { BlocksRoute } from "@packages/core-p2p/src/socket-server/routes/blocks";

describe("BlocksRoute", () => {
let blocksRoute: BlocksRoute;

const container = new Container.Container();

const logger = { warning: jest.fn(), debug: jest.fn() };
const controller = { getPeers: jest.fn() }; // a mock peer controller
const app = { resolve: jest.fn().mockReturnValue(controller) };
const server = { bind: jest.fn(), route: jest.fn() };

beforeAll(() => {
container.unbindAll();
container.bind(Container.Identifiers.LogService).toConstantValue(logger);
container.bind(Container.Identifiers.Application).toConstantValue(app);
});

beforeEach(() => {
blocksRoute = container.resolve<BlocksRoute>(BlocksRoute);
});

it("should bind the controller to the server and register the routes", () => {
const routes = blocksRoute.getRoutesConfigByPath();
const routesExpected = Object.entries(routes).map(([path, config]) => ({
method: "POST",
path,
config: {
id: config.id,
handler: config.handler,
payload: {
maxBytes: config.maxBytes,
},
},
}));

blocksRoute.register(server);

expect(server.bind).toBeCalledTimes(1);
expect(server.bind).toBeCalledWith(controller);

expect(server.route).toBeCalledTimes(routesExpected.length);
for (const route of routesExpected) {
expect(server.route).toBeCalledWith(route);
}
});
});
49 changes: 49 additions & 0 deletions __tests__/unit/core-p2p/socket-server/routes/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Container } from "@packages/core-kernel";

import { TransactionsRoute } from "@packages/core-p2p/src/socket-server/routes/transactions";

describe("BlocksRoute", () => {
let tranasctionsRoute: TransactionsRoute;

const container = new Container.Container();

const logger = { warning: jest.fn(), debug: jest.fn() };
const controller = { getPeers: jest.fn() }; // a mock peer controller
const app = { resolve: jest.fn().mockReturnValue(controller) };
const server = { bind: jest.fn(), route: jest.fn() };

beforeAll(() => {
container.unbindAll();
container.bind(Container.Identifiers.LogService).toConstantValue(logger);
container.bind(Container.Identifiers.Application).toConstantValue(app);
});

beforeEach(() => {
tranasctionsRoute = container.resolve<TransactionsRoute>(TransactionsRoute);
});

it("should bind the controller to the server and register the routes", () => {
const routes = tranasctionsRoute.getRoutesConfigByPath();
const routesExpected = Object.entries(routes).map(([path, config]) => ({
method: "POST",
path,
config: {
id: config.id,
handler: config.handler,
payload: {
maxBytes: config.maxBytes,
},
},
}));

tranasctionsRoute.register(server);

expect(server.bind).toBeCalledTimes(1);
expect(server.bind).toBeCalledWith(controller);

expect(server.route).toBeCalledTimes(routesExpected.length);
for (const route of routesExpected) {
expect(server.route).toBeCalledWith(route);
}
});
});
9 changes: 6 additions & 3 deletions __tests__/unit/core-p2p/socket-server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Server", () => {

const container = new Container.Container();

const logger = { warning: jest.fn(), debug: jest.fn() };
const logger = { warning: jest.fn(), debug: jest.fn(), info: jest.fn() };
const app = {
log: logger,
terminate: jest.fn(),
Expand All @@ -50,6 +50,7 @@ describe("Server", () => {
hapiServer.inject = jest.fn();
hapiServer.route = jest.fn();
hapiServer.register = jest.fn();
hapiServer.info = { uri: "" };
});

const name = "P2P server";
Expand Down Expand Up @@ -86,7 +87,8 @@ describe("Server", () => {
await server.initialize(name, options);
await server.boot();

expect(hapiServer.start).toBeCalledTimes(1);
expect(hapiServer.start).toBeCalledTimes(3);
expect(app.terminate).not.toBeCalled();
});

it("should terminate app if server.start() failed", async () => {
Expand All @@ -105,7 +107,8 @@ describe("Server", () => {
await server.initialize(name, options);
await server.dispose();

expect(hapiServer.stop).toBeCalledTimes(1);
expect(hapiServer.stop).toBeCalledTimes(3);
expect(app.terminate).not.toBeCalled();
});

it("should terminate app if server.stop() failed", async () => {
Expand Down
9 changes: 7 additions & 2 deletions __tests__/unit/core-p2p/utils/build-rate-limiter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//import { buildRateLimiter } from "@arkecosystem/core-p2p/src/utils/build-rate-limiter";
import { buildRateLimiter } from "@packages/core-p2p/src/utils/build-rate-limiter";
import { RateLimiter } from "@packages/core-p2p/src/rate-limiter";

describe("buildRateLimiter", () => {
it.todo("");
it("should return instance of RateLimiter", () => {
const rateLimiter = buildRateLimiter({ whitelist: [], remoteAccess: [] });

expect(rateLimiter).toBeInstanceOf(RateLimiter);
});
});
1 change: 1 addition & 0 deletions packages/core-p2p/src/peer-communicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class PeerCommunicator implements Contracts.P2P.PeerCommunicator {
if (process.env.CORE_P2P_PEER_VERIFIER_DEBUG_EXTRA) {
this.logger.debug(`Socket error (peer ${peer.ip}) : ${error.message}`);
}
/* istanbul ignore else */
if (disconnect) {
this.events.dispatch(Enums.PeerEvent.Disconnect, { peer });
}
Expand Down

0 comments on commit faaf4e1

Please sign in to comment.