Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket provider fix 6416 #6496

Merged
merged 9 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
6 changes: 5 additions & 1 deletion packages/web3-providers-ipc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ Documentation:

- Dependencies updated

## [Unreleased]
## [Unreleased]

### Fixed

- Fixed bug in chunks processing logic (#6496)
6 changes: 5 additions & 1 deletion packages/web3-providers-ws/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ Documentation:
- Dependencies updated


## [Unreleased]
## [Unreleased]

### Fixed

- Fixed bug in chunks processing logic (#6496)
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import {
waitForEvent,
describeIf,
getSystemTestBackend,
isWs,
} from '../fixtures/system_test_utils';
import WebSocketProvider from '../../src/index';

// create helper functions to open server
describeIf(getSystemTestBackend() === 'ganache')('ganache tests', () => {
describeIf(getSystemTestBackend() === 'ganache' && isWs)('ganache tests', () => {
describe('WebSocketProvider - ganache', () => {
jest.setTimeout(17000);
const port = 7547;
Expand Down Expand Up @@ -232,26 +233,21 @@ describeIf(getSystemTestBackend() === 'ganache')('ganache tests', () => {
const webSocketProvider = new WebSocketProvider(host, {}, reconnectionOptions);
await waitForSocketConnect(webSocketProvider);

webSocketProvider.on('error', (err: any) => {
if (err.message === `Maximum number of reconnect attempts reached! (${1})`) {
mockCallBack();
}
});

await server.close();

const errorPromise = new Promise(resolve => {
webSocketProvider.on('error', (err: any) => {
if (err.message === `Maximum number of reconnect attempts reached! (${1})`) {
mockCallBack();
resolve(true);
}
});
});
// send an event to be parsed and fail
const event = {
data: 'abc|--|ded',
type: 'websocket',
// @ts-expect-error run protected method
target: webSocketProvider._socketConnection,
};
// @ts-expect-error run protected method
webSocketProvider._onMessage(event);
await errorPromise;
// when server is not listening send request, and expect that lib will try to reconnect and at end will throw con not open error
await expect(
webSocketProvider.request(
{"method":"eth_getBlockByNumber","params":["0xc5043f",false],"id":1,"jsonrpc":"2.0"}
))
.rejects.toThrow(ConnectionNotOpenError);

expect(mockCallBack).toHaveBeenCalled();
});

Expand Down
4 changes: 4 additions & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,7 @@ Documentation:
### Added

- As a replacment of the node EventEmitter, a custom `EventEmitter` has been implemented and exported. (#6398)

### Fixed

- Fixed bug in chunks processing logic (#6496)
7 changes: 2 additions & 5 deletions packages/web3-utils/src/socket_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,10 @@ export abstract class SocketProvider<

protected _onMessage(event: MessageEvent): void {
const responses = this._parseResponses(event);
if (responses.length === 0) {
// no responses means lost connection, autoreconnect if possible
if (this._reconnectOptions.autoReconnect) {
this._reconnect();
}
if (isNullish(responses) || responses.length === 0) {
return;
}

for (const response of responses) {
if (
jsonRpc.isResponseWithNotification(response as JsonRpcNotification) &&
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/test/unit/socket_provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ describe('SocketProvider', () => {
// @ts-expect-error run protected method
expect(provider._reconnect).not.toHaveBeenCalled();
});
it('should be called when { autoReconnect: true }', () => {
it('should not call _reconnect with empty response when { autoReconnect: true }', () => {
const reconnectOptions = { autoReconnect: true };
const provider = new TestProvider(socketPath, socketOption, reconnectOptions);
// @ts-expect-error run protected method
jest.spyOn(provider, '_reconnect').mockReturnValue('');
provider.message('');
// @ts-expect-error run protected method
expect(provider._reconnect).toHaveBeenCalled();
expect(provider._reconnect).not.toHaveBeenCalled();
});
});

Expand Down
Loading