Skip to content

Commit fb623da

Browse files
committed
Improve the forced reconnect code in the init_command test.
Instead of using a timeout to trigger a connection close, explicitly kill the connection. Instead of testing the return value of ping for reconnect, test that the connection ID changed.
1 parent bbea9e6 commit fb623da

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

spec/mysql2/client_spec.rb

+17-6
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,26 @@ def connect *args
9191
result = client.query("SELECT @something;")
9292
result.first['@something'].should eq('setting_value')
9393

94-
# simulate a broken connection
94+
# get the current connection id
95+
result = client.query("SELECT CONNECTION_ID()")
96+
first_conn_id = result.first['CONNECTION_ID()']
97+
98+
# break the current connection
9599
begin
96-
Timeout.timeout(1, Timeout::Error) do
97-
client.query("SELECT sleep(2)")
98-
end
99-
rescue Timeout::Error
100+
client.query("KILL #{first_conn_id}")
101+
rescue Mysql2::Error
100102
end
101-
client.ping.should be_false
102103

104+
client.ping # reconnect now
105+
106+
# get the new connection id
107+
result = client.query("SELECT CONNECTION_ID()")
108+
second_conn_id = result.first['CONNECTION_ID()']
109+
110+
# confirm reconnect by checking the new connection id
111+
first_conn_id.should_not == second_conn_id
112+
113+
# At last, check that the init command executed
103114
result = client.query("SELECT @something;")
104115
result.first['@something'].should eq('setting_value')
105116
end

0 commit comments

Comments
 (0)