Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 21 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
7 changes: 6 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ Documentation:

### Added

- Add `isContractInitOptions` method (#6455)
- `SocketProvider` now contains public function `getPendingRequestQueueSize`, `getSentRequestsQueueSize` (#6451)
- Added `safeDisconnect` as a `SocketProvider` method to disconnect only when request queue size and send request queue size is 0 (#6451)

### Fixed

- Add `isContractInitOptions` method (#6455)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be still under Added section. But I noticed that the PR number was wrong (was 6455 but should be 6555), right?

Suggested change
### Fixed
- Add `isContractInitOptions` method (#6455)
- Add `isContractInitOptions` method (#6555)
### Fixed

39 changes: 39 additions & 0 deletions packages/web3-utils/src/socket_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ export abstract class SocketProvider<
protected _validateProviderPath(path: string): boolean {
return !!path;
}

/**
*
* @returns the pendingRequestQueue size
*/
// eslint-disable-next-line class-methods-use-this
public getPendingRequestQueueSize() {
return this._pendingRequestsQueue.size;
}

/**
*
* @returns the sendPendingRequests size
*/
// eslint-disable-next-line class-methods-use-this
public getSentRequestsQueueSize() {
return this._sentRequestsQueue.size;
}

/**
*
Expand Down Expand Up @@ -331,6 +349,27 @@ export abstract class SocketProvider<
this._onDisconnect(disconnectCode, data);
}

/**
* Safely disconnects the socket, async and waits for request size to be 0 before disconnecting
* @param code - The code to be sent to the server
* @param data - The data to be sent to the server
*/
public async safeDisconnect(code?: number, data?: string) {
const checkQueue = async () =>
new Promise(resolve => {
const interval = setInterval(() => {
if (this.getPendingRequestQueueSize() === 0 && this.getSentRequestsQueueSize() === 0) {
clearInterval(interval);
resolve(true);
}
}, 1000)
})

await checkQueue();
this.disconnect(code, data);
}


/**
* Removes all listeners for the specified event type.
* @param type - The event type to remove the listeners for
Expand Down
5 changes: 4 additions & 1 deletion packages/web3-utils/test/unit/socket_provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('SocketProvider', () => {
});
// @ts-expect-error run protected method
expect(provider._pendingRequestsQueue.size).toBe(1);

expect(provider.getPendingRequestQueueSize()).toBe(1);
const payload2 = { id: 2, method: 'some_rpc_method' };
provider.setStatus('connected');
const req2 = provider.request(payload2);
Expand All @@ -323,6 +323,7 @@ describe('SocketProvider', () => {

// @ts-expect-error run protected method
expect(provider._sentRequestsQueue.size).toBe(1);
expect(provider.getSentRequestsQueueSize).toBe(1);

provider.on('error', () => {
// nothing
Expand All @@ -331,8 +332,10 @@ describe('SocketProvider', () => {
provider._clearQueues();
// @ts-expect-error run protected method
expect(provider._pendingRequestsQueue.size).toBe(0);
expect(provider.getPendingRequestQueueSize()).toBe(0);
// @ts-expect-error run protected method
expect(provider._sentRequestsQueue.size).toBe(0);
expect(provider.getSentRequestsQueueSize).toBe(0);
});
});
});
Expand Down
48 changes: 48 additions & 0 deletions packages/web3/test/integration/ws.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { WebSocketProvider } from 'web3-providers-ws';
import {
describeIf, getSystemTestProvider, isWs,
} from '../shared_fixtures/system_tests_utils';
import Web3 from '../../src/index';



describe('Web3 instance', () => {
let web3: Web3;

beforeEach(() => {
const provider = getSystemTestProvider();
web3 = new Web3(provider);
});

describeIf(isWs)('web3 ws tests', () => {
it('should connect and disconnect using safe disconnect subscription successfully', async () => {
try {
const subscription = await web3.eth.subscribe("newBlockHeaders");
// eslint-disable-next-line
subscription.unsubscribe();
await (web3.currentProvider as WebSocketProvider).safeDisconnect();
} catch(error) {
// if it errors, fail test
// eslint-disable-next-line
expect(true).toBe(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

pls use some standard way of checking like,

expect(myAsyncFunction()).resolves.not.toThrow()
expect(myAsyncFunction()).resolves.not.toThrowError();

Copy link
Contributor

Choose a reason for hiding this comment

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

}
});
});
});