Skip to content

Commit 51b1ba2

Browse files
author
Benjamin Coe
committed
finished porting blpop, expire, mset, slowlog, sunionstore, ttl
1 parent 65db5db commit 51b1ba2

File tree

7 files changed

+260
-152
lines changed

7 files changed

+260
-152
lines changed

test/commands/blpop.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var assert = require("assert");
2+
var config = require("../lib/config");
3+
var helper = require("../helper");
4+
var redis = config.redis;
5+
6+
describe("The 'blpop' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
11+
describe("using " + parser + " and " + ip, function () {
12+
var client;
13+
var bclient;
14+
15+
beforeEach(function (done) {
16+
client = redis.createClient.apply(redis.createClient, args);
17+
client.once("error", done);
18+
client.once("connect", function () {
19+
client.flushdb(done);
20+
});
21+
});
22+
23+
it('pops value immediately if list contains values', function (done) {
24+
bclient = redis.createClient.apply(redis.createClient, args);
25+
client.rpush("blocking list", "initial value", helper.isNumber(1));
26+
bclient.blpop("blocking list", 0, function (err, value) {
27+
assert.strictEqual(value[0], "blocking list");
28+
assert.strictEqual(value[1], "initial value");
29+
return done(err);
30+
});
31+
});
32+
33+
it('waits for value if list is not yet populated', function (done) {
34+
bclient = redis.createClient.apply(redis.createClient, args);
35+
bclient.blpop("blocking list 2", 5, function (err, value) {
36+
assert.strictEqual(value[0], "blocking list 2");
37+
assert.strictEqual(value[1], "initial value");
38+
return done(err);
39+
});
40+
client.rpush("blocking list 2", "initial value", helper.isNumber(1));
41+
});
42+
43+
it('times out after specified time', function (done) {
44+
bclient = redis.createClient.apply(redis.createClient, args);
45+
bclient.BLPOP("blocking list", 1, function (err, res) {
46+
assert.strictEqual(res, null);
47+
return done(err);
48+
});
49+
});
50+
51+
afterEach(function () {
52+
client.end();
53+
bclient.end();
54+
});
55+
});
56+
}
57+
58+
['javascript', 'hiredis'].forEach(function (parser) {
59+
allTests(parser, "/tmp/redis.sock");
60+
['IPv4', 'IPv6'].forEach(function (ip) {
61+
allTests(parser, ip);
62+
})
63+
});
64+
});

test/commands/expire.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var assert = require("assert");
2+
var config = require("../lib/config");
3+
var helper = require("../helper");
4+
var redis = config.redis;
5+
6+
describe("The 'expire' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
11+
describe("using " + parser + " and " + ip, function () {
12+
var client;
13+
14+
beforeEach(function (done) {
15+
client = redis.createClient.apply(redis.createClient, args);
16+
client.once("error", done);
17+
client.once("connect", function () {
18+
client.flushdb(done);
19+
});
20+
});
21+
22+
it('expires key after timeout', function (done) {
23+
client.set(['expiry key', 'bar'], helper.isString("OK"));
24+
client.EXPIRE(["expiry key", "1"], helper.isNumber(1));
25+
setTimeout(function () {
26+
client.exists(["expiry key"], helper.isNumber(0, done));
27+
}, 1500);
28+
});
29+
30+
afterEach(function () {
31+
client.end();
32+
});
33+
});
34+
}
35+
36+
['javascript', 'hiredis'].forEach(function (parser) {
37+
allTests(parser, "/tmp/redis.sock");
38+
['IPv4', 'IPv6'].forEach(function (ip) {
39+
allTests(parser, ip);
40+
})
41+
});
42+
});

