Skip to content

Commit

Permalink
Rework server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Dec 24, 2017
1 parent edfa843 commit 4496d24
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 145 deletions.
4 changes: 2 additions & 2 deletions packages/client-p2p/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = class Server extends EventEmitter implements P2pInterface {

_onMessage = ({ peer, message }: { peer: PeerInterface, message: MessageInterface }): void => {
if (message.id === StatusMessage.MESSAGE_ID) {
// this._sendStatus(peer);
this._sendStatus(peer);
peer.status = ((message: any): StatusMessage);
}

Expand All @@ -104,7 +104,7 @@ module.exports = class Server extends EventEmitter implements P2pInterface {
}

_sendStatus = (peer: PeerInterface): boolean => {
setTimeout(() => this._sendStatus(peer), 5000);
// setTimeout(() => this._sendStatus(peer), 5000);

return peer.send(
new StatusMessage({
Expand Down
173 changes: 30 additions & 143 deletions packages/client-p2p/src/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ const PeerId = require('peer-id');
const PeerInfo = require('peer-info');

const State = require('@polkadot/client-state');
const isInstanceOf = require('@polkadot/util/is/instanceOf');

const StatusMessage = require('./message/status');
const { streamReader, streamWriter } = require('./stream');
const defaults = require('./defaults');
const Server = require('./index');

Expand Down Expand Up @@ -162,109 +160,50 @@ describe('Server', () => {
});
});

describe('_dialPeer', () => {
let connection;
describe('_onMessage', () => {
let peer;
let status;

beforeEach(() => {
connection = { 'some': 'connection' };
peer = {
connection,
isConnecting: false,
peerInfo: 'peerInfo'
};

server._send = jest.fn(() => true);
server._node = {
dial: jest.fn((peerInfo, protocol, cb) => cb(null, connection))
};
});

afterEach(() => {
server._node = null;
});
peer = { id: '123456', shortId: '123456' };
status = new StatusMessage();

it('returns false when peer is not found', () => {
return server._dialPeer().then((result) => {
expect(result).toEqual(false);
});
server._sendStatus = jest.fn(() => true);
});

it('returns false when peer isConnecting', () => {
return server._dialPeer({ isConnecting: true }).then((result) => {
expect(result).toEqual(false);
it('emits the message as received', (done) => {
server.on('message', ({ peer, message }) => {
expect(message).toEqual(status);
done();
});
});

it('returns true when completed', () => {
return server._dialPeer(peer).then((result) => {
expect(result).toEqual(true);
});
server._onMessage({ peer, message: status });
});

it('returns false when error ocurred', () => {
server._send = () => {
throw new Error('error');
};

return server._dialPeer(peer).then((result) => {
expect(peer.isConnecting).toEqual(false);
expect(peer.isConnected).toEqual(false);
expect(result).toEqual(false);
});
});
it('add status to peer status is received', () => {
server._onMessage({ peer, message: status });

it('dials the peer', () => {
return server._dialPeer(peer).then((result) => {
expect(server._node.dial).toHaveBeenCalledWith(
peer.peerInfo, defaults.PROTOCOL, expect.anything()
);
});
expect(peer.status).toEqual(status);
});

it('sends the status to the peer', (done) => {
server._send = (_connection, message) => {
expect(connection).toEqual(connection);
expect(
isInstanceOf(message, StatusMessage)
).toEqual(true);
done();

return true;
};
it('send status when status is received', () => {
server._onMessage({ peer, message: status });

server._dialPeer(peer);
expect(server._sendStatus).toHaveBeenCalledWith(peer);
});
});

describe('_handleMessage', () => {
describe('_onPeerConnected', () => {
let peer;
let status;

beforeEach(() => {
peer = {};
status = new StatusMessage();
peer = { id: '123456', shortId: '123456' };

server._sendStatus = jest.fn(() => true);
});

it('emits the message as received', (done) => {
server.on('message', (message) => {
expect(message).toEqual(status);
done();
});

server._handleMessage(peer, status);
});

it('add status to peer status is received', () => {
server._handleMessage(peer, status);

expect(peer.status).toEqual(status);
});

it('send status when status is received', () => {
server._handleMessage(peer, status);
it('send status when connection received', () => {
server._onPeerConnected(peer);

expect(server._sendStatus).toHaveBeenCalledWith(peer);
});
Expand All @@ -275,36 +214,26 @@ describe('Server', () => {

beforeEach(() => {
peer = {
hasStatus: false,
connection: true
send: jest.fn(() => 'testResult')
};
server._send = jest.fn(() => true);
});

it('returns false when peer hasStatus', () => {
expect(
server._sendStatus(
Object.assign(peer, { hasStatus: true })
)
).toEqual(false);
});
it('calls into peer send', () => {
server._sendStatus(peer);

it('returns true when completed', () => {
expect(
server._sendStatus(peer)
).toEqual(true);
peer.send
).toHaveBeenCalled();
});

it('sets hasStatus upon sending', () => {
server._sendStatus(peer);

it('returns result from peer send', () => {
expect(
peer.hasStatus
).toEqual(true);
server._sendStatus(peer)
).toEqual('testResult');
});
});

describe('_handleProtocol', () => {
describe('_onProtocol', () => {
let connection;
let peer;
let peerInfo;
Expand All @@ -326,7 +255,7 @@ describe('Server', () => {
};
server._receive = jest.fn((peer) => true);

server._handleProtocol(defaults.PROTOCOL, connection);
return server._onProtocol(defaults.PROTOCOL, connection);
});

it('retrieves the peerInfo', () => {
Expand All @@ -336,47 +265,5 @@ describe('Server', () => {
it('adds the peer', () => {
expect(server._peers.add).toHaveBeenCalledWith(peerInfo, connection);
});

it('calls into _receive with the peer', () => {
expect(server._receive).toHaveBeenCalledWith(peer);
});
});

describe('_receive', () => {
beforeEach(() => {
server._handleMessage = jest.fn(() => true);
});

it('reads the stream, handles the message', () => {
const message = new StatusMessage();
const peer = {
connection: streamWriter(message)
};

server._receive(peer);

expect(server._handleMessage).toHaveBeenCalledWith(
peer, expect.objectContaining({ id: 0 })
);
});
});

describe('_send', () => {
it('does not send without a peer connection', () => {
expect(
server._send({})
).toEqual(false);
});

it('sends the message', (done) => {
const message = new StatusMessage();

server._send({
connection: streamReader((_message) => {
expect(JSON.stringify(_message)).toEqual(JSON.stringify(message));
done();
})
}, message);
});
});
});

0 comments on commit 4496d24

Please sign in to comment.