1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Make .end flush optional and add some tests

This commit is contained in:
Ruben Bridgewater
2015-09-24 13:27:32 +02:00
parent 4b100b8b64
commit bd4fca130d
5 changed files with 48 additions and 6 deletions

View File

@@ -212,11 +212,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
you are doing this wrong, `client` will emit an error that looks you are doing this wrong, `client` will emit an error that looks
something like this `Error: Ready check failed: ERR operation not permitted`. something like this `Error: Ready check failed: ERR operation not permitted`.
## client.end() ## client.end([flush])
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed. Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies. If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
If flush is set to true, all commands will be rejected instead of ignored after using `.end`.
This example closes the connection to the Redis server before the replies have been read. You probably don't This example closes the connection to the Redis server before the replies have been read. You probably don't
want to do this: want to do this:
@@ -227,7 +229,7 @@ var redis = require("redis"),
client.set("foo_rand000000000000", "some fantastic value"); client.set("foo_rand000000000000", "some fantastic value");
client.end(); // No further commands will be processed client.end(); // No further commands will be processed
client.get("foo_rand000000000000", function (err, reply) { client.get("foo_rand000000000000", function (err, reply) {
// This won't be called anymore // This won't be called anymore, since flush has not been set to true!
console.log(err); console.log(err);
}); });
``` ```

View File

@@ -1,7 +1,11 @@
Changelog Changelog
========= =========
## v2.x.x - xx, 2015 ## v2.1.0 - xx, 2015
Features:
- Add optional flush parameter to `.end`. If set to true, commands fired after using .end are going to be rejected instead of being ignored. (@crispy1989)
Bugfixes: Bugfixes:

View File

@@ -838,7 +838,7 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
} }
}; };
RedisClient.prototype.end = function () { RedisClient.prototype.end = function (flush) {
this.stream._events = {}; this.stream._events = {};
// Clear retry_timer // Clear retry_timer
@@ -848,8 +848,10 @@ RedisClient.prototype.end = function () {
} }
this.stream.on("error", function noop(){}); this.stream.on("error", function noop(){});
// Flush queue // Flush queue if wanted
if (flush) {
this.flush_and_error("Redis connection ended."); this.flush_and_error("Redis connection ended.");
}
this.connected = false; this.connected = false;
this.ready = false; this.ready = false;

View File

@@ -196,6 +196,14 @@ describe("The 'multi' method", function () {
}).exec(done); }).exec(done);
}); });
it('runs a multi without any further commands', function(done) {
client.multi().exec(function(err, res) {
assert.strictEqual(err, null);
assert.strictEqual(res.length, 0);
done();
});
});
it('allows multiple operations to be performed using a chaining API', function (done) { it('allows multiple operations to be performed using a chaining API', function (done) {
client.multi() client.multi()
.mset('some', '10', 'keys', '20') .mset('some', '10', 'keys', '20')

View File

@@ -174,6 +174,32 @@ describe("The node_redis client", function () {
}); });
describe(".end", function () {
it('used without flush', function(done) {
var err = null;
client.set('foo', 'bar');
client.end();
client.get('foo', function(err, res) {
err = new Error('failed');
});
setTimeout(function() {
done(err);
}, 200);
});
it('used with flush set to true', function(done) {
client.set('foo', 'bar');
client.end();
client.get('foo', function(err, res) {
assert.strictEqual(err.command, 'GET');
assert.strictEqual(err.message, "GET can't be processed. The connection has already been closed.");
done();
});
});
});
describe("commands after using .quit should fail", function () { describe("commands after using .quit should fail", function () {
it("return an error in the callback", function (done) { it("return an error in the callback", function (done) {