You've already forked node-redis
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:
12
index.js
12
index.js
@@ -88,13 +88,14 @@ function RedisClient (options) {
|
||||
this.options = options;
|
||||
// Init parser once per instance
|
||||
this.init_parser();
|
||||
self.stream = net.createConnection(cnx_options);
|
||||
self.install_stream_listeners();
|
||||
self.create_stream();
|
||||
}
|
||||
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;
|
||||
this.stream = net.createConnection(this.connection_option);
|
||||
|
||||
if (this.options.connect_timeout) {
|
||||
this.stream.setTimeout(this.connect_timeout, function () {
|
||||
@@ -479,10 +480,7 @@ var retry_connection = function (self) {
|
||||
self.retry_totaltime += self.retry_delay;
|
||||
self.attempts += 1;
|
||||
self.retry_delay = Math.round(self.retry_delay * self.retry_backoff);
|
||||
|
||||
self.stream = net.createConnection(self.connection_option);
|
||||
self.install_stream_listeners();
|
||||
|
||||
self.create_stream();
|
||||
self.retry_timer = null;
|
||||
};
|
||||
|
||||
|
@@ -320,9 +320,22 @@ describe("connection tests", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it("works with missing options object for new redis instances", function () {
|
||||
// This is needed for libraries that have their own createClient function like fakeredis
|
||||
client = new redis.RedisClient({ on: function () {}});
|
||||
it("fake the stream to mock redis", function () {
|
||||
// This is needed for libraries that want to mock the stream like fakeredis
|
||||
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 () {
|
||||
|
Reference in New Issue
Block a user