Skip to content

Commit

Permalink
Merge pull request #18 from Level/destroy-tests
Browse files Browse the repository at this point in the history
Destroy tests
  • Loading branch information
juliangruber authored Aug 28, 2017
2 parents b19c7c7 + 66c8eaa commit 55cec40
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
6 changes: 0 additions & 6 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.3.1",
"description": "Turn a leveldown iterator into a readable stream",
"scripts": {
"test": "make test"
"test": "standard && node test.js"
},
"repository": "Level/iterator-stream",
"dependencies": {
Expand Down
80 changes: 77 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var data = [
test('setup', function (t) {
db = leveldown(path.join(__dirname, 'db-test'))
db.open(function (err) {
t.error(err)
t.error(err, 'no error')
db.batch(data, function (err) {
t.error(err)
t.error(err, 'no error')
t.end()
})
})
Expand All @@ -40,12 +40,86 @@ test('keys and values', function (t) {
}))
})

test('destroy', function (t) {
test('.destroy closes the stream', function (t) {
var stream = iteratorStream(db.iterator())
stream.on('close', t.end.bind(t))
stream.destroy()
})

test('.destroy during iterator.next 1', function (t) {
var stream
var iterator = db.iterator()
var next = iterator.next.bind(iterator)
iterator.next = function (cb) {
t.pass('should be called once')
next(cb)
stream.destroy()
}
stream = iteratorStream(iterator)
stream.on('data', function (data) {
t.fail('should not be called')
})
stream.on('close', t.end.bind(t))
})

test('.destroy during iterator.next 2', function (t) {
var stream
var iterator = db.iterator()
var next = iterator.next.bind(iterator)
var count = 0
iterator.next = function (cb) {
t.pass('should be called')
next(cb)
if (++count === 2) {
stream.destroy()
}
}
stream = iteratorStream(iterator)
stream.on('data', function (data) {
t.pass('should be called')
})
stream.on('close', t.end.bind(t))
})

test('.destroy after iterator.next 1', function (t) {
var stream
var iterator = db.iterator()
var next = iterator.next.bind(iterator)
iterator.next = function (cb) {
next(function (err, key, value) {
stream.destroy()
cb(err, key, value)
t.pass('should be called')
})
}
stream = iteratorStream(iterator)
stream.on('data', function (data) {
t.fail('should not be called')
})
stream.on('close', t.end.bind(t))
})

test('.destroy after iterator.next 2', function (t) {
var stream
var iterator = db.iterator()
var next = iterator.next.bind(iterator)
var count = 0
iterator.next = function (cb) {
next(function (err, key, value) {
if (++count === 2) {
stream.destroy()
}
cb(err, key, value)
t.pass('should be called')
})
}
stream = iteratorStream(iterator)
stream.on('data', function (data) {
t.pass('should be called')
})
stream.on('close', t.end.bind(t))
})

test('keys=false', function (t) {
var stream = iteratorStream(db.iterator(), { keys: false })
stream.once('data', function (value) {
Expand Down

0 comments on commit 55cec40

Please sign in to comment.