From 9f4b28c5881551c7a4376ef79d657b0a97c11111 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 29 Jan 2020 21:48:59 -0800 Subject: [PATCH 1/4] Add subscript behavioral tests for disconnect & setProvider --- test/eth.subscribe.ganache.js | 55 ++++++++++++++++++++++++++++++++++- test/helpers/test.utils.js | 6 ++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index da94f6e587d..0727941358f 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,58 @@ describe('subscription connect/reconnect', function () { }, 500); }); + // The ganache unit tests are erroring under similar conditions - + // test verifies behavior is as expected. + it('does not error when client closes immediately 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(); + 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 (_) { + emitterInstance = this; + 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..969f1f931fd 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,6 @@ module.exports = { extractReceipt: extractReceipt, getWeb3: getWeb3, getWebsocketPort: getWebsocketPort, + waitSeconds: waitSeconds }; From 1984430022286809d31c88b57313344c4a63d7f6 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 29 Jan 2020 23:14:12 -0800 Subject: [PATCH 2/4] Add delay between disconnect and server shutdown --- test/eth.subscribe.ganache.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index 0727941358f..f359849b0a7 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -102,8 +102,7 @@ describe('subscription connect/reconnect', function () { }); // The ganache unit tests are erroring under similar conditions - - // test verifies behavior is as expected. - it('does not error when client closes immediately after disconnect', async function(){ + it('does not error when client closes after disconnect', async function(){ this.timeout(7000); return new Promise(async function(resolve, reject) { @@ -116,7 +115,12 @@ describe('subscription connect/reconnect', function () { // 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(); }); From e78bed7cc8fbae7d6c458523eb3c27a8337cac19 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Thu, 30 Jan 2020 01:48:56 -0800 Subject: [PATCH 3/4] Remove unused emitter ref --- test/eth.subscribe.ganache.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index f359849b0a7..a34a5bfc047 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -135,7 +135,6 @@ describe('subscription connect/reconnect', function () { web3.eth .subscribe('newBlockHeaders') .on("data", function (_) { - emitterInstance = this; counter++; }); From bca6a58235e6909df7015d3fd45e878f984a3a1b Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Thu, 30 Jan 2020 13:05:57 +0100 Subject: [PATCH 4/4] Update test/helpers/test.utils.js --- test/helpers/test.utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/helpers/test.utils.js b/test/helpers/test.utils.js index 969f1f931fd..67922e03aff 100644 --- a/test/helpers/test.utils.js +++ b/test/helpers/test.utils.js @@ -52,7 +52,7 @@ var getWebsocketPort = function(){ } // Delay -var waitSeconds = async function(seconds=0){ +var waitSeconds = async function(seconds = 0){ return new Promise(resolve => setTimeout(() => resolve(), seconds * 1000)) } @@ -65,4 +65,3 @@ module.exports = { getWebsocketPort: getWebsocketPort, waitSeconds: waitSeconds }; -