Skip to content

Commit

Permalink
Fix unsubscribe and add a test (#4058)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex <alex@chainsafe.io>
Co-authored-by: Wyatt Barnes <wyatt@writerof.software>
  • Loading branch information
3 people authored Sep 8, 2021
1 parent 1547b18 commit 37d3f7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ Subscription.prototype.subscribe = function() {
if(!err && result) {
_this.id = result;
_this.method = payload.params[0];
_this.emit('connected', result);

// call callback on notifications
_this.options.requestManager.addSubscription(_this, function(error, result) {
Expand Down Expand Up @@ -310,6 +309,7 @@ Subscription.prototype.subscribe = function() {
_this.emit('error', error);
}
});
_this.emit('connected', result);
} else {
setTimeout(function(){
_this.callback(err, false, _this);
Expand Down
12 changes: 12 additions & 0 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe('subscription connect/reconnect', function () {
});
});

it('unsubscribe should remove the subscription object from the subscriptions and send eth_unsubscribe to the node', function (done) {
subscription = web3.eth
.subscribe('newBlockHeaders')
.on('connected', function () {
const id = subscription.id;
assert(subscription.options.requestManager.subscriptions.has(id));
subscription.unsubscribe(); // Send eth_unsubscribe to the node
assert(!subscription.options.requestManager.subscriptions.has(id));
done();
});
});

it('clearSubscriptions', async function() {
web3.eth.subscribe('newBlockHeaders');
await waitSeconds(1); // Sub need a little time to set up
Expand Down

0 comments on commit 37d3f7a

Please sign in to comment.