Skip to content

Commit e2e507e

Browse files
committed
Merge pull request #793 from NodeRedis/squash-mocha-work
porting tests from hand-rolled artisinal test-suite to mocha
2 parents 6cae0b8 + 5d83e64 commit e2e507e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4869
-2467
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@ sudo: true
33
node_js:
44
- "0.10"
55
- "0.12"
6-
- "iojs"
7-
before_install:
8-
- 'printf ''bind ::1 127.0.0.1\nunixsocket /tmp/redis.sock\ndaemonize yes\nunixsocketperm 777'' >> /tmp/redis.conf'
9-
before_script:
10-
- sudo redis-server /tmp/redis.conf
6+
- "iojs-v2"
117
after_success: npm run coverage

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
"main": "./index.js",
1212
"scripts": {
1313
"coverage": "nyc report --reporter=text-lcov | coveralls",
14-
"test": "nyc ./test/run.sh"
14+
"test": "nyc ./node_modules/.bin/_mocha ./test/*.js ./test/commands/*.js ./test/parser/*.js --timeout=8000"
1515
},
1616
"devDependencies": {
17+
"async": "^1.3.0",
1718
"colors": "~0.6.0-1",
1819
"coveralls": "^2.11.2",
1920
"hiredis": "^0.4.0",
2021
"metrics": ">=0.1.5",
22+
"mocha": "^2.2.5",
2123
"nyc": "^3.0.0",
22-
"underscore": "~1.4.4"
24+
"tcp-port-used": "^0.1.2",
25+
"underscore": "~1.4.4",
26+
"uuid": "^2.0.1"
2327
},
2428
"repository": {
2529
"type": "git",

test/auth.spec.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var assert = require("assert");
2+
var config = require("./lib/config");
3+
var helper = require('./helper')
4+
var path = require('path');
5+
var redis = config.redis;
6+
7+
describe("client authentication", function () {
8+
before(function (done) {
9+
helper.stopRedis(function () {
10+
helper.startRedis('./conf/password.conf', done);
11+
});
12+
});
13+
14+
function allTests(parser, ip) {
15+
describe("using " + parser + " and " + ip, function () {
16+
var args = config.configureClient(parser, ip);
17+
var auth = 'porkchopsandwiches';
18+
var client = null;
19+
20+
afterEach(function () {
21+
client.end();
22+
});
23+
24+
it("allows auth to be provided with 'auth' method", function (done) {
25+
client = redis.createClient.apply(redis.createClient, args);
26+
client.auth(auth, function (err, res) {
27+
assert.strictEqual(null, err);
28+
assert.strictEqual("OK", res.toString());
29+
return done(err);
30+
});
31+
});
32+
33+
it("raises error when auth is bad", function (done) {
34+
client = redis.createClient.apply(redis.createClient, args);
35+
36+
client.once('error', function (error) {
37+
assert.ok(/ERR invalid password/.test(error))
38+
return done();
39+
});
40+
41+
client.auth(auth + 'bad');
42+
});
43+
44+
if (ip === 'IPv4') {
45+
it('allows auth to be provided as config option for client', function (done) {
46+
client = redis.createClient('redis://foo:' + auth + '@' + config.HOST[ip] + ':' + config.PORT);
47+
client.on("ready", function () {
48+
return done();
49+
});
50+
});
51+
}
52+
53+
it('allows auth to be provided as part of redis url', function (done) {
54+
var args = config.configureClient(parser, ip, {
55+
auth_pass: auth
56+
});
57+
client = redis.createClient.apply(redis.createClient, args);
58+
client.on("ready", function () {
59+
return done();
60+
});
61+
});
62+
63+
it('reconnects with appropriate authentication', function (done) {
64+
var readyCount = 0;
65+
client = redis.createClient.apply(redis.createClient, args);
66+
client.auth(auth);
67+
client.on("ready", function () {
68+
readyCount++;
69+
if (readyCount === 1) {
70+
client.stream.destroy();
71+
} else {
72+
return done();
73+
}
74+
});
75+
});
76+
});
77+
}
78+
79+
['javascript', 'hiredis'].forEach(function (parser) {
80+
allTests(parser, "/tmp/redis.sock");
81+
['IPv4', 'IPv6'].forEach(function (ip) {
82+
allTests(parser, ip);
83+
})
84+
});
85+
86+
after(function (done) {
87+
helper.stopRedis(function () {
88+
helper.startRedis('./conf/redis.conf', done);
89+
});
90+
});
91+
});

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/client.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 'client' method", function () {
7+
8+
function allTests(parser, ip) {
9+
var args = config.configureClient(parser, ip);
10+
var pattern = /addr=/;
11+
12+
describe("using " + parser + " and " + ip, function () {
13+
var client;
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(function (err) {
20+
if (!helper.serverVersionAtLeast(client, [2, 4, 0])) {
21+
err = Error('script not supported in redis <= 2.4.0')
22+
}
23+
return done(err);
24+
25+
})
26+
});
27+
});
28+
29+
afterEach(function () {
30+
client.end();
31+
});
32+
33+
describe('list', function () {
34+
it('lists connected clients', function (done) {
35+
client.client("list", helper.match(pattern, done));
36+
});
37+
38+
it("lists connected clients when invoked with multi's chaining syntax", function (done) {
39+
client.multi().client("list").exec(function(err, results) {
40+
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
41+
return done()
42+
})
43+
});
44+
45+
it("lists connected clients when invoked with multi's array syntax", function (done) {
46+
client.multi().client("list").exec(function(err, results) {
47+
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
48+
return done()
49+
})
50+
});
51+
});
52+
});
53+
}
54+
55+
['javascript', 'hiredis'].forEach(function (parser) {
56+
allTests(parser, "/tmp/redis.sock");
57+
['IPv4', 'IPv6'].forEach(function (ip) {
58+
allTests(parser, ip);
59+
})
60+
});
61+
});

