Skip to content

Commit

Permalink
fix(core-p2p): replace ws connection logging method (#3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Jun 13, 2020
1 parent 9fc047a commit 3525e86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions __tests__/unit/core-p2p/peer-connector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import { NesClient } from "./mocks/nes";

jest.mock("@hapi/nes", () => require("./mocks/nes"));


describe("PeerConnector", () => {
let peerConnector: PeerConnector;
let logger;

beforeEach(() => {
logger = { warning: jest.fn(), debug: jest.fn(), error: jest.fn(), info: jest.fn() };

const container = new Container.Container();
container.unbindAll();
container.bind(Container.Identifiers.LogService).toConstantValue(logger);
container.bind(Container.Identifiers.PeerConnector).to(PeerConnector);

peerConnector = container.get<PeerConnector>(Container.Identifiers.PeerConnector);
Expand Down Expand Up @@ -58,6 +63,16 @@ describe("PeerConnector", () => {
expect(peerConnection).toBeInstanceOf(NesClient);
expect(peerConnection).toBe(peerConnector.connection(peer));
});

it("should log if error on connection", async () => {
const peer = new Peer("178.165.55.11", 4000);
const peerConnection = await peerConnector.connect(peer);

peerConnection.onError(new Error("dummy"));

expect(peerConnection).toBeInstanceOf(NesClient);
expect(logger.debug).toHaveBeenCalled();
});
});

describe("disconnect", () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core-p2p/src/peer-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Nes from "@hapi/nes";
// todo: review the implementation
@Container.injectable()
export class PeerConnector implements Contracts.P2P.PeerConnector {
@Container.inject(Container.Identifiers.LogService)
private readonly logger!: Contracts.Kernel.Logger;

private readonly connections: Map<string, Nes.Client> = new Map<string, Nes.Client>();
private readonly errors: Map<string, string> = new Map<string, string>();

Expand Down Expand Up @@ -65,6 +68,11 @@ export class PeerConnector implements Contracts.P2P.PeerConnector {

private async create(peer: Contracts.P2P.Peer): Promise<Nes.Client> {
const connection = new Nes.Client(`ws://${peer.ip}:${peer.port}`);

connection.onError = (error) => {
this.logger.debug(`Socket error (peer ${peer.ip}) : ${error.message}`);
};

await connection.connect();

return connection;
Expand Down

0 comments on commit 3525e86

Please sign in to comment.