Skip to content

Commit a7a07a7

Browse files
committed
Adding tests.
1 parent ce8f749 commit a7a07a7

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/tmp

test.js renamed to example.js

File renamed without changes.

medea_ttl.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var bufferEqual = require('buffer-equal');
22

33
module.exports = function(db, options) {
4-
new MedeaTtl(db, options);
4+
if (!db._ttl) {
5+
new MedeaTtl(db, options);
6+
}
7+
58
return db;
69
};
710

@@ -13,6 +16,7 @@ var MedeaTtl = function(db, options) {
1316
options = options || {};
1417

1518
this.db = db;
19+
this.db._ttl = this;
1620
this.frequency = options.frequency || 5 * 60 * 1000;
1721
this.prefix = options.prefix || 'ttl-';
1822

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
"medea": "^0.12.0"
99
},
1010
"devDependencies": {
11-
"medea": "~0.11.0"
11+
"medea": "~0.11.0",
12+
"mocha": "^1.21.4"
1213
},
1314
"scripts": {
14-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"test": "mocha -R spec"
1516
},
1617
"keywords": [
1718
"medea",

test/medea_ttl_test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var assert = require('assert');
2+
var medea = require('medea');
3+
var ttl = require('../');
4+
5+
describe('MedeaTtl', function() {
6+
var db = ttl(medea());
7+
var dir = __dirname + '/tmp/medea_ttl_test';
8+
9+
before(function(done) {
10+
db.open(dir, done);
11+
});
12+
13+
it('should assign itself to db._ttl', function() {
14+
assert(!!db._ttl);
15+
});
16+
17+
describe('#open', function() {
18+
it('should start the cleanup interval', function() {
19+
assert(!!db._ttl.interval);
20+
});
21+
});
22+
23+
describe('#close', function() {
24+
it('should stop the cleanup interval', function(done) {
25+
db.close(function() {
26+
assert(!!db._ttl.interval);
27+
db.open(dir, done);
28+
});
29+
});
30+
});
31+
32+
describe('#put', function() {
33+
it('should take a ttl parameter', function(done) {
34+
db.put('hello', 'world', 1, function(err, val) {
35+
assert(!err);
36+
done();
37+
});
38+
});
39+
});
40+
41+
describe('#get', function() {
42+
it('should return a fresh value', function(done) {
43+
db.put('hello', 'world', 100, function(err, val) {
44+
setTimeout(function() {
45+
db.get('hello', function(err, val) {
46+
assert(!err);
47+
assert.equal(val, 'world');
48+
done();
49+
});
50+
}, 50);
51+
});
52+
});
53+
54+
it('should not return an expired value', function(done) {
55+
db.put('hello', 'world', 50, function(err, val) {
56+
setTimeout(function() {
57+
db.get('hello', function(err, val) {
58+
assert(!err);
59+
assert(!val);
60+
done();
61+
});
62+
}, 50);
63+
});
64+
});
65+
});
66+
67+
describe('#remove', function() {
68+
it('should auto-expire a ttl value', function(done) {
69+
db.put('hello', 'world', 10000, function(err, val) {
70+
db.remove('hello', function(err) {
71+
db.get('hello', function(err, val) {
72+
assert(!err);
73+
assert(!val);
74+
done();
75+
});
76+
});
77+
});
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)