Skip to content

Commit

Permalink
Merge 1cb8b80 into a46dbcd
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Dec 17, 2019
2 parents a46dbcd + 1cb8b80 commit a4ab3ec
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/web3-core-subscriptions/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Subscriptions.prototype.buildCall = function() {
}

var subscription = new Subscription({
subscription: _this.subscriptions[arguments[0]],
subscription: _this.subscriptions[arguments[0]] || {}, // Subscript might not exist
requestManager: _this.requestManager,
type: _this.type
});
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ Subscription.prototype._validateArgs = function (args) {
subscription.params = 0;

if (args.length !== subscription.params) {
throw errors.InvalidNumberOfParams(args.length, subscription.params + 1, args[0]);
throw errors.InvalidNumberOfParams(
args.length,
subscription.params,
subscription.subscriptionName
);
}
};

Expand Down
77 changes: 68 additions & 9 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const ganache = require('ganache-cli');
const pify = require('pify');
const Web3 = require('./helpers/test.utils').getWeb3();

describe('subscription connect/reconnect', function() {
describe.only('subscription connect/reconnect', function() {
let server;
let web3;
let accounts;
let subscription;
const port = 8545;

beforeEach(async function() {
Expand All @@ -24,15 +25,73 @@ describe('subscription connect/reconnect', function() {
}
});

it('subscribes (baseline)', function() {
return new Promise(async function (resolve) {
web3.eth
.subscribe('newBlockHeaders')
.on('data', function(result) {
it('subscribes (baseline)', function(done) {
web3.eth
.subscribe('newBlockHeaders')
.on('data', function(result) {
assert(result.parentHash);
done();
});
});

it('subscribes with a callback', function(done){
subscription = web3.eth
.subscribe('newBlockHeaders', function(err, result){
assert(result.parentHash);
subscription.unsubscribe(); // Stop listening..
done();
});
});

it('resubscribes', function(done){
this.timeout(5000);

let stage = 0;

subscription = web3.eth
.subscribe('newBlockHeaders')
.on('data', function(result) {
assert(result.parentHash);
subscription.unsubscribe();
stage = 1;
});

// Resubscribe from outside
interval = setInterval(async function(){
if (stage === 1){
clearInterval(interval);
subscription.resubscribe();
subscription.on('data', function(result){
assert(result.parentHash);
resolve();
});
});
subscription.unsubscribe(); // Stop listening..
done();
})
}
}, 500)
});

it('allows a subscription which does not exist', function(){
web3.eth.subscribe('subscription-does-not-exists');
});

it('errors when zero params subscrip. is called with the wrong arguments', function(){
try {
web3.eth.subscribe('newBlockHeaders', 5)
assert.fail();
} catch (err) {
assert(err.message.includes('Invalid number of parameters for "newHeads"'))
assert(err.message.includes('Got 1 expected 0'));
}
});

// Could not get the .on('error') version of this to work - maybe a race condition setting it up.
it('errors when the provider is not set', function(done){
web3 = new Web3();

web3.eth.subscribe('newBlockHeaders', function(err, result){
assert(err.message.includes('No provider set'));
done();
})
});

it('errors when the `eth_subscribe` request got send, the reponse isnt returned from the node, and the connection does get closed in the mean time', async function() {
Expand Down

0 comments on commit a4ab3ec

Please sign in to comment.