Skip to content

Commit bd4fca1

Browse files
author
Ruben Bridgewater
committed
Make .end flush optional and add some tests
1 parent 4b100b8 commit bd4fca1

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
212212
you are doing this wrong, `client` will emit an error that looks
213213
something like this `Error: Ready check failed: ERR operation not permitted`.
214214

215-
## client.end()
215+
## client.end([flush])
216216

217217
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
218218
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
219219

220+
If flush is set to true, all commands will be rejected instead of ignored after using `.end`.
221+
220222
This example closes the connection to the Redis server before the replies have been read. You probably don't
221223
want to do this:
222224

@@ -227,7 +229,7 @@ var redis = require("redis"),
227229
client.set("foo_rand000000000000", "some fantastic value");
228230
client.end(); // No further commands will be processed
229231
client.get("foo_rand000000000000", function (err, reply) {
230-
// This won't be called anymore
232+
// This won't be called anymore, since flush has not been set to true!
231233
console.log(err);
232234
});
233235
```

changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
Changelog
22
=========
33

4-
## v2.x.x - xx, 2015
4+
## v2.1.0 - xx, 2015
5+
6+
Features:
7+
8+
- 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)
59

610
Bugfixes:
711

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
838838
}
839839
};
840840

841-
RedisClient.prototype.end = function () {
841+
RedisClient.prototype.end = function (flush) {
842842
this.stream._events = {};
843843

844844
// Clear retry_timer
@@ -848,8 +848,10 @@ RedisClient.prototype.end = function () {
848848
}
849849
this.stream.on("error", function noop(){});
850850

851-
// Flush queue
852-
this.flush_and_error("Redis connection ended.");
851+
// Flush queue if wanted
852+
if (flush) {
853+
this.flush_and_error("Redis connection ended.");
854+
}
853855

854856
this.connected = false;
855857
this.ready = false;

test/commands/multi.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ describe("The 'multi' method", function () {
196196
}).exec(done);
197197
});
198198

199+
it('runs a multi without any further commands', function(done) {
200+
client.multi().exec(function(err, res) {
201+
assert.strictEqual(err, null);
202+
assert.strictEqual(res.length, 0);
203+
done();
204+
});
205+
});
206+
199207
it('allows multiple operations to be performed using a chaining API', function (done) {
200208
client.multi()
201209
.mset('some', '10', 'keys', '20')

test/node_redis.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,32 @@ describe("The node_redis client", function () {
174174

175175
});
176176

177+
describe(".end", function () {
178+
179+
it('used without flush', function(done) {
180+
var err = null;
181+
client.set('foo', 'bar');
182+
client.end();
183+
client.get('foo', function(err, res) {
184+
err = new Error('failed');
185+
});
186+
setTimeout(function() {
187+
done(err);
188+
}, 200);
189+
});
190+
191+
it('used with flush set to true', function(done) {
192+
client.set('foo', 'bar');
193+
client.end();
194+
client.get('foo', function(err, res) {
195+
assert.strictEqual(err.command, 'GET');
196+
assert.strictEqual(err.message, "GET can't be processed. The connection has already been closed.");
197+
done();
198+
});
199+
});
200+
201+
});
202+
177203
describe("commands after using .quit should fail", function () {
178204

179205
it("return an error in the callback", function (done) {

0 commit comments

Comments
 (0)