Skip to content

Commit bfb8230

Browse files
committed
Merge pull request #1010 from NodeRedis/redis-2.4-support
Fix Redis 2.4 support
2 parents 08a4537 + 359820c commit bfb8230

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

lib/individualCommands.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var utils = require('./utils');
44
var debug = require('./debug');
55
var Multi = require('./multi');
66
var no_password_is_set = /no password is set/;
7+
var loading = /LOADING/;
78
var RedisClient = require('../').RedisClient;
89

910
/********************************
@@ -36,14 +37,14 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (d
3637
RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section, callback) {
3738
var self = this;
3839
var ready = this.ready;
40+
var args = [];
3941
if (typeof section === 'function') {
4042
callback = section;
41-
section = 'default';
42-
} else if (section === undefined) {
43-
section = 'default';
43+
} else if (section !== undefined) {
44+
args = Array.isArray(section) ? section : [section];
4445
}
4546
this.ready = ready || this.offline_queue.length === 0; // keep the execution order intakt
46-
var tmp = this.send_command('info', [section], function (err, res) {
47+
var tmp = this.send_command('info', args, function (err, res) {
4748
if (res) {
4849
var obj = {};
4950
var lines = res.toString().split('\r\n');
@@ -91,12 +92,20 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
9192
this.auth_pass = pass;
9293
this.ready = this.offline_queue.length === 0; // keep the execution order intakt
9394
var tmp = this.send_command('auth', [pass], function (err, res) {
94-
if (err && no_password_is_set.test(err.message)) {
95-
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
96-
err = null;
97-
res = 'OK';
95+
if (err) {
96+
if (no_password_is_set.test(err.message)) {
97+
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
98+
err = null;
99+
res = 'OK';
100+
} else if (loading.test(err.message)) {
101+
// If redis is still loading the db, it will not authenticate and everything else will fail
102+
debug('Redis still loading, trying to authenticate later');
103+
setTimeout(function () {
104+
self.auth(pass, callback);
105+
}, 200);
106+
return;
107+
}
98108
}
99-
100109
utils.callback_or_emit(self, callback, err, res);
101110
});
102111
this.ready = ready;

test/auth.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ describe("client authentication", function () {
4545
});
4646
});
4747

48+
it('support redis 2.4 with retrying auth commands if still loading', function (done) {
49+
if (helper.redisProcess().spawnFailed()) this.skip();
50+
51+
client = redis.createClient.apply(null, args);
52+
var time = Date.now();
53+
client.auth(auth, function (err, res) {
54+
assert.strictEqual('retry worked', res);
55+
assert(Date.now() - time >= 200, 'Time should be above 200 ms (the reconnect time)');
56+
assert(Date.now() - time < 300, 'Time should be below 300 ms (the reconnect should only take a bit above 200 ms)');
57+
done();
58+
});
59+
var tmp = client.command_queue.get(0).callback;
60+
client.command_queue.get(0).callback = function (err, res) {
61+
client.auth = function (pass, callback) {
62+
callback(null, 'retry worked');
63+
};
64+
tmp(new Error('ERR redis is still LOADING'));
65+
};
66+
});
67+
4868
it("emits error when auth is bad without callback", function (done) {
4969
if (helper.redisProcess().spawnFailed()) this.skip();
5070

test/commands/info.spec.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ describe("The 'info' method", function () {
1212
describe("using " + parser + " and " + ip, function () {
1313
var client;
1414

15-
before(function (done) {
15+
beforeEach(function (done) {
1616
client = redis.createClient.apply(null, args);
1717
client.once("ready", function () {
1818
client.flushall(done);
1919
});
2020
});
2121

22-
after(function () {
22+
afterEach(function () {
2323
client.end(true);
2424
});
2525

@@ -41,9 +41,10 @@ describe("The 'info' method", function () {
4141
client.set('foo', 'bar');
4242
client.info('keyspace');
4343
client.select(2, function () {
44-
assert.strictEqual(Object.keys(client.server_info).length, 3, 'Key length should be three');
45-
assert(typeof client.server_info.db2 === 'object', 'db2 keyspace should be an object');
44+
assert.strictEqual(Object.keys(client.server_info).length, 2, 'Key length should be three');
45+
assert.strictEqual(typeof client.server_info.db0, 'object', 'db0 keyspace should be an object');
4646
});
47+
client.info(['keyspace']);
4748
client.set('foo', 'bar');
4849
client.info('all', function (err, res) {
4950
assert(Object.keys(client.server_info).length > 3, 'Key length should be way above three');
@@ -53,6 +54,17 @@ describe("The 'info' method", function () {
5354
});
5455
});
5556

57+
it('check redis v.2.4 support', function (done) {
58+
var end = helper.callFuncAfter(done, 2);
59+
client.send_command = function (command, args, callback) {
60+
assert.strictEqual(args.length, 0);
61+
assert.strictEqual(command, 'info');
62+
end();
63+
};
64+
client.info();
65+
client.info(function () {});
66+
});
67+
5668
it("emit error after a failure", function (done) {
5769
client.info();
5870
client.once('error', function (err) {

0 commit comments

Comments
 (0)