1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

Use a .create_stream function, so other libraries can mock the stream if wanted

Reference https://github.com/hdachev/fakeredis/pull/34
This commit is contained in:
Ruben Bridgewater
2015-11-22 22:50:28 +01:00
parent 8712e32e62
commit b6a81a4297
2 changed files with 21 additions and 10 deletions

View File

@@ -88,13 +88,14 @@ function RedisClient (options) {
this.options = options; this.options = options;
// Init parser once per instance // Init parser once per instance
this.init_parser(); this.init_parser();
self.stream = net.createConnection(cnx_options); self.create_stream();
self.install_stream_listeners();
} }
util.inherits(RedisClient, events.EventEmitter); util.inherits(RedisClient, events.EventEmitter);
RedisClient.prototype.install_stream_listeners = function () { // Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
RedisClient.prototype.create_stream = function () {
var self = this; var self = this;
this.stream = net.createConnection(this.connection_option);
if (this.options.connect_timeout) { if (this.options.connect_timeout) {
this.stream.setTimeout(this.connect_timeout, function () { this.stream.setTimeout(this.connect_timeout, function () {
@@ -479,10 +480,7 @@ var retry_connection = function (self) {
self.retry_totaltime += self.retry_delay; self.retry_totaltime += self.retry_delay;
self.attempts += 1; self.attempts += 1;
self.retry_delay = Math.round(self.retry_delay * self.retry_backoff); self.retry_delay = Math.round(self.retry_delay * self.retry_backoff);
self.create_stream();
self.stream = net.createConnection(self.connection_option);
self.install_stream_listeners();
self.retry_timer = null; self.retry_timer = null;
}; };

View File

@@ -320,9 +320,22 @@ describe("connection tests", function () {
}); });
}); });
it("works with missing options object for new redis instances", function () { it("fake the stream to mock redis", function () {
// This is needed for libraries that have their own createClient function like fakeredis // This is needed for libraries that want to mock the stream like fakeredis
client = new redis.RedisClient({ on: function () {}}); var temp = redis.RedisClient.prototype.create_stream;
var create_stream_string = String(temp);
redis.RedisClient.prototype.create_stream = function () {
this.connected = true;
this.ready = true;
};
client = new redis.RedisClient();
assert.strictEqual(client.stream, undefined);
assert.strictEqual(client.ready, true);
assert.strictEqual(client.connected, true);
client.end = function () {};
assert(create_stream_string !== String(redis.RedisClient.prototype.create_stream));
redis.RedisClient.prototype.create_stream = temp;
assert(create_stream_string === String(redis.RedisClient.prototype.create_stream));
}); });
it("throws on strange connection info", function () { it("throws on strange connection info", function () {