Skip to content

Commit 1d7d704

Browse files
committed
Refactor stop()
1 parent aa183d8 commit 1d7d704

File tree

2 files changed

+32
-56
lines changed

2 files changed

+32
-56
lines changed

level-ttl.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function buildQuery (db) {
2626
}
2727
}
2828

29-
function startTtl (db, checkFrequency) {
29+
function startTtl (db, checkFrequency, setInterval) {
3030
db._ttl.intervalId = setInterval(function () {
3131
if (db._ttl._checkInProgress) return
3232

@@ -80,15 +80,7 @@ function startTtl (db, checkFrequency) {
8080
if (err) db.emit('error', err)
8181

8282
db._ttl._checkInProgress = false
83-
84-
// Exposed for unit tests
85-
// TODO: also use this for _stopAfterCheck
8683
db.emit('ttl:sweep')
87-
88-
if (db._ttl._stopAfterCheck) {
89-
stopTtl(db, db._ttl._stopAfterCheck)
90-
db._ttl._stopAfterCheck = null
91-
}
9284
}
9385
}, checkFrequency)
9486

@@ -98,15 +90,15 @@ function startTtl (db, checkFrequency) {
9890
}
9991

10092
function stopTtl (db, callback) {
93+
db._ttl.options.clearInterval.call(null, db._ttl.intervalId)
94+
10195
// can't close a db while an interator is in progress
10296
// so if one is, defer
10397
if (db._ttl._checkInProgress) {
104-
db._ttl._stopAfterCheck = callback
105-
// TODO do we really need to return the callback here?
106-
return db._ttl._stopAfterCheck
98+
db.once('ttl:sweep', callback)
99+
} else {
100+
process.nextTick(callback)
107101
}
108-
clearInterval(db._ttl.intervalId)
109-
callback()
110102
}
111103

112104
function ttlon (db, keys, ttl, callback) {
@@ -268,6 +260,7 @@ function close (db, callback) {
268260
}
269261

270262
stopTtl(db, function () {
263+
// TODO: when/why is db._ttl not defined?
271264
if (db._ttl && typeof db._ttl.close === 'function') {
272265
return db._ttl.close.call(db, callback)
273266
}
@@ -286,6 +279,8 @@ function setup (db, options) {
286279
expiryNamespace: 'x',
287280
separator: '!',
288281
checkFrequency: 10000,
282+
setInterval: global.setInterval,
283+
clearInterval: global.clearInterval,
289284
defaultTTL: 0
290285
}, options)
291286

@@ -312,7 +307,7 @@ function setup (db, options) {
312307
// we must intercept close()
313308
db.close = close.bind(null, db)
314309

315-
startTtl(db, options.checkFrequency)
310+
startTtl(db, options.checkFrequency, options.setInterval)
316311

317312
return db
318313
}

test.js

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -517,57 +517,38 @@ test.skip('del removes both key and its ttl meta data (custom ttlEncoding)', fun
517517
}, { valueEncoding: 'utf8' })
518518
}, { keyEncoding: 'utf8', valueEncoding: 'json', ttlEncoding: bytewise })
519519

520-
// TODO: rewrite to be less sensitive and more a unit test
521-
// eslint-disable-next-line no-unused-vars
522-
function wrappedTest () {
523-
var intervals = 0
524-
var _setInterval = global.setInterval
525-
var _clearInterval = global.clearInterval
526-
527-
global.setInterval = function () {
528-
intervals++
529-
return _setInterval.apply(global, arguments)
530-
}
531-
532-
global.clearInterval = function () {
533-
intervals--
534-
return _clearInterval.apply(global, arguments)
535-
}
520+
{
521+
let intervals = 0
536522

537-
test('test stop() method stops interval and doesn\'t hold process up', function (t, db) {
523+
test('test stop() method stops interval', function (t, db) {
538524
t.equals(intervals, 1, '1 interval timer')
539-
db.put('foo', 'bar1', { ttl: 25 })
540525

541-
setTimeout(function () {
542-
db.get('foo', function (err, value) {
543-
t.notOk(err, 'no error')
544-
t.equal('bar1', value)
545-
})
546-
}, 40)
526+
db.put('foo', 'bar1', { ttl: 25 }, function (err) {
527+
t.ifError(err, 'no put error')
547528

548-
setTimeout(function () {
549-
db.get('foo', function (err, value) {
550-
t.ok(err && err.notFound, 'not found error')
551-
t.notOk(value, 'no value')
552-
})
553-
}, 80)
529+
waitForSweep(db, function () {
530+
db.get('foo', function (err) {
531+
t.ok(err && err.notFound, 'not found error')
554532

555-
setTimeout(function () {
556-
db.stop(function () {
557-
db._ttl.close(function () {
558-
global.setInterval = _setInterval
559-
global.clearInterval = _clearInterval
560-
t.equals(0, intervals, 'all interval timers cleared')
561-
t.end()
533+
db.stop(function () {
534+
t.equals(0, intervals, 'all interval timers cleared')
535+
db._ttl.close(t.end.bind(t))
536+
})
562537
})
563538
})
564-
}, 120)
539+
})
540+
}, {
541+
setInterval: function () {
542+
intervals++
543+
return setInterval.apply(null, arguments)
544+
},
545+
clearInterval: function () {
546+
intervals--
547+
return clearInterval.apply(null, arguments)
548+
}
565549
})
566550
}
567551

568-
// TODO: restore
569-
// wrappedTest()
570-
571552
// TODO: rewrite to be less sensitive and more a unit test
572553
function put (timeout, opts) {
573554
return function (t, db) {

0 commit comments

Comments
 (0)