1
- var nodeAssert = require ( "../lib/nodeify-assertions" ) ;
1
+ var async = require ( 'async' ) ;
2
+ var assert = require ( 'assert' ) ;
2
3
var config = require ( "../lib/config" ) ;
4
+ var nodeAssert = require ( '../lib/nodeify-assertions' ) ;
3
5
var redis = config . redis ;
4
- var async = require ( "async" ) ;
5
- var assert = require ( "assert" ) ;
6
+ var RedisProcess = require ( "../lib/redis-process" ) ;
6
7
7
8
describe ( "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 ) ;
10
26
11
27
describe ( "using " + parser + " and " + ip , function ( ) {
12
28
describe ( "when not connected" , function ( ) {
@@ -15,26 +31,19 @@ describe("The 'select' method", function () {
15
31
beforeEach ( function ( done ) {
16
32
client = redis . createClient . apply ( redis . createClient , args ) ;
17
33
client . once ( "error" , done ) ;
18
-
19
34
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 ( ) ;
24
39
} ) ;
25
40
} ) ;
26
41
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 ) {
30
43
client . select ( 1 , function ( err , res ) {
31
- nodeAssert . isNotError ( ) ( err , res ) ;
44
+ assert . equal ( err . message , 'Redis connection gone from end event.' ) ;
32
45
done ( ) ;
33
46
} ) ;
34
-
35
- setTimeout ( function ( ) {
36
- done ( ) ;
37
- } , 45 ) ;
38
47
} ) ;
39
48
} ) ;
40
49
@@ -44,10 +53,7 @@ describe("The 'select' method", function () {
44
53
beforeEach ( function ( done ) {
45
54
client = redis . createClient . apply ( redis . createClient , args ) ;
46
55
client . once ( "error" , done ) ;
47
-
48
- client . once ( "connect" , function ( ) {
49
- done ( ) ;
50
- } ) ;
56
+ client . once ( "connect" , function ( ) { done ( ) ; } ) ;
51
57
} ) ;
52
58
53
59
afterEach ( function ( ) {
@@ -65,54 +71,37 @@ describe("The 'select' method", function () {
65
71
} ) ;
66
72
67
73
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.
71
74
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 ) {
73
76
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
- } ) ;
80
77
client . select ( 1 ) ;
81
-
82
78
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 ) ;
85
82
} ) ;
86
83
} ) ;
87
84
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 ( ) {
90
86
it ( "emits an error" , function ( done ) {
91
- this . timeout ( 50 ) ;
92
-
93
87
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 ( ) ;
97
91
} ) ;
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 ) ;
106
92
} ) ;
107
93
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 ( ) ;
109
96
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 ) ;
116
105
} ) ;
117
106
} ) ;
118
107
} ) ;
@@ -121,10 +110,13 @@ describe("The 'select' method", function () {
121
110
}
122
111
123
112
[ '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 ) {
127
115
allTests ( parser , ip ) ;
128
116
} )
129
117
} ) ;
118
+
119
+ after ( function ( done ) {
120
+ if ( rp ) rp . stop ( done ) ;
121
+ } ) ;
130
122
} ) ;
0 commit comments