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

Add subscription tests for disconnect & setProvider (#3190) #3347

Merged
merged 4 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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
};