@@ -462,59 +462,40 @@ def connect *args
462462 } . should raise_error ( Mysql2 ::Error )
463463 end
464464
465- it "should close the connection when an exception is raised" do
466- begin
467- Timeout . timeout ( 1 , Timeout ::Error ) do
468- @client . query ( "SELECT sleep(2)" )
469- end
470- rescue Timeout ::Error
471- end
472465
473- lambda {
474- @client . query ( "SELECT 1" )
475- } . should raise_error ( Mysql2 ::Error , 'closed MySQL connection' )
466+ it 'should be impervious to connection-corrupting timeouts ' do
467+ pending ( '`Thread.handle_interrupt` is not defined' ) unless Thread . respond_to? ( :handle_interrupt )
468+ # attempt to break the connection
469+ expect { Timeout . timeout ( 0.1 ) { @client . query ( 'SELECT SLEEP(1)' ) } } . to raise_error ( Timeout ::Error )
470+
471+ # expect the connection to not be broken
472+ expect { @client . query ( 'SELECT 1' ) } . to_not raise_error
476473 end
477474
478- it "should handle Timeouts without leaving the connection hanging if reconnect is true" do
479- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :reconnect => true ) )
480- begin
481- Timeout . timeout ( 1 , Timeout ::Error ) do
482- client . query ( "SELECT sleep(2)" )
483- end
484- rescue Timeout ::Error
475+ context 'when a non-standard exception class is raised' do
476+ it "should close the connection when an exception is raised" do
477+ expect { Timeout . timeout ( 0.1 , ArgumentError ) { @client . query ( 'SELECT SLEEP(1)' ) } } . to raise_error ( ArgumentError )
478+ expect { @client . query ( 'SELECT 1' ) } . to raise_error ( Mysql2 ::Error , 'closed MySQL connection' )
485479 end
486480
487- lambda {
488- client . query ( "SELECT 1" )
489- } . should_not raise_error ( Mysql2 ::Error )
490- end
481+ it "should handle Timeouts without leaving the connection hanging if reconnect is true" do
482+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] . merge ( :reconnect => true ) )
491483
492- it "should handle Timeouts without leaving the connection hanging if reconnect is set to true after construction true" do
493- client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
494- begin
495- Timeout . timeout ( 1 , Timeout ::Error ) do
496- client . query ( "SELECT sleep(2)" )
497- end
498- rescue Timeout ::Error
484+ expect { Timeout . timeout ( 0.1 , ArgumentError ) { client . query ( 'SELECT SLEEP(1)' ) } } . to raise_error ( ArgumentError )
485+ expect { client . query ( 'SELECT 1' ) } . to_not raise_error
499486 end
500487
501- lambda {
502- client . query ( "SELECT 1" )
503- } . should raise_error ( Mysql2 ::Error )
488+ it "should handle Timeouts without leaving the connection hanging if reconnect is set to true after construction true" do
489+ client = Mysql2 ::Client . new ( DatabaseCredentials [ 'root' ] )
504490
505- client . reconnect = true
491+ expect { Timeout . timeout ( 0.1 , ArgumentError ) { client . query ( 'SELECT SLEEP(1)' ) } } . to raise_error ( ArgumentError )
492+ expect { client . query ( 'SELECT 1' ) } . to raise_error ( Mysql2 ::Error )
506493
507- begin
508- Timeout . timeout ( 1 , Timeout ::Error ) do
509- client . query ( "SELECT sleep(2)" )
510- end
511- rescue Timeout ::Error
512- end
513-
514- lambda {
515- client . query ( "SELECT 1" )
516- } . should_not raise_error ( Mysql2 ::Error )
494+ client . reconnect = true
517495
496+ expect { Timeout . timeout ( 0.1 , ArgumentError ) { client . query ( 'SELECT SLEEP(1)' ) } } . to raise_error ( ArgumentError )
497+ expect { client . query ( 'SELECT 1' ) } . to_not raise_error
498+ end
518499 end
519500
520501 it "threaded queries should be supported" do
0 commit comments