Skip to content

Commit eaca486

Browse files
Erin SpicelandBenjamin Coe
authored andcommitted
Add Mocha tests for the "set" method.
Fix error in organization of connection Mocha tests. Clarify some test descriptions in 'set' Mocha tests. Add some tests for mset. Remove old 'set' tests. Add some Mocha tests for 'get'. Add tests for 'getset'. Add tests for 'dbsize'. Add 'flushdb' tests. Add tests for 'incr'.
1 parent 1e9613f commit eaca486

File tree

10 files changed

+1089
-130
lines changed

10 files changed

+1089
-130
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"repository": {
2929
"type": "git",
3030
"url": "git://github.com/mranney/node_redis.git"
31+
},
32+
"dependencies": {
33+
"uuid": "^2.0.1"
3134
}
3235
}

test/mocha/connecting.spec.js

Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var config = require("../lib/config");
33
var nodeAssert = require("../lib/nodeify-assertions");
44
var redis = config.redis;
55
var RedisProcess = require("../lib/redis-process");
6+
var uuid = require("uuid");
67

78
describe("A node_redis client", function () {
89

@@ -20,97 +21,65 @@ describe("A node_redis client", function () {
2021
describe("using " + parser + " and " + ip, function () {
2122
var client;
2223

23-
after(function () {
24-
client.end();
25-
});
26-
27-
it("connects correctly", function (done) {
28-
client = redis.createClient.apply(redis.createClient, args);
29-
client.on("error", done);
30-
31-
client.once("ready", function () {
32-
client.removeListener("error", done);
33-
client.get("recon 1", function (err, res) {
34-
done(err);
35-
});
24+
describe("when not connected", function () {
25+
afterEach(function () {
26+
client.end();
3627
});
37-
});
38-
});
3928

40-
describe("when connected", function () {
41-
var client;
29+
it("connects correctly", function (done) {
30+
client = redis.createClient.apply(redis.createClient, args);
31+
client.on("error", done);
4232

43-
beforeEach(function (done) {
44-
client = redis.createClient.apply(redis.createClient, args);
45-
client.once("error", function onError(err) {
46-
done(err);
47-
});
48-
client.once("ready", function onReady() {
49-
done();
33+
client.once("ready", function () {
34+
client.removeListener("error", done);
35+
client.get("recon 1", function (err, res) {
36+
done(err);
37+
});
38+
});
5039
});
5140
});
5241

53-
afterEach(function () {
54-
client.end();
55-
});
42+
describe("when connected", function () {
43+
var client;
5644

57-
describe("when redis closes unexpectedly", function () {
58-
it("reconnects and can retrieve the pre-existing data", function (done) {
59-
client.on("reconnecting", function on_recon(params) {
60-
client.on("connect", function on_connect() {
61-
async.parallel([function (cb) {
62-
client.get("recon 1", function (err, res) {
63-
nodeAssert.isString("one")(err, res);
64-
cb();
65-
});
66-
}, function (cb) {
67-
client.get("recon 1", function (err, res) {
68-
nodeAssert.isString("one")(err, res);
69-
cb();
70-
});
71-
}, function (cb) {
72-
client.get("recon 2", function (err, res) {
73-
nodeAssert.isString("two")(err, res);
74-
cb();
75-
});
76-
}, function (cb) {
77-
client.get("recon 2", function (err, res) {
78-
nodeAssert.isString("two")(err, res);
79-
cb();
80-
});
81-
}], function (err, results) {
82-
client.removeListener("connect", on_connect);
83-
client.removeListener("reconnecting", on_recon);
84-
done(err);
85-
});
86-
});
45+
beforeEach(function (done) {
46+
client = redis.createClient.apply(redis.createClient, args);
47+
client.once("error", function onError(err) {
48+
done(err);
8749
});
88-
89-
client.set("recon 1", "one");
90-
client.set("recon 2", "two", function (err, res) {
91-
// Do not do this in normal programs. This is to simulate the server closing on us.
92-
// For orderly shutdown in normal programs, do client.quit()
93-
client.stream.destroy();
50+
client.once("ready", function onReady() {
51+
done();
9452
});
9553
});
9654

97-
describe("and it's subscribed to a channel", function () {
98-
// reconnect_select_db_after_pubsub
99-
// Does not pass.
100-
// "Connection in subscriber mode, only subscriber commands may be used"
101-
xit("reconnects, unsubscribes, and can retrieve the pre-existing data", function (done) {
55+
afterEach(function () {
56+
client.end();
57+
});
58+
59+
describe("when redis closes unexpectedly", function () {
60+
it("reconnects and can retrieve the pre-existing data", function (done) {
10261
client.on("reconnecting", function on_recon(params) {
103-
client.on("ready", function on_connect() {
62+
client.on("connect", function on_connect() {
10463
async.parallel([function (cb) {
105-
client.unsubscribe("recon channel", function (err, res) {
106-
nodeAssert.isNotError()(err, res);
64+
client.get("recon 1", function (err, res) {
65+
nodeAssert.isString("one")(err, res);
10766
cb();
10867
});
10968
}, function (cb) {
11069
client.get("recon 1", function (err, res) {
11170
nodeAssert.isString("one")(err, res);
11271
cb();
11372
});
73+
}, function (cb) {
74+
client.get("recon 2", function (err, res) {
75+
nodeAssert.isString("two")(err, res);
76+
cb();
77+
});
78+
}, function (cb) {
79+
client.get("recon 2", function (err, res) {
80+
nodeAssert.isString("two")(err, res);
81+
cb();
82+
});
11483
}], function (err, results) {
11584
client.removeListener("connect", on_connect);
11685
client.removeListener("reconnecting", on_recon);
@@ -120,45 +89,79 @@ describe("A node_redis client", function () {
12089
});
12190

12291
client.set("recon 1", "one");
123-
client.subscribe("recon channel", function (err, res) {
92+
client.set("recon 2", "two", function (err, res) {
12493
// Do not do this in normal programs. This is to simulate the server closing on us.
12594
// For orderly shutdown in normal programs, do client.quit()
12695
client.stream.destroy();
12796
});
12897
});
12998

130-
it("remains subscribed", function () {
131-
var client2 = redis.createClient.apply(redis.createClient, args);
132-
133-
client.on("reconnecting", function on_recon(params) {
134-
client.on("ready", function on_connect() {
135-
async.parallel([function (cb) {
136-
client.on("message", function (channel, message) {
137-
try {
138-
nodeAssert.isString("recon channel")(null, channel);
139-
nodeAssert.isString("a test message")(null, message);
140-
} catch (err) {
141-
cb(err);
142-
}
99+
describe("and it's subscribed to a channel", function () {
100+
// reconnect_select_db_after_pubsub
101+
// Does not pass.
102+
// "Connection in subscriber mode, only subscriber commands may be used"
103+
xit("reconnects, unsubscribes, and can retrieve the pre-existing data", function (done) {
104+
client.on("reconnecting", function on_recon(params) {
105+
client.on("ready", function on_connect() {
106+
async.parallel([function (cb) {
107+
client.unsubscribe("recon channel", function (err, res) {
108+
nodeAssert.isNotError()(err, res);
109+
cb();
110+
});
111+
}, function (cb) {
112+
client.get("recon 1", function (err, res) {
113+
nodeAssert.isString("one")(err, res);
114+
cb();
115+
});
116+
}], function (err, results) {
117+
client.removeListener("connect", on_connect);
118+
client.removeListener("reconnecting", on_recon);
119+
done(err);
143120
});
121+
});
122+
});
123+
124+
client.set("recon 1", "one");
125+
client.subscribe("recon channel", function (err, res) {
126+
// Do not do this in normal programs. This is to simulate the server closing on us.
127+
// For orderly shutdown in normal programs, do client.quit()
128+
client.stream.destroy();
129+
});
130+
});
144131

145-
client2.subscribe("recon channel", function (err, res) {
146-
if (err) {
147-
cb(err);
148-
return;
149-
}
150-
client2.publish("recon channel", "a test message");
132+
it("remains subscribed", function () {
133+
var client2 = redis.createClient.apply(redis.createClient, args);
134+
135+
client.on("reconnecting", function on_recon(params) {
136+
client.on("ready", function on_connect() {
137+
async.parallel([function (cb) {
138+
client.on("message", function (channel, message) {
139+
try {
140+
nodeAssert.isString("recon channel")(null, channel);
141+
nodeAssert.isString("a test message")(null, message);
142+
} catch (err) {
143+
cb(err);
144+
}
145+
});
146+
147+
client2.subscribe("recon channel", function (err, res) {
148+
if (err) {
149+
cb(err);
150+
return;
151+
}
152+
client2.publish("recon channel", "a test message");
153+
});
154+
}], function (err, results) {
155+
done(err);
151156
});
152-
}], function (err, results) {
153-
done(err);
154157
});
155158
});
156-
});
157159

158-
client.subscribe("recon channel", function (err, res) {
159-
// Do not do this in normal programs. This is to simulate the server closing on us.
160-
// For orderly shutdown in normal programs, do client.quit()
161-
client.stream.destroy();
160+
client.subscribe("recon channel", function (err, res) {
161+
// Do not do this in normal programs. This is to simulate the server closing on us.
162+
// For orderly shutdown in normal programs, do client.quit()
163+
client.stream.destroy();
164+
});
162165
});
163166
});
164167
});

test/mocha/dbsize.spec.js

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

0 commit comments

Comments
 (0)