Skip to content

Commit

Permalink
Run initially emitted errors in a zero-length timeout (plus tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Dec 19, 2019
1 parent ac880f6 commit d21d746
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
22 changes: 16 additions & 6 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,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
34 changes: 32 additions & 2 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ describe('subscription connect/reconnect', function() {
}
});

// 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){
it('errors when the provider is not set (callback)', function(done){
web3 = new Web3();

web3.eth.subscribe('newBlockHeaders', function(err, result){
Expand All @@ -114,6 +113,37 @@ describe('subscription connect/reconnect', function() {
})
});

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 d21d746

Please sign in to comment.