Skip to content

Commit b6a81a4

Browse files
author
Ruben Bridgewater
committed
Use a .create_stream function, so other libraries can mock the stream if wanted
Reference hdachev/fakeredis#34
1 parent 8712e32 commit b6a81a4

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ function RedisClient (options) {
8888
this.options = options;
8989
// Init parser once per instance
9090
this.init_parser();
91-
self.stream = net.createConnection(cnx_options);
92-
self.install_stream_listeners();
91+
self.create_stream();
9392
}
9493
util.inherits(RedisClient, events.EventEmitter);
9594

96-
RedisClient.prototype.install_stream_listeners = function () {
95+
// Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
96+
RedisClient.prototype.create_stream = function () {
9797
var self = this;
98+
this.stream = net.createConnection(this.connection_option);
9899

99100
if (this.options.connect_timeout) {
100101
this.stream.setTimeout(this.connect_timeout, function () {
@@ -479,10 +480,7 @@ var retry_connection = function (self) {
479480
self.retry_totaltime += self.retry_delay;
480481
self.attempts += 1;
481482
self.retry_delay = Math.round(self.retry_delay * self.retry_backoff);
482-
483-
self.stream = net.createConnection(self.connection_option);
484-
self.install_stream_listeners();
485-
483+
self.create_stream();
486484
self.retry_timer = null;
487485
};
488486

test/connection.spec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,22 @@ describe("connection tests", function () {
320320
});
321321
});
322322

323-
it("works with missing options object for new redis instances", function () {
324-
// This is needed for libraries that have their own createClient function like fakeredis
325-
client = new redis.RedisClient({ on: function () {}});
323+
it("fake the stream to mock redis", function () {
324+
// This is needed for libraries that want to mock the stream like fakeredis
325+
var temp = redis.RedisClient.prototype.create_stream;
326+
var create_stream_string = String(temp);
327+
redis.RedisClient.prototype.create_stream = function () {
328+
this.connected = true;
329+
this.ready = true;
330+
};
331+
client = new redis.RedisClient();
332+
assert.strictEqual(client.stream, undefined);
333+
assert.strictEqual(client.ready, true);
334+
assert.strictEqual(client.connected, true);
335+
client.end = function () {};
336+
assert(create_stream_string !== String(redis.RedisClient.prototype.create_stream));
337+
redis.RedisClient.prototype.create_stream = temp;
338+
assert(create_stream_string === String(redis.RedisClient.prototype.create_stream));
326339
});
327340

328341
it("throws on strange connection info", function () {

0 commit comments

Comments
 (0)