Skip to content

Commit e6f37c9

Browse files
committed
add more tests
1 parent f333ebf commit e6f37c9

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

lib/redis_pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const logger = require("./logger");
66

77
const debug = require("debug")("simpleRedisPool");
88

9-
function RedisPool (name, redisOptions={}, poolOptions={}) {
9+
function RedisPool (name, redisOptions, poolOptions) {
1010

1111
this._redisDb = redisOptions.db || 0;
1212

lib/redis_store.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,105 +9,101 @@ function RedisStore (name, redisOptions, poolOptions) {
99
const pool = new RedisPool(name, redisOptions, poolOptions);
1010
logger.info("Redis store created.", pool.status());
1111

12-
function release (conn) {
13-
return pool.release(conn);
14-
}
15-
1612
this.getName = bind(pool.getName, pool);
1713
this.getDB = bind(pool.getDB, pool);
1814
this.status = bind(pool.status, pool);
1915

20-
this.get = function (key) {
16+
this.get = (key) => {
2117

2218
return pool.acquire()
23-
.then(function (conn) {
19+
.then((conn) => {
2420
return conn.getAsync(key)
25-
.then(function (result) {
26-
release(conn);
21+
.then((result) => {
22+
pool.release(conn);
2723
return result;
2824
});
2925
});
3026
};
3127

32-
this.set = function (key, value) {
28+
this.set = (key, value) => {
3329

3430
return pool.acquire()
35-
.then(function (conn) {
31+
.then((conn) => {
3632
return conn.setAsync(key, value)
37-
.then(function (result) {
38-
release(conn);
33+
.then((result) => {
34+
pool.release(conn);
3935
return result;
4036
});
4137
});
4238
};
4339

44-
this.setex = function (key, value, ttl) {
40+
this.setex = (key, value, ttlInSeconds) => {
4541

4642
return pool.acquire()
47-
.then(function (conn) {
48-
return conn.setexAsync(key, ttl, value)
49-
.then(function (result) {
50-
release(conn);
43+
.then((conn) => {
44+
return conn.setexAsync(key, ttlInSeconds, value)
45+
.then((result) => {
46+
pool.release(conn);
5147
return result;
5248
});
5349
});
5450
};
5551

56-
this.del = function (key) {
52+
this.del = (key) => {
5753

5854
return pool.acquire()
59-
.then(function (conn) {
55+
.then((conn) => {
6056
return conn.delAsync(key)
61-
.then(function (result) {
62-
release(conn);
57+
.then((result) => {
58+
pool.release(conn);
6359
return result;
6460
});
6561
});
6662
};
6763

68-
this.ttl = function (key) {
64+
this.ttlInSeconds = (key) => {
6965

7066
return pool.acquire()
71-
.then(function (conn) {
72-
return conn.ttlAsync(key)
73-
.then(function (result) {
74-
release(conn);
67+
.then((conn) => {
68+
return conn.ttlInSecondsAsync(key)
69+
.then((result) => {
70+
pool.release(conn);
7571
return result;
7672
});
7773
});
7874
};
7975

80-
this.keys = function (pattern) {
76+
this.keys = (pattern) => {
8177
if (!pattern || pattern === "") {
8278
pattern = "*";
8379
}
8480

8581
return pool.acquire()
86-
.then(function (conn) {
82+
.then((conn) => {
8783
return conn.keysAsync(pattern)
88-
.then(function (result) {
89-
release(conn);
84+
.then((result) => {
85+
pool.release(conn);
9086
return result;
9187
});
9288
});
9389
};
9490

9591

96-
this.deleteAll = function (pattern) {
92+
this.deleteAll = (pattern) => {
9793
if (!pattern || pattern === "") {
9894
pattern = "*";
9995
}
10096
logger.info("clearing redis keys: ", pattern);
10197

10298
return pool.acquire()
103-
.then(function (conn) {
99+
.then((conn) => {
104100
return conn.keysAsync(pattern)
105-
.then(function (keys) {
101+
.then((keys) => {
106102
if (keys.length > 0) {
107103
debug("deleting keys ", keys);
108104
return conn.delAsync(keys)
109-
.then(function (result) {
110-
release(conn);
105+
.then((result) => {
106+
pool.release(conn);
111107
return result;
112108
});
113109
}

test/index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require("should");
1+
const should = require("should");
2+
const Bluebird = require("bluebird");
23
const RedisStore = require("../lib/redis_store");
34

45
describe("redisStore", function () {
@@ -8,6 +9,28 @@ describe("redisStore", function () {
89
});
910
const store = new RedisStore("testStore", redisOptions);
1011

12+
describe("get", function () {
13+
14+
const key = "chuck-norris";
15+
const value = "superman";
16+
17+
before(function (done) {
18+
store.set(key, value)
19+
.then(function () {
20+
done();
21+
});
22+
});
23+
24+
it("get", function (done) {
25+
26+
store.get(key)
27+
.then(function (v) {
28+
v.should.be.equal(value);
29+
done();
30+
});
31+
});
32+
});
33+
1134
describe("set", function () {
1235
it("set", function (done) {
1336

@@ -19,25 +42,32 @@ describe("redisStore", function () {
1942
});
2043
});
2144

22-
describe("get", function () {
45+
describe("setex", function () {
46+
it("setex", function (done) {
2347

24-
const key = "chuck-norris";
25-
const value = "alias: superman";
48+
const key = "shortLivedKey";
49+
const value = "expireIn10ms";
50+
const ttlInSeconds = 1;
2651

27-
before(function (done) {
28-
store.set(key, value)
29-
.then(function () {
30-
done();
52+
store.setex(key, value, ttlInSeconds)
53+
.then(function (test) {
54+
test.should.be.ok();
3155
});
32-
});
33-
34-
it("get", function (done) {
3556

3657
store.get(key)
3758
.then(function (v) {
3859
v.should.be.equal(value);
39-
done();
4060
});
61+
62+
Bluebird.delay(ttlInSeconds * 1000)
63+
.done(() => {
64+
return store.get(key)
65+
.then(function (v) {
66+
should(v).be.null;
67+
done();
68+
});
69+
});
70+
4171
});
4272
});
4373
});

0 commit comments

Comments
 (0)