Skip to content

Commit

Permalink
Merge d21d746 into 80ea81c
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Dec 19, 2019
2 parents 80ea81c + d21d746 commit 24f8135
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 15 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
28 changes: 21 additions & 7 deletions 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 Expand Up @@ -192,18 +196,28 @@ Subscription.prototype.subscribe = function() {
return this;
}

// throw error, if provider is not set
if(!this.options.requestManager.provider) {
var err1 = new Error('No provider set.');
this.callback(err1, null, this);
this.emit('error', err1);
setTimeout(function(){
var err1 = new Error('No provider set.');
_this.callback(err1, null, _this);
_this.emit('error', err1);
},0);

return this;
}

// throw error, if provider doesnt support subscriptions
if(!this.options.requestManager.provider.on) {
var err2 = new Error('The current provider doesn\'t support subscriptions: '+ this.options.requestManager.provider.constructor.name);
this.callback(err2, null, this);
this.emit('error', err2);
setTimeout(function(){
var err2 = new Error(
'The current provider doesn\'t support subscriptions: ' +
_this.options.requestManager.provider.constructor.name
);
_this.callback(err2, null, _this);
_this.emit('error', err2);
},0);

return this;
}

Expand Down
123 changes: 116 additions & 7 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('subscription connect/reconnect', function() {
let server;
let web3;
let accounts;
let subscription;
const port = 8545;

beforeEach(async function() {
Expand All @@ -24,17 +25,125 @@ 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 to an existing subscription', function(done){
this.timeout(5000);

let stage = 0;

subscription = web3.eth.subscribe('newBlockHeaders');

subscription.on('data', function(result){
if (stage === 0) {
subscription.resubscribe();
stage = 1;
return;
}

assert(result.parentHash);
subscription.unsubscribe(); // Stop listening..
done();
});
});

it('resubscribes after being unsubscribed', 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'));
}
});

it('errors when the provider is not set (callback)', function(done){
web3 = new Web3();

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

it('errors when the provider is not set (.on("error"))', function(done){
web3 = new Web3();

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

it('errors when the provider does not support subscriptions (callback)', function(done){
web3 = new Web3('http://localhost:' + port);

web3.eth.subscribe('newBlockHeaders', function(err, result){
assert(err.message.includes("provider doesn't support subscriptions: HttpProvider"));
done();
});
});

it('errors when the provider is not set (.on("error"))', function(done){
web3 = new Web3('http://localhost:' + port);

web3.eth
.subscribe('newBlockHeaders')
.on("error", function(err){
assert(err.message.includes("provider doesn't support subscriptions: HttpProvider"));
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() {
await pify(server.close)();

Expand Down

0 comments on commit 24f8135

Please sign in to comment.