Skip to content

Commit

Permalink
Close dbs (#167)
Browse files Browse the repository at this point in the history
* rimraf should work, callback with error if not

* close db in approximate-size-test

* assert no error after closing db

* avoid zalgo in cleanup

* close db in custom _serialize test in del-test.js + fix zalgo in ._del

* 🐛 was using value as a global, add missing parameter

* close db in custom _serialize test in get-test.js + fix zalgo

* close db in custom _serialize test in put-test.js + fix zalgo

* put back t.plan() in custom _serialize tests
  • Loading branch information
ralphtheninja authored Dec 23, 2017
1 parent 88b8900 commit d3a4022
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions abstract/approximate-size-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var leveldown
var testCommon

module.exports.setUp = function (_leveldown, test, _testCommon) {
test('setUp common', _testCommon.setUp)
test('setUp common for approximate size', _testCommon.setUp)
test('setUp db', function (t) {
leveldown = _leveldown
testCommon = _testCommon
Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports.args = function (test) {
})

test('test custom _serialize*', function (t) {
t.plan(3)
t.plan(4)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._approximateSize = function (start, end, callback) {
Expand All @@ -79,6 +79,7 @@ module.exports.args = function (test) {
db.open(function () {
db.approximateSize({ foo: 'bar' }, { beep: 'boop' }, function (err) {
t.error(err)
db.close(t.error.bind(t))
})
})
})
Expand Down
7 changes: 4 additions & 3 deletions abstract/del-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ module.exports.args = function (test) {
})

test('test custom _serialize*', function (t) {
t.plan(2)
t.plan(3)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._del = function (key, options, callback) {
t.deepEqual(key, { foo: 'bar' })
callback()
process.nextTick(callback)
}
db.open(function () {
db.del({ foo: 'bar' }, function (err) {
t.error(err)
db.close(t.error.bind(t))
})
})
})
Expand All @@ -63,7 +64,7 @@ module.exports.del = function (test) {
t.error(err)
db.del('foo', function (err) {
t.error(err)
db.get('foo', function (err) {
db.get('foo', function (err, value) {
t.ok(err, 'entry propertly deleted')
t.ok(typeof value === 'undefined', 'value is undefined')
t.ok(verifyNotFoundError(err), 'NotFound error')
Expand Down
5 changes: 3 additions & 2 deletions abstract/get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ module.exports.args = function (test) {
})

test('test custom _serialize*', function (t) {
t.plan(2)
t.plan(3)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._get = function (key, options, callback) {
t.deepEqual(key, { foo: 'bar' })
callback()
process.nextTick(callback)
}
db.open(function () {
db.get({ foo: 'bar' }, function (err) {
t.error(err)
db.close(t.error.bind(t))
})
})
})
Expand Down
7 changes: 4 additions & 3 deletions abstract/put-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,26 @@ module.exports.args = function (test) {
db._put = function (key, value, opts, callback) {
t.ok(key)
t.ok(value)
callback()
process.nextTick(callback)
}
db.put({}, {}, function (err, val) {
t.error(err)
})
})

test('test custom _serialize*', function (t) {
t.plan(3)
t.plan(4)
var db = leveldown(testCommon.location())
db._serializeKey = db._serializeValue = function (data) { return data }
db._put = function (key, value, options, callback) {
t.deepEqual(key, { foo: 'bar' })
t.deepEqual(value, { beep: 'boop' })
callback()
process.nextTick(callback)
}
db.open(function () {
db.put({ foo: 'bar' }, { beep: 'boop' }, function (err) {
t.error(err)
db.close(t.error.bind(t))
})
})
})
Expand Down
5 changes: 3 additions & 2 deletions testCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var lastLocation = function () {
}

var cleanup = function (callback) {
if (process.browser) { return callback() }
if (process.browser) { return process.nextTick(callback) }

fs.readdir(__dirname, function (err, list) {
if (err) return callback(err)
Expand All @@ -27,7 +27,8 @@ var cleanup = function (callback) {
var ret = 0

list.forEach(function (f) {
rimraf(path.join(__dirname, f), function () {
rimraf(path.join(__dirname, f), function (err) {
if (err) return callback(err)
if (++ret === list.length) { callback() }
})
})
Expand Down

0 comments on commit d3a4022

Please sign in to comment.