Skip to content

Commit 5da0833

Browse files
author
Benjamin Coe
committed
fixed up some tests, changed how redis spawns
1 parent 2b44245 commit 5da0833

File tree

7 files changed

+109
-116
lines changed

7 files changed

+109
-116
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ node_js:
44
- "0.10"
55
- "0.12"
66
- "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
7+
script: npm run mocha
118
after_success: npm run coverage

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"main": "./index.js",
1212
"scripts": {
1313
"coverage": "nyc report --reporter=text-lcov | coveralls",
14-
"test": "nyc ./test/run.sh"
14+
"test": "nyc ./test/run.sh",
15+
"mocha": "nyc mocha ./test/mocha/*.js"
1516
},
1617
"devDependencies": {
1718
"async": "^1.3.0",
@@ -21,6 +22,7 @@
2122
"metrics": ">=0.1.5",
2223
"mocha": "^2.2.5",
2324
"nyc": "^3.0.0",
25+
"tcp-port-used": "^0.1.2",
2426
"underscore": "~1.4.4"
2527
},
2628
"repository": {

run-bootstrapped-mocha.js

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

test/conf/redis.conf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
daemonize yes
21
port 6378
3-
bind ::1
2+
bind ::1 127.0.0.1
43
unixsocket /tmp/redis.sock
54
unixsocketperm 755

test/lib/redis-process.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
var cp = require('child_process');
2+
var config = require('./config');
3+
var path = require('path');
4+
var tcpPortUsed = require('tcp-port-used');
25

36
module.exports = {
4-
start: function (done, isSocket) {
5-
var confFile = isSocket ? "test/conf/redis-socket.conf" : "test/conf/redis.conf";
6-
var redis = cp.spawn("redis-server", [confFile]);
7+
start: function (done) {
8+
// spawn redis with our testing configuration.
9+
var confFile = path.resolve(__dirname, '../conf/redis.conf');
10+
var rp = cp.spawn("redis-server", [confFile], {});
711

8-
redis.once('err', done);
9-
setTimeout(function (data) {
10-
redis.removeListener('err', done);
11-
done();
12-
}, 1000);
12+
// wait for redis to become available, by
13+
// checking the port we bind on.
14+
var id = setInterval(function () {
15+
tcpPortUsed.check(config.PORT, '127.0.0.1')
16+
.then(function (inUse) {
17+
if (inUse) {
18+
clearInterval(id);
1319

14-
return {
15-
stop: function (done) {
16-
redis.once("exit", function () {
17-
done();
18-
});
19-
redis.kill("SIGINT");
20-
}
21-
};
20+
// return an object that can be used in
21+
// an after() block to shutdown redis.
22+
return done(null, {
23+
stop: function (done) {
24+
rp.once("exit", function (code) {
25+
var error = null;
26+
if (code !== 0) error = Error('failed to shutdown redis');
27+
return done(error);
28+
});
29+
rp.kill("SIGINT");
30+
}
31+
});
32+
}
33+
})
34+
.catch(function (err) {
35+
clearInterval(id);
36+
return done(err);
37+
})
38+
}, 100);
2239
}
2340
};

test/mocha/connecting.spec.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
var nodeAssert = require("../lib/nodeify-assertions");
1+
var async = require("async");
22
var config = require("../lib/config");
3+
var nodeAssert = require("../lib/nodeify-assertions");
34
var redis = config.redis;
4-
var async = require("async");
5+
var RedisProcess = require("../lib/redis-process");
56

67
describe("A node_redis client", function () {
7-
function allTests(parser, ip, isSocket) {
8-
var args = config.configureClient(parser, ip, isSocket);
8+
9+
var rp;
10+
before(function (done) {
11+
RedisProcess.start(function (err, _rp) {
12+
rp = _rp;
13+
return done(err);
14+
});
15+
})
16+
17+
function allTests(parser, ip) {
18+
var args = config.configureClient(parser, ip);
919

1020
describe("using " + parser + " and " + ip, function () {
1121
var client;
@@ -157,9 +167,13 @@ describe("A node_redis client", function () {
157167
}
158168

159169
['javascript', 'hiredis'].forEach(function (parser) {
160-
allTests(parser, "/tmp/redis.sock", true);
170+
allTests(parser, "/tmp/redis.sock");
161171
['IPv4', 'IPv6'].forEach(function (ip) {
162172
allTests(parser, ip);
163173
})
164174
});
175+
176+
after(function (done) {
177+
if (rp) rp.stop(done);
178+
})
165179
});

test/mocha/select.spec.js

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
var nodeAssert = require("../lib/nodeify-assertions");
1+
var async = require('async');
2+
var assert = require('assert');
23
var config = require("../lib/config");
4+
var nodeAssert = require('../lib/nodeify-assertions');
35
var redis = config.redis;
4-
var async = require("async");
5-
var assert = require("assert");
6+
var RedisProcess = require("../lib/redis-process");
67

78
describe("The 'select' method", function () {
8-
function allTests(parser, ip, isSocket) {
9-
var args = config.configureClient(parser, ip, isSocket);
9+
10+
var rp;
11+
before(function (done) {
12+
RedisProcess.start(function (err, _rp) {
13+
rp = _rp;
14+
return done(err);
15+
});
16+
})
17+
18+
function removeMochaListener () {
19+
var mochaListener = process.listeners('uncaughtException').pop();
20+
process.removeListener('uncaughtException', mochaListener);
21+
return mochaListener;
22+
}
23+
24+
function allTests(parser, ip) {
25+
var args = config.configureClient(parser, ip);
1026

1127
describe("using " + parser + " and " + ip, function () {
1228
describe("when not connected", function () {
@@ -15,26 +31,19 @@ describe("The 'select' method", function () {
1531
beforeEach(function (done) {
1632
client = redis.createClient.apply(redis.createClient, args);
1733
client.once("error", done);
18-
1934
client.once("connect", function () {
20-
client.set("doot", "good calsum", function (err, res) {
21-
client.end();
22-
done();
23-
});
35+
client.quit();
36+
});
37+
client.on('end', function () {
38+
return done();
2439
});
2540
});
2641

27-
it("doesn't even throw an error or call the callback at all WTF", function (done) {
28-
this.timeout(50);
29-
42+
it("throws an error if redis is not connected", function (done) {
3043
client.select(1, function (err, res) {
31-
nodeAssert.isNotError()(err, res);
44+
assert.equal(err.message, 'Redis connection gone from end event.');
3245
done();
3346
});
34-
35-
setTimeout(function () {
36-
done();
37-
}, 45);
3847
});
3948
});
4049

@@ -44,10 +53,7 @@ describe("The 'select' method", function () {
4453
beforeEach(function (done) {
4554
client = redis.createClient.apply(redis.createClient, args);
4655
client.once("error", done);
47-
48-
client.once("connect", function () {
49-
done();
50-
});
56+
client.once("connect", function () { done(); });
5157
});
5258

5359
afterEach(function () {
@@ -65,54 +71,37 @@ describe("The 'select' method", function () {
6571
});
6672

6773
describe("and no callback is specified", function () {
68-
// select_error_emits_if_no_callback
69-
// this is another test that was testing the wrong thing. The old test did indeed emit an error,
70-
// but not because of the lacking callback, but because 9999 was an invalid db index.
7174
describe("with a valid db index", function () {
72-
it("works just fine and does not actually emit an error like the old tests assert WTF", function (done) {
75+
it("selects the appropriate database", function (done) {
7376
assert.strictEqual(client.selected_db, null, "default db should be null");
74-
this.timeout(50);
75-
client.on("error", function (err) {
76-
nodeAssert.isNotError()(err);
77-
assert.strictEqual(client.selected_db, 1, "db should be 1 after select");
78-
done(new Error("the old tests were crap"));
79-
});
8077
client.select(1);
81-
8278
setTimeout(function () {
83-
done();
84-
}, 45);
79+
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
80+
return done();
81+
}, 100);
8582
});
8683
});
8784

88-
// Can't seem to catch the errors thrown here.
89-
xdescribe("with an invalid db index", function () {
85+
describe("with an invalid db index", function () {
9086
it("emits an error", function (done) {
91-
this.timeout(50);
92-
9387
assert.strictEqual(client.selected_db, null, "default db should be null");
94-
client.on("error", function (err) {
95-
console.log('got an error', err);
96-
done();
88+
client.select(9999, function (err) {
89+
assert.equal(err.message, 'ERR invalid DB index')
90+
return done();
9791
});
98-
99-
try {
100-
client.select(9999);
101-
} catch (err) {}
102-
103-
setTimeout(function () {
104-
done(new Error("It was supposed to emit an error."));
105-
}, 45);
10692
});
10793

108-
it("throws an error bc a callback is not", function (done) {
94+
it("throws an error when callback not provided", function (done) {
95+
var mochaListener = removeMochaListener();
10996
assert.strictEqual(client.selected_db, null, "default db should be null");
110-
try {
111-
client.select(9999);
112-
done(new Error("Was supposed to throw an invalid db index error."));
113-
} catch (err) {
114-
done();
115-
}
97+
98+
process.once('uncaughtException', function (err) {
99+
process.on('uncaughtException', mochaListener);
100+
assert.equal(err.message, 'ERR invalid DB index');
101+
return done();
102+
});
103+
104+
client.select(9999);
116105
});
117106
});
118107
});
@@ -121,10 +110,13 @@ describe("The 'select' method", function () {
121110
}
122111

123112
['javascript', 'hiredis'].forEach(function (parser) {
124-
//allTests(parser, "/tmp/redis.sock", true);
125-
//['IPv4', 'IPv6'].forEach(function (ip) {
126-
['IPv4'].forEach(function (ip) {
113+
allTests(parser, "/tmp/redis.sock");
114+
['IPv4', 'IPv6'].forEach(function (ip) {
127115
allTests(parser, ip);
128116
})
129117
});
118+
119+
after(function (done) {
120+
if (rp) rp.stop(done);
121+
});
130122
});

0 commit comments

Comments
 (0)