@@ -19,7 +19,7 @@ FakeClient.prototype.connect = function(cb) {
19
19
}
20
20
21
21
FakeClient . prototype . end = function ( ) {
22
-
22
+ this . endCalled = true ;
23
23
}
24
24
25
25
//Hangs the event loop until 'end' is called on client
@@ -59,7 +59,7 @@ test('pool creates pool on miss', function() {
59
59
assert . equal ( Object . keys ( pool . all ) . length , 2 ) ;
60
60
} ) ;
61
61
62
- test ( 'pool follows default limits ' , function ( ) {
62
+ test ( 'pool follows defaults ' , function ( ) {
63
63
var p = pool ( poolId ++ ) ;
64
64
for ( var i = 0 ; i < 100 ; i ++ ) {
65
65
p . acquire ( function ( err , client ) {
@@ -101,3 +101,78 @@ test('pool#connect with 3 parameters', function() {
101
101
p . destroyAllNow ( ) ;
102
102
} ) ;
103
103
} ) ;
104
+
105
+ test ( 'on client error, client is removed from pool' , function ( ) {
106
+ var p = pool ( poolId ++ ) ;
107
+ p . connect ( assert . success ( function ( client ) {
108
+ assert . ok ( client ) ;
109
+ client . emit ( 'drain' ) ;
110
+ assert . equal ( p . availableObjectsCount ( ) , 1 ) ;
111
+ assert . equal ( p . getPoolSize ( ) , 1 ) ;
112
+ //error event fires on pool BEFORE pool.destroy is called with client
113
+ assert . emits ( p , 'error' , function ( err ) {
114
+ assert . equal ( err . message , 'test error' ) ;
115
+ assert . ok ( ! client . endCalled ) ;
116
+ assert . equal ( p . availableObjectsCount ( ) , 1 ) ;
117
+ assert . equal ( p . getPoolSize ( ) , 1 ) ;
118
+ //after we're done in our callback, pool.destroy is called
119
+ process . nextTick ( function ( ) {
120
+ assert . ok ( client . endCalled ) ;
121
+ assert . equal ( p . availableObjectsCount ( ) , 0 ) ;
122
+ assert . equal ( p . getPoolSize ( ) , 0 ) ;
123
+ p . destroyAllNow ( ) ;
124
+ } ) ;
125
+ } ) ;
126
+ client . emit ( 'error' , new Error ( 'test error' ) ) ;
127
+ } ) ) ;
128
+ } ) ;
129
+
130
+ test ( 'pool with connection error on connection' , function ( ) {
131
+ pool . Client = function ( ) {
132
+ return {
133
+ connect : function ( cb ) {
134
+ process . nextTick ( function ( ) {
135
+ cb ( new Error ( 'Could not connect' ) ) ;
136
+ } ) ;
137
+ }
138
+ } ;
139
+ }
140
+ test ( 'two parameters' , function ( ) {
141
+ var p = pool ( poolId ++ ) ;
142
+ p . connect ( assert . calls ( function ( err , client ) {
143
+ assert . ok ( err ) ;
144
+ assert . equal ( client , null ) ;
145
+ //client automatically removed
146
+ assert . equal ( p . availableObjectsCount ( ) , 0 ) ;
147
+ assert . equal ( p . getPoolSize ( ) , 0 ) ;
148
+ } ) ) ;
149
+ } ) ;
150
+ test ( 'three parameters' , function ( ) {
151
+ var p = pool ( poolId ++ ) ;
152
+ var tid = setTimeout ( function ( ) {
153
+ assert . fail ( 'Did not call connect callback' ) ;
154
+ } , 100 ) ;
155
+ p . connect ( function ( err , client , done ) {
156
+ clearTimeout ( tid ) ;
157
+ assert . ok ( err ) ;
158
+ assert . equal ( client , null ) ;
159
+ //done does nothing
160
+ done ( new Error ( 'OH NOOOO' ) ) ;
161
+ done ( ) ;
162
+ assert . equal ( p . availableObjectsCount ( ) , 0 ) ;
163
+ assert . equal ( p . getPoolSize ( ) , 0 ) ;
164
+ } ) ;
165
+ } ) ;
166
+ } ) ;
167
+
168
+ test ( 'returnning an error to done()' , function ( ) {
169
+ var p = pool ( poolId ++ ) ;
170
+ pool . Client = FakeClient ;
171
+ p . connect ( function ( err , client , done ) {
172
+ assert . equal ( err , null ) ;
173
+ assert ( client ) ;
174
+ done ( new Error ( "BROKEN" ) ) ;
175
+ assert . equal ( p . availableObjectsCount ( ) , 0 ) ;
176
+ assert . equal ( p . getPoolSize ( ) , 0 ) ;
177
+ } ) ;
178
+ } ) ;
0 commit comments