You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Fix fired but not yet returned commands not being rejected after a connection loss
This commit is contained in:
@@ -236,6 +236,63 @@ describe("connection tests", function () {
|
||||
});
|
||||
}
|
||||
|
||||
it("redis still loading <= 1000ms", function (done) {
|
||||
client = redis.createClient.apply(redis.createClient, args);
|
||||
var tmp = client.info.bind(client);
|
||||
var end = helper.callFuncAfter(done, 3);
|
||||
var delayed = false;
|
||||
var time;
|
||||
// Mock original function and pretent redis is still loading
|
||||
client.info = function (cb) {
|
||||
tmp(function(err, res) {
|
||||
if (!delayed) {
|
||||
assert(!err);
|
||||
res = res.toString().replace(/loading:0/, 'loading:1\r\nloading_eta_seconds:0.5');
|
||||
delayed = true;
|
||||
time = Date.now();
|
||||
}
|
||||
end();
|
||||
cb(err, res);
|
||||
});
|
||||
};
|
||||
client.on("ready", function () {
|
||||
var rest = Date.now() - time;
|
||||
// Be on the safe side and accept 100ms above the original value
|
||||
assert(rest - 100 < 500 && rest >= 500);
|
||||
assert(delayed);
|
||||
end();
|
||||
});
|
||||
});
|
||||
|
||||
it("redis still loading > 1000ms", function (done) {
|
||||
client = redis.createClient.apply(redis.createClient, args);
|
||||
var tmp = client.info.bind(client);
|
||||
var end = helper.callFuncAfter(done, 3);
|
||||
var delayed = false;
|
||||
var time;
|
||||
// Mock original function and pretent redis is still loading
|
||||
client.info = function (cb) {
|
||||
tmp(function(err, res) {
|
||||
if (!delayed) {
|
||||
assert(!err);
|
||||
// Try reconnecting after one second even if redis tells us the time needed is above one second
|
||||
res = res.toString().replace(/loading:0/, 'loading:1\r\nloading_eta_seconds:2.5');
|
||||
delayed = true;
|
||||
time = Date.now();
|
||||
}
|
||||
end();
|
||||
cb(err, res);
|
||||
});
|
||||
};
|
||||
client.on("ready", function () {
|
||||
var rest = Date.now() - time;
|
||||
// Be on the safe side and accept 100ms above the original value
|
||||
assert(rest - 100 < 1000 && rest >= 1000);
|
||||
assert(delayed);
|
||||
end();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -556,6 +556,43 @@ describe("The node_redis client", function () {
|
||||
assert.strictEqual(client.offline_queue.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
it("flushes the command queue if connection is lost", function (done) {
|
||||
client = redis.createClient({
|
||||
parser: parser
|
||||
});
|
||||
|
||||
client.once('ready', function() {
|
||||
var multi = client.multi();
|
||||
multi.config("bar");
|
||||
var cb = function(err, reply) {
|
||||
assert.equal(err.code, 'UNCERTAIN_STATE');
|
||||
};
|
||||
for (var i = 0; i < 12; i += 3) {
|
||||
client.set("foo" + i, "bar" + i);
|
||||
multi.set("foo" + (i + 1), "bar" + (i + 1), cb);
|
||||
multi.set("foo" + (i + 2), "bar" + (i + 2));
|
||||
}
|
||||
multi.exec();
|
||||
assert.equal(client.command_queue.length, 15);
|
||||
helper.killConnection(client);
|
||||
});
|
||||
|
||||
client.on("reconnecting", function (params) {
|
||||
assert.equal(client.command_queue.length, 15);
|
||||
});
|
||||
|
||||
client.on('error', function(err) {
|
||||
if (/uncertain state/.test(err.message)) {
|
||||
assert.equal(client.command_queue.length, 0);
|
||||
done();
|
||||
} else {
|
||||
assert.equal(err.code, 'ECONNREFUSED');
|
||||
assert.equal(err.errno, 'ECONNREFUSED');
|
||||
assert.equal(err.syscall, 'connect');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('false', function () {
|
||||
@@ -599,7 +636,7 @@ describe("The node_redis client", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it("flushes the command queue connection if in broken connection mode", function (done) {
|
||||
it("flushes the command queue if connection is lost", function (done) {
|
||||
client = redis.createClient({
|
||||
parser: parser,
|
||||
max_attempts: 2,
|
||||
@@ -610,7 +647,7 @@ describe("The node_redis client", function () {
|
||||
var multi = client.multi();
|
||||
multi.config("bar");
|
||||
var cb = function(err, reply) {
|
||||
assert.equal(err.code, 'CONNECTION_BROKEN');
|
||||
assert.equal(err.code, 'UNCERTAIN_STATE');
|
||||
};
|
||||
for (var i = 0; i < 12; i += 3) {
|
||||
client.set("foo" + i, "bar" + i);
|
||||
@@ -627,7 +664,7 @@ describe("The node_redis client", function () {
|
||||
});
|
||||
|
||||
client.on('error', function(err) {
|
||||
if (/Redis connection in broken state:/.test(err.message)) {
|
||||
if (err.code === 'UNCERTAIN_STATE') {
|
||||
assert.equal(client.command_queue.length, 0);
|
||||
done();
|
||||
} else {
|
||||
|
@@ -317,6 +317,39 @@ describe("publish/subscribe", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it("should not publish a message multiple times per command", function (done) {
|
||||
var published = {};
|
||||
|
||||
function subscribe(message) {
|
||||
sub.removeAllListeners('subscribe');
|
||||
sub.removeAllListeners('message');
|
||||
sub.removeAllListeners('unsubscribe');
|
||||
sub.on('subscribe', function () {
|
||||
pub.publish('/foo', message);
|
||||
});
|
||||
sub.on('message', function (channel, message) {
|
||||
if (published[message]) {
|
||||
done(new Error('Message published more than once.'));
|
||||
}
|
||||
published[message] = true;
|
||||
});
|
||||
sub.on('unsubscribe', function (channel, count) {
|
||||
assert.strictEqual(count, 0);
|
||||
});
|
||||
sub.subscribe('/foo');
|
||||
}
|
||||
|
||||
subscribe('hello');
|
||||
|
||||
setTimeout(function () {
|
||||
sub.unsubscribe();
|
||||
setTimeout(function () {
|
||||
subscribe('world');
|
||||
setTimeout(done, 50);
|
||||
}, 40);
|
||||
}, 40);
|
||||
});
|
||||
|
||||
// TODO: Fix pub sub
|
||||
// And there's more than just those two issues
|
||||
describe.skip('FIXME: broken pub sub', function () {
|
||||
@@ -331,35 +364,6 @@ describe("publish/subscribe", function () {
|
||||
});
|
||||
setTimeout(done, 200);
|
||||
});
|
||||
|
||||
it("should not publish a message multiple times per command", function (done) {
|
||||
var published = {};
|
||||
|
||||
function subscribe(message) {
|
||||
sub.on('subscribe', function () {
|
||||
pub.publish('/foo', message);
|
||||
});
|
||||
sub.on('message', function (channel, message) {
|
||||
if (published[message]) {
|
||||
done(new Error('Message published more than once.'));
|
||||
}
|
||||
published[message] = true;
|
||||
});
|
||||
sub.on('unsubscribe', function (channel, count) {
|
||||
assert.strictEqual(count, 0);
|
||||
});
|
||||
sub.subscribe('/foo');
|
||||
}
|
||||
|
||||
subscribe('hello');
|
||||
|
||||
setTimeout(function () {
|
||||
sub.unsubscribe();
|
||||
setTimeout(function () {
|
||||
subscribe('world');
|
||||
}, 40);
|
||||
}, 40);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
|
Reference in New Issue
Block a user