1- var nodeAssert = require ( "../lib/nodeify-assertions" ) ;
1+ var async = require ( 'async' ) ;
2+ var assert = require ( 'assert' ) ;
23var config = require ( "../lib/config" ) ;
4+ var nodeAssert = require ( '../lib/nodeify-assertions' ) ;
35var redis = config . redis ;
4- var async = require ( "async" ) ;
5- var assert = require ( "assert" ) ;
6+ var RedisProcess = require ( "../lib/redis-process" ) ;
67
78describe ( "The 'select' method" , function ( ) {
8- function allTests ( parser , ip , isSocket ) {
9- var args = config . configureClient ( parser , ip , isSocket ) ;
9+
10+ var rp ;
11+ before ( function ( done ) {
12+ RedisProcess . start ( function ( err , _rp ) {
13+ rp = _rp ;
14+ return done ( err ) ;
15+ } ) ;
16+ } )
17+
18+ function removeMochaListener ( ) {
19+ var mochaListener = process . listeners ( 'uncaughtException' ) . pop ( ) ;
20+ process . removeListener ( 'uncaughtException' , mochaListener ) ;
21+ return mochaListener ;
22+ }
23+
24+ function allTests ( parser , ip ) {
25+ var args = config . configureClient ( parser , ip ) ;
1026
1127 describe ( "using " + parser + " and " + ip , function ( ) {
1228 describe ( "when not connected" , function ( ) {
@@ -15,26 +31,19 @@ describe("The 'select' method", function () {
1531 beforeEach ( function ( done ) {
1632 client = redis . createClient . apply ( redis . createClient , args ) ;
1733 client . once ( "error" , done ) ;
18-
1934 client . once ( "connect" , function ( ) {
20- client . set ( "doot" , "good calsum" , function ( err , res ) {
21- client . end ( ) ;
22- done ( ) ;
23- } ) ;
35+ client . quit ( ) ;
36+ } ) ;
37+ client . on ( 'end' , function ( ) {
38+ return done ( ) ;
2439 } ) ;
2540 } ) ;
2641
27- it ( "doesn't even throw an error or call the callback at all WTF" , function ( done ) {
28- this . timeout ( 50 ) ;
29-
42+ it ( "throws an error if redis is not connected" , function ( done ) {
3043 client . select ( 1 , function ( err , res ) {
31- nodeAssert . isNotError ( ) ( err , res ) ;
44+ assert . equal ( err . message , 'Redis connection gone from end event.' ) ;
3245 done ( ) ;
3346 } ) ;
34-
35- setTimeout ( function ( ) {
36- done ( ) ;
37- } , 45 ) ;
3847 } ) ;
3948 } ) ;
4049
@@ -44,10 +53,7 @@ describe("The 'select' method", function () {
4453 beforeEach ( function ( done ) {
4554 client = redis . createClient . apply ( redis . createClient , args ) ;
4655 client . once ( "error" , done ) ;
47-
48- client . once ( "connect" , function ( ) {
49- done ( ) ;
50- } ) ;
56+ client . once ( "connect" , function ( ) { done ( ) ; } ) ;
5157 } ) ;
5258
5359 afterEach ( function ( ) {
@@ -65,54 +71,37 @@ describe("The 'select' method", function () {
6571 } ) ;
6672
6773 describe ( "and no callback is specified" , function ( ) {
68- // select_error_emits_if_no_callback
69- // this is another test that was testing the wrong thing. The old test did indeed emit an error,
70- // but not because of the lacking callback, but because 9999 was an invalid db index.
7174 describe ( "with a valid db index" , function ( ) {
72- it ( "works just fine and does not actually emit an error like the old tests assert WTF " , function ( done ) {
75+ it ( "selects the appropriate database " , function ( done ) {
7376 assert . strictEqual ( client . selected_db , null , "default db should be null" ) ;
74- this . timeout ( 50 ) ;
75- client . on ( "error" , function ( err ) {
76- nodeAssert . isNotError ( ) ( err ) ;
77- assert . strictEqual ( client . selected_db , 1 , "db should be 1 after select" ) ;
78- done ( new Error ( "the old tests were crap" ) ) ;
79- } ) ;
8077 client . select ( 1 ) ;
81-
8278 setTimeout ( function ( ) {
83- done ( ) ;
84- } , 45 ) ;
79+ assert . equal ( client . selected_db , 1 , "we should have selected the new valid DB" ) ;
80+ return done ( ) ;
81+ } , 100 ) ;
8582 } ) ;
8683 } ) ;
8784
88- // Can't seem to catch the errors thrown here.
89- xdescribe ( "with an invalid db index" , function ( ) {
85+ describe ( "with an invalid db index" , function ( ) {
9086 it ( "emits an error" , function ( done ) {
91- this . timeout ( 50 ) ;
92-
9387 assert . strictEqual ( client . selected_db , null , "default db should be null" ) ;
94- client . on ( "error" , function ( err ) {
95- console . log ( 'got an error' , err ) ;
96- done ( ) ;
88+ client . select ( 9999 , function ( err ) {
89+ assert . equal ( err . message , 'ERR invalid DB index' )
90+ return done ( ) ;
9791 } ) ;
98-
99- try {
100- client . select ( 9999 ) ;
101- } catch ( err ) { }
102-
103- setTimeout ( function ( ) {
104- done ( new Error ( "It was supposed to emit an error." ) ) ;
105- } , 45 ) ;
10692 } ) ;
10793
108- it ( "throws an error bc a callback is not" , function ( done ) {
94+ it ( "throws an error when callback not provided" , function ( done ) {
95+ var mochaListener = removeMochaListener ( ) ;
10996 assert . strictEqual ( client . selected_db , null , "default db should be null" ) ;
110- try {
111- client . select ( 9999 ) ;
112- done ( new Error ( "Was supposed to throw an invalid db index error." ) ) ;
113- } catch ( err ) {
114- done ( ) ;
115- }
97+
98+ process . once ( 'uncaughtException' , function ( err ) {
99+ process . on ( 'uncaughtException' , mochaListener ) ;
100+ assert . equal ( err . message , 'ERR invalid DB index' ) ;
101+ return done ( ) ;
102+ } ) ;
103+
104+ client . select ( 9999 ) ;
116105 } ) ;
117106 } ) ;
118107 } ) ;
@@ -121,10 +110,13 @@ describe("The 'select' method", function () {
121110 }
122111
123112 [ 'javascript' , 'hiredis' ] . forEach ( function ( parser ) {
124- //allTests(parser, "/tmp/redis.sock", true);
125- //['IPv4', 'IPv6'].forEach(function (ip) {
126- [ 'IPv4' ] . forEach ( function ( ip ) {
113+ allTests ( parser , "/tmp/redis.sock" ) ;
114+ [ 'IPv4' , 'IPv6' ] . forEach ( function ( ip ) {
127115 allTests ( parser , ip ) ;
128116 } )
129117 } ) ;
118+
119+ after ( function ( done ) {
120+ if ( rp ) rp . stop ( done ) ;
121+ } ) ;
130122} ) ;
0 commit comments