Skip to content

Commit

Permalink
Merge pull request #793 from NodeRedis/squash-mocha-work
Browse files Browse the repository at this point in the history
porting tests from hand-rolled artisinal test-suite to mocha
  • Loading branch information
bcoe committed Aug 15, 2015
2 parents 6cae0b8 + 5d83e64 commit e2e507e
Show file tree
Hide file tree
Showing 65 changed files with 4,869 additions and 2,467 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ sudo: true
node_js:
- "0.10"
- "0.12"
- "iojs"
before_install:
- 'printf ''bind ::1 127.0.0.1\nunixsocket /tmp/redis.sock\ndaemonize yes\nunixsocketperm 777'' >> /tmp/redis.conf'
before_script:
- sudo redis-server /tmp/redis.conf
- "iojs-v2"
after_success: npm run coverage
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
"main": "./index.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc ./test/run.sh"
"test": "nyc ./node_modules/.bin/_mocha ./test/*.js ./test/commands/*.js ./test/parser/*.js --timeout=8000"
},
"devDependencies": {
"async": "^1.3.0",
"colors": "~0.6.0-1",
"coveralls": "^2.11.2",
"hiredis": "^0.4.0",
"metrics": ">=0.1.5",
"mocha": "^2.2.5",
"nyc": "^3.0.0",
"underscore": "~1.4.4"
"tcp-port-used": "^0.1.2",
"underscore": "~1.4.4",
"uuid": "^2.0.1"
},
"repository": {
"type": "git",
Expand Down
91 changes: 91 additions & 0 deletions test/auth.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var assert = require("assert");
var config = require("./lib/config");
var helper = require('./helper')
var path = require('path');
var redis = config.redis;

describe("client authentication", function () {
before(function (done) {
helper.stopRedis(function () {
helper.startRedis('./conf/password.conf', done);
});
});

function allTests(parser, ip) {
describe("using " + parser + " and " + ip, function () {
var args = config.configureClient(parser, ip);
var auth = 'porkchopsandwiches';
var client = null;

afterEach(function () {
client.end();
});

it("allows auth to be provided with 'auth' method", function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.auth(auth, function (err, res) {
assert.strictEqual(null, err);
assert.strictEqual("OK", res.toString());
return done(err);
});
});

it("raises error when auth is bad", function (done) {
client = redis.createClient.apply(redis.createClient, args);

client.once('error', function (error) {
assert.ok(/ERR invalid password/.test(error))
return done();
});

client.auth(auth + 'bad');
});

if (ip === 'IPv4') {
it('allows auth to be provided as config option for client', function (done) {
client = redis.createClient('redis://foo:' + auth + '@' + config.HOST[ip] + ':' + config.PORT);
client.on("ready", function () {
return done();
});
});
}

it('allows auth to be provided as part of redis url', function (done) {
var args = config.configureClient(parser, ip, {
auth_pass: auth
});
client = redis.createClient.apply(redis.createClient, args);
client.on("ready", function () {
return done();
});
});

it('reconnects with appropriate authentication', function (done) {
var readyCount = 0;
client = redis.createClient.apply(redis.createClient, args);
client.auth(auth);
client.on("ready", function () {
readyCount++;
if (readyCount === 1) {
client.stream.destroy();
} else {
return done();
}
});
});
});
}

['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});

after(function (done) {
helper.stopRedis(function () {
helper.startRedis('./conf/redis.conf', done);
});
});
});
64 changes: 64 additions & 0 deletions test/commands/blpop.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;

describe("The 'blpop' method", function () {

function allTests(parser, ip) {
var args = config.configureClient(parser, ip);

describe("using " + parser + " and " + ip, function () {
var client;
var bclient;

beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});

it('pops value immediately if list contains values', function (done) {
bclient = redis.createClient.apply(redis.createClient, args);
client.rpush("blocking list", "initial value", helper.isNumber(1));
bclient.blpop("blocking list", 0, function (err, value) {
assert.strictEqual(value[0], "blocking list");
assert.strictEqual(value[1], "initial value");
return done(err);
});
});

it('waits for value if list is not yet populated', function (done) {
bclient = redis.createClient.apply(redis.createClient, args);
bclient.blpop("blocking list 2", 5, function (err, value) {
assert.strictEqual(value[0], "blocking list 2");
assert.strictEqual(value[1], "initial value");
return done(err);
});
client.rpush("blocking list 2", "initial value", helper.isNumber(1));
});

it('times out after specified time', function (done) {
bclient = redis.createClient.apply(redis.createClient, args);
bclient.BLPOP("blocking list", 1, function (err, res) {
assert.strictEqual(res, null);
return done(err);
});
});

afterEach(function () {
client.end();
bclient.end();
});
});
}

['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});
61 changes: 61 additions & 0 deletions test/commands/client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;

describe("The 'client' method", function () {

function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
var pattern = /addr=/;

describe("using " + parser + " and " + ip, function () {
var client;

beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
if (!helper.serverVersionAtLeast(client, [2, 4, 0])) {
err = Error('script not supported in redis <= 2.4.0')
}
return done(err);

})
});
});

afterEach(function () {
client.end();
});

describe('list', function () {
it('lists connected clients', function (done) {
client.client("list", helper.match(pattern, done));
});

it("lists connected clients when invoked with multi's chaining syntax", function (done) {
client.multi().client("list").exec(function(err, results) {
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
return done()
})
});

it("lists connected clients when invoked with multi's array syntax", function (done) {
client.multi().client("list").exec(function(err, results) {
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
return done()
})
});
});
});
}

['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});
106 changes: 106 additions & 0 deletions test/commands/dbsize.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');

describe("The 'dbsize' method", function () {

function allTests(parser, ip) {
var args = config.configureClient(parser, ip);

describe("using " + parser + " and " + ip, function () {
var key, value;

beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
});

describe("when not connected", function () {
var client;

beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});

it("reports an error", function (done) {
client.dbsize([], function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});

describe("when connected", function () {
var client;

beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err, res) {
helper.isString("OK")(err, res);
done();
});
});
});

afterEach(function () {
client.end();
});

it("returns a zero db size", function (done) {
client.dbsize([], function (err, res) {
helper.isNotError()(err, res);
helper.isType.number()(err, res);
assert.strictEqual(res, 0, "Initial db size should be 0");
done();
});
});

describe("when more data is added to Redis", function () {
var oldSize;

beforeEach(function (done) {
client.dbsize([], function (err, res) {
helper.isType.number()(err, res);
assert.strictEqual(res, 0, "Initial db size should be 0");

oldSize = res;

client.set(key, value, function (err, res) {
helper.isNotError()(err, res);
done();
});
});
});

it("returns a larger db size", function (done) {
client.dbsize([], function (err, res) {
helper.isNotError()(err, res);
helper.isType.positiveNumber()(err, res);
assert.strictEqual(true, (oldSize < res), "Adding data should increase db size.");
done();
});
});
});
});
});
}

['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});
Loading

0 comments on commit e2e507e

Please sign in to comment.