Skip to content

Commit a9ff27d

Browse files
committed
more tests
1 parent 4ab2bf0 commit a9ff27d

File tree

3 files changed

+149
-6
lines changed

3 files changed

+149
-6
lines changed

lib/redis_pool.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const debug = require("debug")("simpleRedisPool");
88

99
function RedisPool (name, redisOptions, poolOptions) {
1010

11+
this.name = name;
1112
this._redisDb = redisOptions.db || 0;
12-
1313
let _pool = null;
1414

1515
const factory = {
@@ -56,9 +56,6 @@ function RedisPool (name, redisOptions, poolOptions) {
5656
// Now that the pool settings are ready create a pool instance.
5757
_pool = genericPool.createPool(factory, poolOptions);
5858

59-
// set name
60-
this.name = name;
61-
6259
// Acquire a database connection and use an optional priority.
6360
this.acquire = (priority) => {
6461
return _pool.acquire(priority);

lib/redis_store.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ function RedisStore (name, redisOptions, poolOptions) {
4141

4242
this.del = (key) => wrapFunc(pool, (conn) => conn.delAsync(key));
4343

44-
this.ttlInSeconds = (key) => wrapFunc(pool, (conn) => conn.ttlInSecondsAsync(key));
44+
this.expire = (key, ttlInSeconds) => wrapFunc(pool, (conn) => conn.expireAsync(key, ttlInSeconds));
45+
46+
this.ttlInSeconds = (key) => wrapFunc(pool, (conn) => conn.ttlAsync(key));
4547

4648
this.keys = (pattern) => {
4749
if (!pattern || pattern === "") {
@@ -51,7 +53,6 @@ function RedisStore (name, redisOptions, poolOptions) {
5153
return wrapFunc(pool, (conn) => conn.keysAsync(pattern));
5254
};
5355

54-
5556
this.deleteAll = (pattern) => {
5657
if (!pattern || pattern === "") {
5758
pattern = "*";

test/redis_store.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,149 @@ describe("redisStore", function () {
115115
});
116116
});
117117
});
118+
119+
describe("expire", function () {
120+
it("should set a key with expire in seconds", function (done) {
121+
122+
const key = "key";
123+
const value = "make it expire";
124+
const ttlInSeconds = 1;
125+
126+
store.set(key, value)
127+
.then(function (test) {
128+
test.should.be.ok();
129+
});
130+
131+
store.expire(key, ttlInSeconds)
132+
.then(function (v) {
133+
v.should.be.ok();
134+
});
135+
136+
Bluebird.delay(ttlInSeconds * 1000)
137+
.done(() => {
138+
return store.get(key)
139+
.then(function (v) {
140+
should(v).be.null;
141+
done();
142+
});
143+
});
144+
});
145+
146+
it("should return null expiring non-existing key", function (done) {
147+
store.expire("unknownKey", 10)
148+
.then(function (v) {
149+
should(v).be.null;
150+
done();
151+
});
152+
});
153+
});
154+
155+
describe("ttl", function () {
156+
it("should return ttl left for a key in seconds", function (done) {
157+
158+
const key = "key";
159+
const value = "make it expire";
160+
const ttlInSeconds = 10;
161+
162+
store.setex(key, value, ttlInSeconds)
163+
.then(function (test) {
164+
test.should.be.ok();
165+
});
166+
167+
store.ttlInSeconds(key)
168+
.then(function (v) {
169+
170+
// it should be same as the time elapsed is very vvery small
171+
v.should.be.equal(ttlInSeconds);
172+
done();
173+
});
174+
175+
});
176+
177+
it("should return null on ttl for a non-existing key", function (done) {
178+
store.ttlInSeconds("unknownKey")
179+
.then(function (v) {
180+
should(v).be.null;
181+
done();
182+
});
183+
});
184+
});
185+
186+
describe("keys", function () {
187+
188+
beforeEach(function (done) {
189+
store.deleteAll()
190+
.then(() => done());
191+
});
192+
193+
it("should return all the keys", function (done) {
194+
195+
const keyValues = {key1: "value1", key2: "value2"};
196+
197+
for (var key in keyValues) {
198+
store.set(key, keyValues[key]);
199+
}
200+
201+
store.keys()
202+
.then(function (keys) {
203+
keyValues.should.have.keys(keys[0], keys[1]);
204+
done();
205+
});
206+
});
207+
208+
it("should return all the keys matches pattern", function (done) {
209+
210+
const keyValues = {key1: "value1", key2: "value2"};
211+
212+
for (var key in keyValues) {
213+
store.set(key, keyValues[key]);
214+
}
215+
216+
store.keys("key[2]")
217+
.then(function (keys) {
218+
keys.should.containEql("key2");
219+
done();
220+
});
221+
});
222+
});
223+
224+
describe("deleteAll", function () {
225+
226+
beforeEach(function () {
227+
const keyValues = {key1: "value1", key2: "value2"};
228+
229+
for (var key in keyValues) {
230+
store.set(key, keyValues[key]);
231+
}
232+
});
233+
234+
it("should delete all the keys", function (done) {
235+
236+
store.deleteAll()
237+
.then(function (v) {
238+
v.should.be.ok();
239+
});
240+
241+
store.keys()
242+
.then(function (keys) {
243+
keys.should.be.empty();
244+
done();
245+
});
246+
});
247+
248+
it("should delete all the keys matches pattern", function (done) {
249+
250+
store.deleteAll("key[2]")
251+
.then(function (v) {
252+
v.should.be.ok();
253+
});
254+
255+
store.keys()
256+
.then(function (keys) {
257+
keys.should.be.not.empty();
258+
keys.should.not.containEql("key2");
259+
done();
260+
});
261+
});
262+
});
118263
});

0 commit comments

Comments
 (0)