Skip to content

Commit 4ab2bf0

Browse files
committed
added more tests
1 parent e6f37c9 commit 4ab2bf0

File tree

3 files changed

+146
-138
lines changed

3 files changed

+146
-138
lines changed

lib/redis_store.js

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,51 @@ const logger = require("./logger");
44

55
const debug = require("debug")("simpleRedisStore");
66

7+
function wrapFunc (pool, func) {
8+
return pool.acquire()
9+
.then((conn) => func(conn)
10+
.then((result) => {
11+
pool.release(conn);
12+
return result;
13+
})
14+
.catch(err => {
15+
pool.release(conn);
16+
throw err;
17+
}));
18+
}
19+
720
function RedisStore (name, redisOptions, poolOptions) {
821

22+
if (!poolOptions) {
23+
poolOptions = {};
24+
}
25+
poolOptions = Object.assign({
26+
acquireTimeoutMillis: 50
27+
}, poolOptions);
28+
929
const pool = new RedisPool(name, redisOptions, poolOptions);
1030
logger.info("Redis store created.", pool.status());
1131

1232
this.getName = bind(pool.getName, pool);
13-
this.getDB = bind(pool.getDB, pool);
14-
this.status = bind(pool.status, pool);
15-
16-
this.get = (key) => {
17-
18-
return pool.acquire()
19-
.then((conn) => {
20-
return conn.getAsync(key)
21-
.then((result) => {
22-
pool.release(conn);
23-
return result;
24-
});
25-
});
26-
};
33+
this.getRedisDB = bind(pool.getDB, pool);
34+
this.getPoolStatus = bind(pool.status, pool);
2735

28-
this.set = (key, value) => {
29-
30-
return pool.acquire()
31-
.then((conn) => {
32-
return conn.setAsync(key, value)
33-
.then((result) => {
34-
pool.release(conn);
35-
return result;
36-
});
37-
});
38-
};
39-
40-
this.setex = (key, value, ttlInSeconds) => {
41-
42-
return pool.acquire()
43-
.then((conn) => {
44-
return conn.setexAsync(key, ttlInSeconds, value)
45-
.then((result) => {
46-
pool.release(conn);
47-
return result;
48-
});
49-
});
50-
};
36+
this.get = (key) => wrapFunc(pool, (conn) => conn.getAsync(key));
5137

52-
this.del = (key) => {
38+
this.set = (key, value) => wrapFunc(pool, (conn) => conn.setAsync(key, value));
5339

54-
return pool.acquire()
55-
.then((conn) => {
56-
return conn.delAsync(key)
57-
.then((result) => {
58-
pool.release(conn);
59-
return result;
60-
});
61-
});
62-
};
40+
this.setex = (key, value, ttlInSeconds) => wrapFunc(pool, (conn) => conn.setexAsync(key, ttlInSeconds, value));
6341

64-
this.ttlInSeconds = (key) => {
42+
this.del = (key) => wrapFunc(pool, (conn) => conn.delAsync(key));
6543

66-
return pool.acquire()
67-
.then((conn) => {
68-
return conn.ttlInSecondsAsync(key)
69-
.then((result) => {
70-
pool.release(conn);
71-
return result;
72-
});
73-
});
74-
};
44+
this.ttlInSeconds = (key) => wrapFunc(pool, (conn) => conn.ttlInSecondsAsync(key));
7545

7646
this.keys = (pattern) => {
7747
if (!pattern || pattern === "") {
7848
pattern = "*";
7949
}
8050

81-
return pool.acquire()
82-
.then((conn) => {
83-
return conn.keysAsync(pattern)
84-
.then((result) => {
85-
pool.release(conn);
86-
return result;
87-
});
88-
});
51+
return wrapFunc(pool, (conn) => conn.keysAsync(pattern));
8952
};
9053

9154

test/index.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

test/redis_store.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const should = require("should");
2+
const Bluebird = require("bluebird");
3+
const RedisStore = require("../lib/redis_store");
4+
5+
describe("redisStore", function () {
6+
7+
const redisOptions = Object.assign({
8+
host: process.env.REDIS_HOST || "127.0.0.1"
9+
});
10+
const store = new RedisStore("testStore", redisOptions);
11+
12+
describe("get", function () {
13+
it("should retrieve an existing key", function (done) {
14+
15+
const key = "chuck-norris";
16+
const value = "superman";
17+
18+
store.set(key, value)
19+
.then(function (test) {
20+
test.should.be.ok();
21+
});
22+
23+
store.get(key)
24+
.then(function (v) {
25+
v.should.be.equal(value);
26+
done();
27+
});
28+
});
29+
30+
it("should return null if key doesn't exist", function (done) {
31+
32+
store.get("unknownKey")
33+
.then(function (v) {
34+
should(v).be.null;
35+
done();
36+
});
37+
});
38+
});
39+
40+
describe("set", function () {
41+
it("should store a value", function (done) {
42+
43+
const key = "key";
44+
const value = "neverExpire";
45+
46+
store.set(key, value)
47+
.then(function (test) {
48+
test.should.be.ok();
49+
});
50+
51+
store.get(key)
52+
.then(function (v) {
53+
v.should.be.equal(value);
54+
done();
55+
});
56+
});
57+
});
58+
59+
describe("setex", function () {
60+
it("should store with an expiry", function (done) {
61+
62+
const key = "shortLivedKey";
63+
const value = "expireIn10ms";
64+
const ttlInSeconds = 1;
65+
66+
store.setex(key, value, ttlInSeconds)
67+
.then(function (test) {
68+
test.should.be.ok();
69+
});
70+
71+
store.get(key)
72+
.then(function (v) {
73+
v.should.be.equal(value);
74+
});
75+
76+
Bluebird.delay(ttlInSeconds * 1000)
77+
.done(() => {
78+
return store.get(key)
79+
.then(function (v) {
80+
should(v).be.null;
81+
done();
82+
});
83+
});
84+
});
85+
});
86+
87+
describe("del", function () {
88+
it("should delete an existing key", function (done) {
89+
90+
const key = "key";
91+
const value = "neverExpire";
92+
93+
store.set(key, value)
94+
.then(function (test) {
95+
test.should.be.ok();
96+
});
97+
98+
store.del(key)
99+
.then(function (v) {
100+
v.should.be.ok();
101+
});
102+
103+
store.get(key)
104+
.then(function (v) {
105+
should(v).be.null;
106+
done();
107+
});
108+
});
109+
110+
it("should return null deleting non-existing key", function (done) {
111+
store.del("unknownKey")
112+
.then(function (v) {
113+
should(v).be.null;
114+
done();
115+
});
116+
});
117+
});
118+
});

0 commit comments

Comments
 (0)