diff --git a/README.md b/README.md index f9bfc1b..b26b4c1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/reader.js b/lib/reader.js index d86a17b..700a094 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -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) { diff --git a/test/reader_test.js b/test/reader_test.js index e8e938d..87a16d8 100644 --- a/test/reader_test.js +++ b/test/reader_test.js @@ -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() } @@ -35,7 +46,7 @@ describe('reader', () => { const reader = readerWithAttempts(maxAttempts) const message = { - attempts: maxAttempts, + attempts: maxAttempts + 1, finish () {} } @@ -48,7 +59,7 @@ describe('reader', () => { const reader = readerWithAttempts(maxAttempts) const message = { - attempts: maxAttempts, + attempts: maxAttempts + 1, finish () {} }