File tree Expand file tree Collapse file tree 2 files changed +21
-10
lines changed Expand file tree Collapse file tree 2 files changed +21
-10
lines changed Original file line number Diff line number Diff line change @@ -88,13 +88,14 @@ function RedisClient (options) {
88
88
this . options = options ;
89
89
// Init parser once per instance
90
90
this . init_parser ( ) ;
91
- self . stream = net . createConnection ( cnx_options ) ;
92
- self . install_stream_listeners ( ) ;
91
+ self . create_stream ( ) ;
93
92
}
94
93
util . inherits ( RedisClient , events . EventEmitter ) ;
95
94
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 ( ) {
97
97
var self = this ;
98
+ this . stream = net . createConnection ( this . connection_option ) ;
98
99
99
100
if ( this . options . connect_timeout ) {
100
101
this . stream . setTimeout ( this . connect_timeout , function ( ) {
@@ -479,10 +480,7 @@ var retry_connection = function (self) {
479
480
self . retry_totaltime += self . retry_delay ;
480
481
self . attempts += 1 ;
481
482
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 ( ) ;
486
484
self . retry_timer = null ;
487
485
} ;
488
486
Original file line number Diff line number Diff line change @@ -320,9 +320,22 @@ describe("connection tests", function () {
320
320
} ) ;
321
321
} ) ;
322
322
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 ) ) ;
326
339
} ) ;
327
340
328
341
it ( "throws on strange connection info" , function ( ) {
You can’t perform that action at this time.
0 commit comments