Skip to content

Commit

Permalink
Fix find cursor close when already paused
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Jul 6, 2016
1 parent c98e9a8 commit ea80ef0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.0.2 / 2016-07-06
==================
- Fix find cursor close when already paused

3.0.1 / 2016-07-06
==================
- Fix find cursor pause and resume
Expand Down
7 changes: 5 additions & 2 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,15 @@ Collection.prototype.find = function (query, opts, fn) {
}
})

cursor.on('end', function () {
function done () {
if (fn) {
fn()
}
resolve()
})
}

cursor.on('close', done)
cursor.on('end', done)

cursor.on('error', function (err) {
if (fn) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monk",
"version": "3.0.1",
"version": "3.0.2",
"main": "lib/monk.js",
"tags": [
"mongodb",
Expand Down
24 changes: 23 additions & 1 deletion test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ test('find > should allow stream cursor destroy', (t) => {
return users.insert([{ cursor: true }, { cursor: true }, { cursor: true }, { cursor: true }]).then(() => {
return users.find(query)
.each((doc, {close}) => {
console.log(found)
t.not(doc.cursor, null)
found++
if (found === 2) close()
Expand All @@ -253,6 +252,29 @@ test('find > should allow stream cursor destroy', (t) => {
})
})

test('find > should allow stream cursor destroy even when paused', (t) => {
const query = { cursor: { $exists: true } }
let found = 0
return users.insert([{ cursor: true }, { cursor: true }, { cursor: true }, { cursor: true }]).then(() => {
return users.find(query)
.each((doc, {close, pause, resume}) => {
pause()
t.not(doc.cursor, null)
found++
if (found === 2) return close()
resume()
})
.then(() => {
return new Promise((resolve) => {
setTimeout(() => {
t.is(found, 2)
resolve()
}, 100)
})
})
})
})

test('find > stream pause and continue', (t) => {
const query = { stream: 4 }
return users.insert([{ stream: 4 }, { stream: 4 }, { stream: 4 }, { stream: 4 }]).then(() => {
Expand Down

0 comments on commit ea80ef0

Please sign in to comment.