Skip to content

Commit

Permalink
Make .end flush optional and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Bridgewater committed Sep 24, 2015
1 parent 4b100b8 commit bd4fca1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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);
});
```
Expand Down
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/commands/multi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
26 changes: 26 additions & 0 deletions test/node_redis.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bd4fca1

Please sign in to comment.