@@ -95,15 +95,15 @@ def test_bad_connection_to_postgres_database
95
95
end
96
96
end
97
97
98
- def test_reconnect_after_bad_connection_on_check_version
98
+ def test_reconnect_after_bad_connection_on_check_version_with_0_return
99
99
db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
100
100
connection = ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . new ( db_config . configuration_hash . merge ( connection_retries : 0 ) )
101
101
connection . connect!
102
102
103
103
# mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
104
104
connection . pool . instance_variable_set ( :@server_version , nil )
105
105
connection . raw_connection . stub ( :server_version , 0 ) do
106
- error = assert_raises ActiveRecord ::ConnectionFailed do
106
+ error = assert_raises ActiveRecord ::ConnectionNotEstablished do
107
107
connection . reconnect!
108
108
end
109
109
assert_equal "Could not determine PostgreSQL version" , error . message
@@ -115,6 +115,27 @@ def test_reconnect_after_bad_connection_on_check_version
115
115
end
116
116
end
117
117
118
+ def test_reconnect_after_bad_connection_on_check_version_with_native_exception
119
+ db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
120
+ connection = ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . new ( db_config . configuration_hash . merge ( connection_retries : 0 ) )
121
+ connection . connect!
122
+
123
+ # mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
124
+ connection . pool . instance_variable_set ( :@server_version , nil )
125
+ # https://github.com/ged/ruby-pg/commit/a565e153d4d05955342ad24d4845378eee956935
126
+ connection . raw_connection . stub ( :server_version , -> { raise PG ::ConnectionBad , "PQserverVersion() can't get server version" } ) do
127
+ error = assert_raises ActiveRecord ::ConnectionNotEstablished do
128
+ connection . reconnect!
129
+ end
130
+ assert_equal "PQserverVersion() can't get server version" , error . message
131
+ end
132
+
133
+ # can reconnect after a bad connection
134
+ assert_nothing_raised do
135
+ connection . reconnect!
136
+ end
137
+ end
138
+
118
139
def test_database_exists_returns_false_when_the_database_does_not_exist
119
140
config = { database : "non_extant_database" , adapter : "postgresql" }
120
141
assert_not ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . database_exists? ( config ) ,
0 commit comments