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

Throw when requests made after WS reconnect attempts are exhausted #3494

Merged
merged 5 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ WebsocketProvider.prototype.reconnect = function () {
}

this.emit(this.ERROR, errors.MaxAttemptsReachedOnReconnectingError());
this.reconnecting = false;

if (this.requestQueue.size > 0) {
this.requestQueue.forEach(function (request, key) {
Expand Down
60 changes: 60 additions & 0 deletions test/websocket.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,66 @@ describe('WebsocketProvider (ganache)', function () {
});
});

it('errors when call is made after max. configured attempts has elapsed', function () {
this.timeout(6000);

return new Promise(async function (resolve) {
server = ganache.server({port: port});
await pify(server.listen)(port);

web3 = new Web3(
new Web3.providers.WebsocketProvider(
host + port,
{reconnect: {auto: true, maxAttempts: 1, delay: 1000,}}
)
);

web3.currentProvider.once('connect', async function () {
await pify(server.close)();
await utils.waitSeconds(2);

try {
await web3.eth.getBlockNumber();
} catch (err) {
assert(err.message.includes('connection not open on send()'));
resolve();
}
});
});
});

it('does not auto reconnect after max. configured attempts has elapsed', function () {
this.timeout(6000);

return new Promise(async function (resolve) {
server = ganache.server({port: port});
await pify(server.listen)(port);

web3 = new Web3(
new Web3.providers.WebsocketProvider(
host + port,
{reconnect: {auto: true, maxAttempts: 1, delay: 1000,}}
)
);

web3.currentProvider.once('connect', async function () {
// Close and then re-open server after
// reconnection window has elapsed.
await pify(server.close)();
await utils.waitSeconds(2);
await pify(server.listen)(port);

try {
await web3.eth.getBlockNumber();
} catch (err){
assert(err.message.includes('connection not open on send()'));
resolve();
}

});
});
});

it('allows disconnection when reconnect is enabled', function () {
this.timeout(6000);

Expand Down