Skip to content

Commit

Permalink
Merge pull request #3347 from ethereum/pri/isolation-tests
Browse files Browse the repository at this point in the history
Add subscription tests for disconnect & setProvider (#3190)
  • Loading branch information
nivida authored Jan 30, 2020
2 parents 46ebb24 + bca6a58 commit 2223335
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
58 changes: 57 additions & 1 deletion test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;
let web3;
let accounts;
let subscription;
const port = 8545;
const Web3 = getWeb3();

beforeEach(async function () {
server = ganache.server({port: port, blockTime: 1});
Expand Down Expand Up @@ -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');
});
Expand Down
7 changes: 6 additions & 1 deletion test/helpers/test.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ 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,
mine: mine,
extractReceipt: extractReceipt,
getWeb3: getWeb3,
getWebsocketPort: getWebsocketPort,
waitSeconds: waitSeconds
};

0 comments on commit 2223335

Please sign in to comment.