1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00

Merge pull request #1010 from NodeRedis/redis-2.4-support

Fix Redis 2.4 support
This commit is contained in:
Ruben Bridgewater
2016-03-17 00:00:21 +01:00
3 changed files with 54 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ var utils = require('./utils');
var debug = require('./debug'); var debug = require('./debug');
var Multi = require('./multi'); var Multi = require('./multi');
var no_password_is_set = /no password is set/; var no_password_is_set = /no password is set/;
var loading = /LOADING/;
var RedisClient = require('../').RedisClient; var RedisClient = require('../').RedisClient;
/******************************** /********************************
@@ -36,14 +37,14 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (d
RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section, callback) { RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section, callback) {
var self = this; var self = this;
var ready = this.ready; var ready = this.ready;
var args = [];
if (typeof section === 'function') { if (typeof section === 'function') {
callback = section; callback = section;
section = 'default'; } else if (section !== undefined) {
} else if (section === undefined) { args = Array.isArray(section) ? section : [section];
section = 'default';
} }
this.ready = ready || this.offline_queue.length === 0; // keep the execution order intakt this.ready = ready || this.offline_queue.length === 0; // keep the execution order intakt
var tmp = this.send_command('info', [section], function (err, res) { var tmp = this.send_command('info', args, function (err, res) {
if (res) { if (res) {
var obj = {}; var obj = {};
var lines = res.toString().split('\r\n'); var lines = res.toString().split('\r\n');
@@ -91,12 +92,20 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
this.auth_pass = pass; this.auth_pass = pass;
this.ready = this.offline_queue.length === 0; // keep the execution order intakt this.ready = this.offline_queue.length === 0; // keep the execution order intakt
var tmp = this.send_command('auth', [pass], function (err, res) { var tmp = this.send_command('auth', [pass], function (err, res) {
if (err && no_password_is_set.test(err.message)) { if (err) {
self.warn('Warning: Redis server does not require a password, but a password was supplied.'); if (no_password_is_set.test(err.message)) {
err = null; self.warn('Warning: Redis server does not require a password, but a password was supplied.');
res = 'OK'; err = null;
res = 'OK';
} else if (loading.test(err.message)) {
// If redis is still loading the db, it will not authenticate and everything else will fail
debug('Redis still loading, trying to authenticate later');
setTimeout(function () {
self.auth(pass, callback);
}, 200);
return;
}
} }
utils.callback_or_emit(self, callback, err, res); utils.callback_or_emit(self, callback, err, res);
}); });
this.ready = ready; this.ready = ready;

View File

@@ -45,6 +45,26 @@ describe("client authentication", function () {
}); });
}); });
it('support redis 2.4 with retrying auth commands if still loading', function (done) {
if (helper.redisProcess().spawnFailed()) this.skip();
client = redis.createClient.apply(null, args);
var time = Date.now();
client.auth(auth, function (err, res) {
assert.strictEqual('retry worked', res);
assert(Date.now() - time >= 200, 'Time should be above 200 ms (the reconnect time)');
assert(Date.now() - time < 300, 'Time should be below 300 ms (the reconnect should only take a bit above 200 ms)');
done();
});
var tmp = client.command_queue.get(0).callback;
client.command_queue.get(0).callback = function (err, res) {
client.auth = function (pass, callback) {
callback(null, 'retry worked');
};
tmp(new Error('ERR redis is still LOADING'));
};
});
it("emits error when auth is bad without callback", function (done) { it("emits error when auth is bad without callback", function (done) {
if (helper.redisProcess().spawnFailed()) this.skip(); if (helper.redisProcess().spawnFailed()) this.skip();

View File

@@ -12,14 +12,14 @@ describe("The 'info' method", function () {
describe("using " + parser + " and " + ip, function () { describe("using " + parser + " and " + ip, function () {
var client; var client;
before(function (done) { beforeEach(function (done) {
client = redis.createClient.apply(null, args); client = redis.createClient.apply(null, args);
client.once("ready", function () { client.once("ready", function () {
client.flushall(done); client.flushall(done);
}); });
}); });
after(function () { afterEach(function () {
client.end(true); client.end(true);
}); });
@@ -41,9 +41,10 @@ describe("The 'info' method", function () {
client.set('foo', 'bar'); client.set('foo', 'bar');
client.info('keyspace'); client.info('keyspace');
client.select(2, function () { client.select(2, function () {
assert.strictEqual(Object.keys(client.server_info).length, 3, 'Key length should be three'); assert.strictEqual(Object.keys(client.server_info).length, 2, 'Key length should be three');
assert(typeof client.server_info.db2 === 'object', 'db2 keyspace should be an object'); assert.strictEqual(typeof client.server_info.db0, 'object', 'db0 keyspace should be an object');
}); });
client.info(['keyspace']);
client.set('foo', 'bar'); client.set('foo', 'bar');
client.info('all', function (err, res) { client.info('all', function (err, res) {
assert(Object.keys(client.server_info).length > 3, 'Key length should be way above three'); assert(Object.keys(client.server_info).length > 3, 'Key length should be way above three');
@@ -53,6 +54,17 @@ describe("The 'info' method", function () {
}); });
}); });
it('check redis v.2.4 support', function (done) {
var end = helper.callFuncAfter(done, 2);
client.send_command = function (command, args, callback) {
assert.strictEqual(args.length, 0);
assert.strictEqual(command, 'info');
end();
};
client.info();
client.info(function () {});
});
it("emit error after a failure", function (done) { it("emit error after a failure", function (done) {
client.info(); client.info();
client.once('error', function (err) { client.once('error', function (err) {