test/commands/dbsize.spec.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
var async = require('async');
2+
var assert = require('assert');
3+
var config = require("../lib/config");
4+
var helper = require('../helper');
5+
var redis = config.redis;
6+
var uuid = require('uuid');
7+
8+
describe("The 'dbsize' method", function () {
9+
10+
function allTests(parser, ip) {
11+
var args = config.configureClient(parser, ip);
12+
13+
describe("using " + parser + " and " + ip, function () {
14+
var key, value;
15+
16+
beforeEach(function () {
17+
key = uuid.v4();
18+
value = uuid.v4();
19+
});
20+
21+
describe("when not connected", function () {
22+
var client;
23+
24+
beforeEach(function (done) {
25+
client = redis.createClient.apply(redis.createClient, args);
26+
client.once("error", done);
27+
client.once("connect", function () {
28+
client.quit();
29+
});
30+
client.on('end', function () {
31+
return done();
32+
});
33+
});
34+
35+
it("reports an error", function (done) {
36+
client.dbsize([], function (err, res) {
37+
assert.equal(err.message, 'Redis connection gone from end event.');
38+
done();
39+
});
40+
});
41+
});
42+
43+
describe("when connected", function () {
44+
var client;
45+
46+
beforeEach(function (done) {
47+
client = redis.createClient.apply(redis.createClient, args);
48+
client.once("error", done);
49+
client.once("connect", function () {
50+
client.flushdb(function (err, res) {
51+
helper.isString("OK")(err, res);
52+
done();
53+
});
54+
});
55+
});
56+
57+
afterEach(function () {
58+
client.end();
59+
});
60+
61+
it("returns a zero db size", function (done) {
62+
client.dbsize([], function (err, res) {
63+
helper.isNotError()(err, res);
64+
helper.isType.number()(err, res);
65+
assert.strictEqual(res, 0, "Initial db size should be 0");
66+
done();
67+
});
68+
});
69+
70+
describe("when more data is added to Redis", function () {
71+
var oldSize;
72+
73+
beforeEach(function (done) {
74+
client.dbsize([], function (err, res) {
75+
helper.isType.number()(err, res);
76+
assert.strictEqual(res, 0, "Initial db size should be 0");
77+
78+
oldSize = res;
79+
80+
client.set(key, value, function (err, res) {
81+
helper.isNotError()(err, res);
82+
done();
83+
});
84+
});
85+
});
86+
87+
it("returns a larger db size", function (done) {
88+
client.dbsize([], function (err, res) {
89+
helper.isNotError()(err, res);
90+
helper.isType.positiveNumber()(err, res);
91+
assert.strictEqual(true, (oldSize < res), "Adding data should increase db size.");
92+
done();
93+
});
94+
});
95+
});
96+
});
97+
});
98+
}
99+
100+
['javascript', 'hiredis'].forEach(function (parser) {
101+
allTests(parser, "/tmp/redis.sock");
102+
['IPv4', 'IPv6'].forEach(function (ip) {
103+
allTests(parser, ip);
104+
})
105+
});
106+
});

0 commit comments

Comments
 (0)