|
| 1 | +var test = require('tap').test |
| 2 | +var level = require('level-mem') |
| 3 | +var spaces = require('level-spaces') |
| 4 | +var ttl = require('../index.js') |
| 5 | + |
| 6 | +test('separators work', function (t) { |
| 7 | + t.plan(14) |
| 8 | + |
| 9 | + var db = level('hello') |
| 10 | + var ttlDb = spaces(db, 'ttl-expiration', { separator: '\xff' }) |
| 11 | + |
| 12 | + ttl(db, { |
| 13 | + ttl: 1000, |
| 14 | + checkInterval: 50, |
| 15 | + separator: '\xff' |
| 16 | + }) |
| 17 | + |
| 18 | + db.put('hi\x00', 'wuzzup', t.notOk.bind(t)) |
| 19 | + db.put('hello\xff', 'coolness', t.notOk.bind(t)) |
| 20 | + |
| 21 | + setTimeout(function () { //before ttl |
| 22 | + db.get('hi\x00', function (err, value) { |
| 23 | + t.notOk(err, 'did not get an error') |
| 24 | + t.notOk(err && err.notFound, 'did not get a notFound error') |
| 25 | + t.equal(value, 'wuzzup', 'got back the expected value') |
| 26 | + |
| 27 | + db.get('hello\xff', function (err, value) { |
| 28 | + t.notOk(err, 'did not get an error') |
| 29 | + t.notOk(err && err.notFound, 'did not get a notFound error') |
| 30 | + t.equal(value, 'coolness', 'got back the expected value') |
| 31 | + }) |
| 32 | + }) |
| 33 | + }, 900) |
| 34 | + |
| 35 | + setTimeout(function () { //after ttl |
| 36 | + db.get('hi\x00', function (err, value) { |
| 37 | + t.ok(err, 'got an error') |
| 38 | + t.ok(err && err.notFound, 'got a notFound error') |
| 39 | + t.notOk(value, 'did not get a value') |
| 40 | + |
| 41 | + db.get('hello\xff', function (err, value) { |
| 42 | + t.notOk(err, 'did not get an error') |
| 43 | + t.notOk(err && err.notFound, 'did not get a notFound error') |
| 44 | + t.equal(value, 'coolness', 'got back the expected value') |
| 45 | + |
| 46 | + t.end() |
| 47 | + }) |
| 48 | + }) |
| 49 | + }, 1100) |
| 50 | +}) |
| 51 | + |
| 52 | +test('separators work', function (t) { |
| 53 | + t.plan(6) |
| 54 | + |
| 55 | + var db = level('hello') |
| 56 | + var ttlDb = spaces(db, 'ttl-expiration', { separator: 'x' }) |
| 57 | + |
| 58 | + ttl(db, { |
| 59 | + ttl: 1000, |
| 60 | + checkInterval: 50, |
| 61 | + separator: 'x' |
| 62 | + }) |
| 63 | + |
| 64 | + db.put('hi', 'wuzzup', t.notOk.bind(t)) |
| 65 | + db.put('hellox', 'coolness', t.notOk.bind(t)) |
| 66 | + |
| 67 | + setTimeout(function () { //before ttl |
| 68 | + db.get('hi', function (err, value) { |
| 69 | + t.equal(value, 'wuzzup') |
| 70 | + }) |
| 71 | + db.get('hellox', function (err, value) { |
| 72 | + t.equal(value, 'coolness') |
| 73 | + }) |
| 74 | + }, 900) |
| 75 | + |
| 76 | + setTimeout(function () { //after ttl |
| 77 | + db.get('hi\x00', function (err, value) { |
| 78 | + t.notOk(value) |
| 79 | + db.get('hellox', function (err, value) { |
| 80 | + t.equal(value, 'coolness') |
| 81 | + |
| 82 | + t.end() |
| 83 | + }) |
| 84 | + }) |
| 85 | + }, 1100) |
| 86 | +}) |
0 commit comments