Skip to content

Commit

Permalink
Fix autofinish when attempts equals maxAttempts (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
dudleycarr authored Nov 21, 2017
1 parent 117e56f commit c1f97b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ w.on('closed', () => {

Changes
-------
* **0.9.3**
* Fix off by one error for Message maxAttempts. (Thanks @tomc974)
* **0.9.2**
* Fix `Reader.close` to cleanup all intervals to allow node to exit cleanly.
* Upraded Sinon
Expand Down
2 changes: 1 addition & 1 deletion lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Reader extends EventEmitter {
process.nextTick(() => {
const autoFinishMessage =
this.config.maxAttempts > 0 &&
this.config.maxAttempts <= message.attempts
this.config.maxAttempts < message.attempts
const numDiscardListeners = this.listeners(Reader.DISCARD).length

if (autoFinishMessage && numDiscardListeners > 0) {
Expand Down
17 changes: 14 additions & 3 deletions test/reader_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ describe('reader', () => {

describe('max attempts', () =>
describe('exceeded', () => {
it('should process msg while attempts do not exceed max', done => {
const maxAttempts = 1
const reader = readerWithAttempts(maxAttempts)

reader.on(nsq.Reader.DISCARD, () => {
done(new Error('should not be discarded'))
})
reader.on(nsq.Reader.MESSAGE, () => done())
reader.handleMessage({attempts: 1, finish: () => {}})
})

it('should finish after exceeding specified max attempts', done => {
const maxAttempts = 2
const reader = readerWithAttempts(maxAttempts)

// Message that has exceed the maximum number of attempts
const message = {
attempts: maxAttempts,
attempts: maxAttempts + 1,
finish: sinon.spy()
}

Expand All @@ -35,7 +46,7 @@ describe('reader', () => {
const reader = readerWithAttempts(maxAttempts)

const message = {
attempts: maxAttempts,
attempts: maxAttempts + 1,
finish () {}
}

Expand All @@ -48,7 +59,7 @@ describe('reader', () => {
const reader = readerWithAttempts(maxAttempts)

const message = {
attempts: maxAttempts,
attempts: maxAttempts + 1,
finish () {}
}

Expand Down

0 comments on commit c1f97b7

Please sign in to comment.