Update Ws provider (#1)
* Remove extra log line * Don't stub WebSocket (available on Node) * Expand provider/ws tests
This commit is contained in:
@@ -4,7 +4,3 @@
|
||||
if (typeof fetch === 'undefined') {
|
||||
require('isomorphic-fetch');
|
||||
}
|
||||
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
global.WebSocket = require('ws');
|
||||
}
|
||||
|
||||
+3
-5
@@ -42,7 +42,7 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
|
||||
}
|
||||
|
||||
_onClose = () => {
|
||||
console.log('disconnected from', this._endpoint);
|
||||
// console.log('disconnected from', this._endpoint);
|
||||
|
||||
if (this._autoConnect) {
|
||||
setTimeout(this.connect, 1000);
|
||||
@@ -54,17 +54,15 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
|
||||
}
|
||||
|
||||
_onOpen = () => {
|
||||
console.log('connected to', this._endpoint);
|
||||
// console.log('connected to', this._endpoint);
|
||||
}
|
||||
|
||||
_onMessage = (message: WsMessageType) => {
|
||||
// {"jsonrpc":"2.0","id":2,"result":"0x9ce59a13059e417087c02d3236a0b1cc"}
|
||||
// "jsonrpc": "2.0", "method": "eth_subscription", "params": { "result": {...} }, "subscription": "0x9ce59a13059e417087c02d3236a0b1cc"
|
||||
const response: JsonRpcResponse = JSON.parse(message.data);
|
||||
const handler = this._handlers[response.id];
|
||||
|
||||
if (!handler) {
|
||||
console.error(`Unable to find handler for ${response.id}`);
|
||||
console.error(`Unable to find handler for id=${response.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,60 @@ describe('provider/Ws', () => {
|
||||
}
|
||||
});
|
||||
|
||||
describe('websocket', () => {
|
||||
beforeEach(() => {
|
||||
ws = createWs([]);
|
||||
|
||||
sinon.spy(console, 'error');
|
||||
sinon.spy(console, 'log');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
console.error.restore();
|
||||
console.log.restore();
|
||||
});
|
||||
|
||||
it('sets up the on* handlers', () => {
|
||||
expect(ws._websocket.onclose[0]).to.equal(ws._onClose);
|
||||
expect(ws._websocket.onerror[0]).to.equal(ws._onError);
|
||||
expect(ws._websocket.onmessage[0]).to.equal(ws._onMessage);
|
||||
expect(ws._websocket.onopen[0]).to.equal(ws._onOpen);
|
||||
});
|
||||
|
||||
describe('_onClose', () => {
|
||||
it('reconnects after delay', () => {
|
||||
const clock = sinon.useFakeTimers();
|
||||
|
||||
ws.connect = sinon.stub();
|
||||
ws._onClose();
|
||||
|
||||
expect(ws.connect).not.to.have.been.called;
|
||||
|
||||
clock.tick(1001);
|
||||
|
||||
expect(ws.connect).to.have.been.called;
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('_onError', () => {
|
||||
it('logs the error', () => {
|
||||
ws._onError('test error');
|
||||
|
||||
expect(console.error).to.have.been.calledWith('test error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('_onMessage', () => {
|
||||
it('fails with log when handler not found', () => {
|
||||
ws._onMessage({ data: '{"id":2}' });
|
||||
|
||||
expect(console.error).to.have.been.calledWith('Unable to find handler for id=2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('send', () => {
|
||||
it('handles internal errors', () => {
|
||||
ws = createWs([], false);
|
||||
|
||||
Reference in New Issue
Block a user