test/commands/mset.spec.js

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,9 @@ describe("The 'mset' method", function () {
6666
describe("and a callback is specified", function () {
6767
describe("with valid parameters", function () {
6868
it("sets the value correctly", function (done) {
69-
client.mset(key, value, key2, value2, function (err, res) {
70-
helper.isNotError()(err, res);
71-
async.parallel([function (next) {
72-
client.get(key, function (err, res) {
73-
helper.isString(value)(err, res);
74-
next();
75-
});
76-
}, function (next) {
77-
client.get(key2, function (err, res) {
78-
helper.isString(value2)(err, res);
79-
next();
80-
});
81-
}], function (err) {
82-
done(err);
83-
});
84-
});
69+
client.mset(key, value, key2, value2);
70+
client.get(key, helper.isString(value));
71+
client.get(key2, helper.isString(value2, done));
8572
});
8673
});
8774

@@ -108,22 +95,8 @@ describe("The 'mset' method", function () {
10895
describe("with valid parameters", function () {
10996
it("sets the value correctly", function (done) {
11097
client.mset(key, value, key2, value2);
111-
112-
setTimeout(function () {
113-
async.parallel([function (next) {
114-
client.get(key, function (err, res) {
115-
helper.isString(value)(err, res);
116-
next();
117-
});
118-
}, function (next) {
119-
client.get(key2, function (err, res) {
120-
helper.isString(value2)(err, res);
121-
next();
122-
});
123-
}], function (err) {
124-
done(err);
125-
});
126-
}, 100);
98+
client.get(key, helper.isString(value));
99+
client.get(key2, helper.isString(value2, done));
127100
});
128101
});
129102

test/commands/slowlog.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var assert = require("assert");
2+
var config = require("../lib/config");
3+
var helper = require("../helper");
4+
var redis = config.redis;
5+
6+
describe("The 'slowlog' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
11+
describe("using " + parser + " and " + ip, function () {
12+
var client;
13+
14+
beforeEach(function (done) {
15+
client = redis.createClient.apply(redis.createClient, args);
16+
client.once("error", done);
17+
client.once("connect", function () {
18+
client.flushdb(done);
19+
});
20+
});
21+
22+
it('logs operations in slowlog', function (done) {
23+
client.config("set", "slowlog-log-slower-than", 0, helper.isString("OK"));
24+
client.slowlog("reset", helper.isString("OK"));
25+
client.set("foo", "bar", helper.isString("OK"));
26+
client.get("foo", helper.isString("bar"));
27+
client.slowlog("get", function (err, res) {
28+
assert.equal(res.length, 3);
29+
assert.equal(res[0][3].length, 2);
30+
assert.deepEqual(res[1][3], ["set", "foo", "bar"]);
31+
assert.deepEqual(res[2][3], ["slowlog", "reset"]);
32+
return done(err);
33+
});
34+
});
35+
36+
afterEach(function () {
37+
client.end();
38+
});
39+
});
40+
}
41+
42+
['javascript', 'hiredis'].forEach(function (parser) {
43+
allTests(parser, "/tmp/redis.sock");
44+
['IPv4', 'IPv6'].forEach(function (ip) {
45+
allTests(parser, ip);
46+
})
47+
});
48+
});

test/commands/sunionstore.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var assert = require("assert");
2+
var config = require("../lib/config");
3+
var helper = require("../helper");
4+
var redis = config.redis;
5+
6+
describe("The 'sunionstore' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
11+
describe("using " + parser + " and " + ip, function () {
12+
var client;
13+
14+
beforeEach(function (done) {
15+
client = redis.createClient.apply(redis.createClient, args);
16+
client.once("error", done);
17+
client.once("connect", function () {
18+
client.flushdb(done);
19+
});
20+
});
21+
22+
it('stores the result of a union', function (done) {
23+
client.sadd('sa', 'a', helper.isNumber(1));
24+
client.sadd('sa', 'b', helper.isNumber(1));
25+
client.sadd('sa', 'c', helper.isNumber(1));
26+
27+
client.sadd('sb', 'b', helper.isNumber(1));
28+
client.sadd('sb', 'c', helper.isNumber(1));
29+
client.sadd('sb', 'd', helper.isNumber(1));
30+
31+
client.sadd('sc', 'c', helper.isNumber(1));
32+
client.sadd('sc', 'd', helper.isNumber(1));
33+
client.sadd('sc', 'e', helper.isNumber(1));
34+
35+
client.sunionstore('foo', 'sa', 'sb', 'sc', helper.isNumber(5));
36+
37+
client.smembers('foo', function (err, members) {
38+
assert.equal(members.length, 5);
39+
assert.deepEqual(members.sort(), ['a', 'b', 'c', 'd', 'e']);
40+
return done(err);
41+
});
42+
});
43+
44+
afterEach(function () {
45+
client.end();
46+
});
47+
});
48+
}
49+
50+
['javascript', 'hiredis'].forEach(function (parser) {
51+
allTests(parser, "/tmp/redis.sock");
52+
['IPv4', 'IPv6'].forEach(function (ip) {
53+
allTests(parser, ip);
54+
})
55+
});
56+
});

test/commands/ttl.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var assert = require("assert");
2+
var config = require("../lib/config");
3+
var helper = require("../helper");
4+
var redis = config.redis;
5+
6+
describe("The 'ttl' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
11+
describe("using " + parser + " and " + ip, function () {
12+
var client;
13+
14+
beforeEach(function (done) {
15+
client = redis.createClient.apply(redis.createClient, args);
16+
client.once("error", done);
17+
client.once("connect", function () {
18+
client.flushdb(done);
19+
});
20+
});
21+
22+
it('returns the current ttl on a key', function (done) {
23+
client.set(["ttl key", "ttl val"], helper.isString("OK"));
24+
client.expire(["ttl key", "100"], helper.isNumber(1));
25+
setTimeout(function () {
26+
client.TTL(["ttl key"], function (err, ttl) {
27+
assert.ok(ttl > 50 && ttl <= 100);
28+
return done(err);
29+
});
30+
}, 500);
31+
});
32+
33+
afterEach(function () {
34+
client.end();
35+
});
36+
});
37+
}
38+
39+
['javascript', 'hiredis'].forEach(function (parser) {
40+
allTests(parser, "/tmp/redis.sock");
41+
['IPv4', 'IPv6'].forEach(function (ip) {
42+
allTests(parser, ip);
43+
})
44+
});
45+
});

0 commit comments

Comments
 (0)