Skip to content

Commit 1121091

Browse files
committed
Assert connection id changes when reconnecting
A [previous commit][0] addressed an issue in the reconnect tests where the PostgreSQL connection was not being terminated in a way that required reconnection (but the test passed anyways). This commit adds an additional assertion to the test to ensure the connection id has changed and a reconnect has occurred. When run with the PostgreSQL adapter and pg_cancel_backend instead of pg_terminate_backend, the test now correctly fails: ``` vscode ➜ /workspaces/rails/activerecord (hm-vnpxkwrpuvlmylmy) $ ARCONN=postgresql bin/test test/cases/adapter_test.rb:884 Using postgresql Run options: --seed 21322 ​# Running: F Failure: ActiveRecord::AdapterConnectionTest#test_#execute_is_retryable [test/cases/adapter_test.rb:891]: Expected 3968 to not be equal to 3968. bin/test test/cases/adapter_test.rb:884 Finished in 0.459251s, 2.1775 runs/s, 10.8873 assertions/s. 1 runs, 5 assertions, 1 failures, 0 errors, 0 skips ``` [0]: 8938715
1 parent 08b54b9 commit 1121091

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

activerecord/test/cases/adapter_test.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,13 @@ def teardown
882882
end
883883

884884
test "#execute is retryable" do
885-
kill_connection_from_server
885+
initial_connection_id = connection_id_from_server
886+
887+
kill_connection_from_server(initial_connection_id)
886888

887889
@connection.execute("SELECT 1", allow_retry: true)
890+
891+
assert_not_equal initial_connection_id, connection_id_from_server
888892
end
889893

890894
test "disconnect and recover on #configure_connection failure" do
@@ -975,14 +979,23 @@ def remote_disconnect(connection)
975979
end
976980
end
977981

978-
def kill_connection_from_server
982+
def connection_id_from_server
983+
case @connection.adapter_name
984+
when "Mysql2", "Trilogy"
985+
@connection.execute("SELECT CONNECTION_ID()").to_a[0][0]
986+
when "PostgreSQL"
987+
@connection.execute("SELECT pg_backend_pid()").to_a[0]["pg_backend_pid"]
988+
else
989+
skip("connection_id_from_server unsupported")
990+
end
991+
end
992+
993+
def kill_connection_from_server(connection_id)
979994
conn = @connection.pool.checkout
980995
case conn.adapter_name
981996
when "Mysql2", "Trilogy"
982-
connection_id = @connection.execute("SELECT CONNECTION_ID()").to_a[0][0]
983997
conn.execute("KILL #{connection_id}")
984998
when "PostgreSQL"
985-
connection_id = @connection.execute("SELECT pg_backend_pid()").to_a[0]["pg_backend_pid"]
986999
conn.execute("SELECT pg_terminate_backend(#{connection_id})")
9871000
else
9881001
skip("kill_connection_from_server unsupported")

0 commit comments

Comments
 (0)