Skip to content

Commit c1f97b7

Browse files
authored
Fix autofinish when attempts equals maxAttempts (#204)
1 parent 117e56f commit c1f97b7

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ w.on('closed', () => {
286286

287287
Changes
288288
-------
289+
* **0.9.3**
290+
* Fix off by one error for Message maxAttempts. (Thanks @tomc974)
289291
* **0.9.2**
290292
* Fix `Reader.close` to cleanup all intervals to allow node to exit cleanly.
291293
* Upraded Sinon

lib/reader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class Reader extends EventEmitter {
247247
process.nextTick(() => {
248248
const autoFinishMessage =
249249
this.config.maxAttempts > 0 &&
250-
this.config.maxAttempts <= message.attempts
250+
this.config.maxAttempts < message.attempts
251251
const numDiscardListeners = this.listeners(Reader.DISCARD).length
252252

253253
if (autoFinishMessage && numDiscardListeners > 0) {

test/reader_test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ describe('reader', () => {
1212

1313
describe('max attempts', () =>
1414
describe('exceeded', () => {
15+
it('should process msg while attempts do not exceed max', done => {
16+
const maxAttempts = 1
17+
const reader = readerWithAttempts(maxAttempts)
18+
19+
reader.on(nsq.Reader.DISCARD, () => {
20+
done(new Error('should not be discarded'))
21+
})
22+
reader.on(nsq.Reader.MESSAGE, () => done())
23+
reader.handleMessage({attempts: 1, finish: () => {}})
24+
})
25+
1526
it('should finish after exceeding specified max attempts', done => {
1627
const maxAttempts = 2
1728
const reader = readerWithAttempts(maxAttempts)
1829

1930
// Message that has exceed the maximum number of attempts
2031
const message = {
21-
attempts: maxAttempts,
32+
attempts: maxAttempts + 1,
2233
finish: sinon.spy()
2334
}
2435

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

3748
const message = {
38-
attempts: maxAttempts,
49+
attempts: maxAttempts + 1,
3950
finish () {}
4051
}
4152

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

5061
const message = {
51-
attempts: maxAttempts,
62+
attempts: maxAttempts + 1,
5263
finish () {}
5364
}
5465

0 commit comments

Comments
 (0)