You've already forked node-redis
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:
@@ -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
|
||||
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.
|
||||
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
|
||||
want to do this:
|
||||
|
||||
@@ -227,7 +229,7 @@ var redis = require("redis"),
|
||||
client.set("foo_rand000000000000", "some fantastic value");
|
||||
client.end(); // No further commands will be processed
|
||||
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);
|
||||
});
|
||||
```
|
||||
|
@@ -1,7 +1,11 @@
|
||||
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:
|
||||
|
||||
|
8
index.js
8
index.js
@@ -838,7 +838,7 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
|
||||
}
|
||||
};
|
||||
|
||||
RedisClient.prototype.end = function () {
|
||||
RedisClient.prototype.end = function (flush) {
|
||||
this.stream._events = {};
|
||||
|
||||
// Clear retry_timer
|
||||
@@ -848,8 +848,10 @@ RedisClient.prototype.end = function () {
|
||||
}
|
||||
this.stream.on("error", function noop(){});
|
||||
|
||||
// Flush queue
|
||||
this.flush_and_error("Redis connection ended.");
|
||||
// Flush queue if wanted
|
||||
if (flush) {
|
||||
this.flush_and_error("Redis connection ended.");
|
||||
}
|
||||
|
||||
this.connected = false;
|
||||
this.ready = false;
|
||||
|
@@ -196,6 +196,14 @@ describe("The 'multi' method", function () {
|
||||
}).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) {
|
||||
client.multi()
|
||||
.mset('some', '10', 'keys', '20')
|
||||
|
@@ -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 () {
|
||||
|
||||
it("return an error in the callback", function (done) {
|
||||
|
Reference in New Issue
Block a user