diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index da94f6e587d..a34a5bfc047 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -1,7 +1,7 @@ const assert = require('assert'); const ganache = require('ganache-cli'); const pify = require('pify'); -const Web3 = require('./helpers/test.utils').getWeb3(); +const { getWeb3, waitSeconds } = require('./helpers/test.utils'); describe('subscription connect/reconnect', function () { let server; @@ -9,6 +9,7 @@ describe('subscription connect/reconnect', function () { let accounts; let subscription; const port = 8545; + const Web3 = getWeb3(); beforeEach(async function () { server = ganache.server({port: port, blockTime: 1}); @@ -100,6 +101,61 @@ describe('subscription connect/reconnect', function () { }, 500); }); + // The ganache unit tests are erroring under similar conditions - + it('does not error when client closes after disconnect', async function(){ + this.timeout(7000); + + return new Promise(async function(resolve, reject) { + web3.eth + .subscribe('newBlockHeaders') + .once("error", function (err) { + reject(new Error('Should not hear an error ')); + }); + + // Let a couple blocks mine.. + await waitSeconds(2) + web3.currentProvider.disconnect(); + + // This delay seems to be required (on Travis). + await waitSeconds(1); + + await pify(server.close)(); + + await waitSeconds(1) + resolve(); + }); + }); + + // Verify subscription cleanup on setProvider + it('does not hear old subscriptions after setting a new provider', async function(){ + this.timeout(7000); + let counter = 0; + + return new Promise(async function(resolve, reject) { + web3.eth + .subscribe('newBlockHeaders') + .on("data", function (_) { + counter++; + }); + + // Let a couple blocks mine.. + await waitSeconds(2) + assert(counter >= 1); + + // Connect to a different client; + const newServer = ganache.server({port: 8777, blockTime: 1}); + await pify(newServer.listen)(8777); + + const finalCount = counter; + web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8777')); + + await waitSeconds(2); + assert.equal(counter, finalCount); + await pify(newServer.close)(); + resolve(); + }); + }) + it('allows a subscription which does not exist', function () { web3.eth.subscribe('subscription-does-not-exists'); }); diff --git a/test/helpers/test.utils.js b/test/helpers/test.utils.js index e4006ff27e8..67922e03aff 100644 --- a/test/helpers/test.utils.js +++ b/test/helpers/test.utils.js @@ -51,6 +51,11 @@ var getWebsocketPort = function(){ return ( process.env.GANACHE || global.window ) ? 8545 : 8546; } +// Delay +var waitSeconds = async function(seconds = 0){ + return new Promise(resolve => setTimeout(() => resolve(), seconds * 1000)) +} + module.exports = { methodExists: methodExists, propertyExists: propertyExists, @@ -58,5 +63,5 @@ module.exports = { extractReceipt: extractReceipt, getWeb3: getWeb3, getWebsocketPort: getWebsocketPort, + waitSeconds: waitSeconds }; -