Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

fix: mark the socket as closed when receiving a close/error message #25180

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4174,13 +4174,15 @@ export class Connection {
* @internal
*/
_wsOnError(err: Error) {
this._rpcWebSocketConnected = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MDN says:

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

console.error('ws error:', err.message);
}

/**
* @internal
*/
_wsOnClose(code: number) {
this._rpcWebSocketConnected = false;
this._rpcWebSocketGeneration++;
if (this._rpcWebSocketHeartbeat) {
clearInterval(this._rpcWebSocketHeartbeat);
Expand Down
26 changes: 26 additions & 0 deletions web3.js/test/connection-subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,36 @@ describe('Subscriptions', () => {
);
});
});
describe('then having the socket connection error', () => {
beforeEach(() => {
stubbedSocket.emit(
'error',
new Error('A bad thing happened to the socket'),
);
});
describe('making another subscription while disconnected', () => {
beforeEach(() => {
stubbedSocket.call.resetHistory();
setupListener(spy());
});
it('does not issue an RPC call', () => {
expect(stubbedSocket.call).not.to.have.been.called;
});
});
});
describe('then having the socket connection drop unexpectedly', () => {
beforeEach(() => {
stubbedSocket.emit('close');
});
describe('making another subscription while disconnected', () => {
beforeEach(() => {
stubbedSocket.call.resetHistory();
setupListener(spy());
});
it('does not issue an RPC call', () => {
expect(stubbedSocket.call).not.to.have.been.called;
});
});
describe('upon the socket connection reopening', () => {
let fatalPriorUnubscribe;
beforeEach(() => {
Expand Down