Skip to content

Commit

Permalink
Fix TypeError: this.queue.close is not a function (#93)
Browse files Browse the repository at this point in the history
* Fix TypeError: this.queue.close is not a function

In some rare cases there's a possibility when `close` will be called before `subscribe` will happen. In this case that error could cause even app server crash. Also it prevents `this.queue.destroy()` to be called.

I don't know how to properly test this case, cause it's caused by some abnormal behaviour of client side. Tried some options with closing ws client right after establishing connection, sending some crappy staff into it - nothing caused this bug to appear.
Anyway it's present and needed to be fixed.

* fix: ignore coverage

* chore: add comments

* test: add test
  • Loading branch information
SkeLLLa authored and mcollina committed Dec 19, 2019
1 parent 21e7f3a commit 25dbe74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class SubscriptionContext {
}

close () {
this.queue.close()
// In rare cases when `subscribe()` not called (e.g. some network error)
// `close` will be `undefined`.
if (typeof this.queue.close === 'function') {
this.queue.close()
}
this.queue.destroy()
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ test('subscriber published an event', async (t) => {
})
})

test('subscription context not throw error on close', t => {
t.plan(1)
const pubsub = new PubSub(mq())

const sc = new SubscriptionContext({ pubsub })

sc.close()
t.pass()
})

test('subscription context publish event returns a promise', t => {
t.plan(1)
const pubsub = new PubSub(mq())
Expand Down

0 comments on commit 25dbe74

Please sign in to comment.