diff --git a/packages/web3-providers-ws/src/index.js b/packages/web3-providers-ws/src/index.js index 006a9224a57..73ff7e3cefe 100644 --- a/packages/web3-providers-ws/src/index.js +++ b/packages/web3-providers-ws/src/index.js @@ -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) { diff --git a/test/websocket.ganache.js b/test/websocket.ganache.js index f5525ba94ff..f626fe46c4c 100644 --- a/test/websocket.ganache.js +++ b/test/websocket.ganache.js @@ -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);