@@ -166,57 +166,36 @@ def run_gc
166
166
expect {
167
167
Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] ) . close
168
168
} . to_not change {
169
- @client . query ( "SHOW STATUS LIKE 'Aborted_clients'" ) . first [ 'Value' ] . to_i
169
+ @client . query ( "SHOW STATUS LIKE 'Aborted_%'" ) . to_a +
170
+ @client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . to_a
170
171
}
171
172
end
172
173
173
174
it "should not leave dangling connections after garbage collection" do
174
175
run_gc
176
+ expect {
177
+ expect {
178
+ 10 . times do
179
+ Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] ) . query ( 'SELECT 1' )
180
+ end
181
+ } . to change {
182
+ @client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
183
+ } . by ( 10 )
175
184
176
- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
177
- before_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
178
-
179
- 10 . times do
180
- Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] ) . query ( 'SELECT 1' )
181
- end
182
- after_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
183
- expect ( after_count ) . to eq ( before_count + 10 )
184
-
185
- run_gc
186
- final_count = client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . first [ 'Value' ] . to_i
187
- expect ( final_count ) . to eq ( before_count )
188
- end
189
-
190
- it "should not close connections when running in a child process" do
191
- pending ( "fork is not available on this platform" ) unless Process . respond_to? ( :fork )
192
-
193
- run_gc
194
- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
195
-
196
- # this empty `fork` call fixes this tests on RBX; without it, the next
197
- # `fork` call hangs forever. WTF?
198
- fork { }
199
-
200
- fork do
201
- client . query ( 'SELECT 1' )
202
- client = nil
203
185
run_gc
204
- end
205
-
206
- Process . wait
207
-
208
- # this will throw an error if the underlying socket was shutdown by the
209
- # child's GC
210
- expect { client . query ( 'SELECT 1' ) } . to_not raise_exception
186
+ } . to_not change {
187
+ @client . query ( "SHOW STATUS LIKE 'Aborted_%'" ) . to_a +
188
+ @client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . to_a
189
+ }
211
190
end
212
191
213
192
context "#automatic_close" do
214
- if RUBY_PLATFORM =~ /mingw|mswin/
215
- it "is enabled by default" do
216
- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
217
- expect ( client . automatic_close? ) . to be ( true )
218
- end
193
+ it "is enabled by default" do
194
+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
195
+ expect ( client . automatic_close? ) . to be ( true )
196
+ end
219
197
198
+ if RUBY_PLATFORM =~ /mingw|mswin/
220
199
it "cannot be disabled" do
221
200
expect {
222
201
Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :automatic_close => false ) )
@@ -228,41 +207,47 @@ def run_gc
228
207
expect { client . automatic_close = true } . to_not raise_error
229
208
end
230
209
else
231
- it "is disabled by default" do
232
- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
233
- expect ( client . automatic_close? ) . to be ( false )
234
- end
235
-
236
210
it "can be configured" do
237
- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :automatic_close => true ) )
238
- expect ( client . automatic_close? ) . to be ( true )
211
+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :automatic_close => false ) )
212
+ expect ( client . automatic_close? ) . to be ( false )
239
213
end
240
214
241
215
it "can be assigned" do
242
216
client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
243
- client . automatic_close = true
244
- expect ( client . automatic_close? ) . to be ( true )
245
-
246
217
client . automatic_close = false
247
218
expect ( client . automatic_close? ) . to be ( false )
248
219
249
- client . automatic_close = 9
220
+ client . automatic_close = true
250
221
expect ( client . automatic_close? ) . to be ( true )
251
222
252
223
client . automatic_close = nil
253
224
expect ( client . automatic_close? ) . to be ( false )
225
+
226
+ client . automatic_close = 9
227
+ expect ( client . automatic_close? ) . to be ( true )
254
228
end
255
- end
256
229
257
- it "should terminate connections during garbage collection" do
258
- run_gc
259
- expect {
260
- Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :automatic_close => true ) ) . query ( 'SELECT 1' )
230
+ it "should not close connections when running in a child process" do
261
231
run_gc
262
- } . to_not change {
263
- @client . query ( "SHOW STATUS LIKE 'Aborted_%'" ) . to_a +
264
- @client . query ( "SHOW STATUS LIKE 'Threads_connected'" ) . to_a
265
- }
232
+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
233
+ client . automatic_close = false
234
+
235
+ # this empty `fork` call fixes this tests on RBX; without it, the next
236
+ # `fork` call hangs forever. WTF?
237
+ fork { }
238
+
239
+ fork do
240
+ client . query ( 'SELECT 1' )
241
+ client = nil
242
+ run_gc
243
+ end
244
+
245
+ Process . wait
246
+
247
+ # this will throw an error if the underlying socket was shutdown by the
248
+ # child's GC
249
+ expect { client . query ( 'SELECT 1' ) } . to_not raise_exception
250
+ end
266
251
end
267
252
end
268
253
0 commit comments