2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2007-05-25 17:09:46 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class MysqlConnectionTest < ActiveRecord::TestCase
|
2007-05-25 17:09:46 -04:00
|
|
|
def setup
|
2009-01-26 09:10:00 -05:00
|
|
|
super
|
2007-05-25 17:09:46 -04:00
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
2009-01-26 09:10:00 -05:00
|
|
|
def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
|
|
|
|
run_without_connection do |orig_connection|
|
|
|
|
ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
|
|
|
|
assert ActiveRecord::Base.connection.raw_connection.reconnect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
|
|
|
|
run_without_connection do |orig_connection|
|
|
|
|
ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
|
|
|
|
assert !ActiveRecord::Base.connection.raw_connection.reconnect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-05-25 17:09:46 -04:00
|
|
|
def test_no_automatic_reconnection_after_timeout
|
|
|
|
assert @connection.active?
|
|
|
|
@connection.update('set @@wait_timeout=1')
|
|
|
|
sleep 2
|
|
|
|
assert !@connection.active?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful_reconnection_after_timeout_with_manual_reconnect
|
|
|
|
assert @connection.active?
|
|
|
|
@connection.update('set @@wait_timeout=1')
|
|
|
|
sleep 2
|
|
|
|
@connection.reconnect!
|
|
|
|
assert @connection.active?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful_reconnection_after_timeout_with_verify
|
|
|
|
assert @connection.active?
|
|
|
|
@connection.update('set @@wait_timeout=1')
|
|
|
|
sleep 2
|
2008-09-04 08:03:19 -04:00
|
|
|
@connection.verify!
|
2007-05-25 17:09:46 -04:00
|
|
|
assert @connection.active?
|
2009-09-14 20:53:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Test that MySQL allows multiple results for stored procedures
|
|
|
|
if Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
|
|
|
|
def test_multi_results
|
|
|
|
rows = ActiveRecord::Base.connection.select_rows('CALL ten();')
|
|
|
|
assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}"
|
|
|
|
end
|
2007-05-25 17:09:46 -04:00
|
|
|
end
|
2009-01-26 09:10:00 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def run_without_connection
|
|
|
|
original_connection = ActiveRecord::Base.remove_connection
|
|
|
|
begin
|
|
|
|
yield original_connection
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.establish_connection(original_connection)
|
|
|
|
end
|
|
|
|
end
|
2007-05-25 17:09:46 -04:00
|
|
|
end
|