Skip to content

Commit bbd2be4

Browse files
authored
Merge pull request rails#51081 from Shopify/test-cleanups
Refactor some Active Record tests
2 parents 13c1dfe + bbf0b1f commit bbd2be4

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

activerecord/test/cases/adapter_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def teardown
736736
end
737737

738738
test "#execute is retryable" do
739-
conn_id = case @connection.class::ADAPTER_NAME
739+
conn_id = case @connection.adapter_name
740740
when "Mysql2"
741741
@connection.execute("SELECT CONNECTION_ID()").to_a[0][0]
742742
when "Trilogy"
@@ -754,7 +754,7 @@ def teardown
754754

755755
private
756756
def raw_transaction_open?(connection)
757-
case connection.class::ADAPTER_NAME
757+
case connection.adapter_name
758758
when "PostgreSQL"
759759
connection.instance_variable_get(:@raw_connection).transaction_status == ::PG::PQTRANS_INTRANS
760760
when "Mysql2", "Trilogy"
@@ -779,7 +779,7 @@ def raw_transaction_open?(connection)
779779
end
780780

781781
def remote_disconnect(connection)
782-
case connection.class::ADAPTER_NAME
782+
case connection.adapter_name
783783
when "PostgreSQL"
784784
unless connection.instance_variable_get(:@raw_connection).transaction_status == ::PG::PQTRANS_INTRANS
785785
connection.instance_variable_get(:@raw_connection).async_exec("begin")
@@ -796,7 +796,7 @@ def remote_disconnect(connection)
796796

797797
def kill_connection_from_server(connection_id)
798798
conn = @connection.pool.checkout
799-
case conn.class::ADAPTER_NAME
799+
case conn.adapter_name
800800
when "Mysql2", "Trilogy"
801801
conn.execute("KILL #{connection_id}")
802802
when "PostgreSQL"

activerecord/test/cases/adapters/sqlite3/copy_table_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ class CopyTableTest < ActiveRecord::SQLite3TestCase
77

88
def setup
99
@connection = ActiveRecord::Base.connection
10-
class << @connection
11-
public :copy_table, :table_structure, :indexes
12-
end
1310
end
1411

1512
def test_copy_table(from = "customers", to = "customers2", options = {})
@@ -90,11 +87,11 @@ def test_copy_table_with_binary_column
9087

9188
private
9289
def copy_table(from, to, options = {})
93-
@connection.copy_table(from, to, { temporary: true }.merge(options))
90+
@connection.send(:copy_table, from, to, { temporary: true }.merge(options))
9491
end
9592

9693
def column_names(table)
97-
@connection.table_structure(table).map { |column| column["name"] }
94+
@connection.send(:table_structure, table).map { |column| column["name"] }
9895
end
9996

10097
def column_values(table, column)

activerecord/test/cases/adapters/trilogy/trilogy_adapter_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
167167
@conn.cache do
168168
event_fired = false
169169
subscription = ->(name, start, finish, id, payload) {
170+
next if payload[:name] == "SCHEMA"
171+
170172
event_fired = true
171173

172174
# First, we test keys that are defined by default by the AbstractAdapter
@@ -198,6 +200,8 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
198200

199201
event_fired = false
200202
subscription = ->(name, start, finish, id, payload) {
203+
next if payload[:name] == "SCHEMA"
204+
201205
event_fired = true
202206

203207
# First, we test keys that are defined by default by the AbstractAdapter

activerecord/test/cases/fixtures_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ def test_bulk_insert_with_a_multi_statement_query_in_a_nested_transaction
148148

149149
if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
150150
def test_bulk_insert_with_multi_statements_enabled
151-
orig_connection_class = ActiveRecord::Base.connection.class
151+
adapter_name = ActiveRecord::Base.connection.adapter_name
152152
run_without_connection do |orig_connection|
153-
case orig_connection_class::ADAPTER_NAME
153+
case adapter_name
154154
when "Trilogy"
155155
ActiveRecord::Base.establish_connection(
156156
orig_connection.merge(multi_statement: true)
@@ -170,7 +170,7 @@ def test_bulk_insert_with_multi_statements_enabled
170170
assert_nothing_raised do
171171
conn = ActiveRecord::Base.connection
172172
conn.execute("SELECT 1; SELECT 2;")
173-
case orig_connection_class::ADAPTER_NAME
173+
case adapter_name
174174
when "Trilogy"
175175
conn.raw_connection.next_result while conn.raw_connection.more_results_exist?
176176
else
@@ -190,7 +190,7 @@ def test_bulk_insert_with_multi_statements_enabled
190190
assert_nothing_raised do
191191
conn = ActiveRecord::Base.connection
192192
conn.execute("SELECT 1; SELECT 2;")
193-
case orig_connection_class::ADAPTER_NAME
193+
case adapter_name
194194
when "Trilogy"
195195
conn.raw_connection.next_result while conn.raw_connection.more_results_exist?
196196
else
@@ -201,9 +201,9 @@ def test_bulk_insert_with_multi_statements_enabled
201201
end
202202

203203
def test_bulk_insert_with_multi_statements_disabled
204-
orig_connection_class = ActiveRecord::Base.connection.class
204+
adapter_name = ActiveRecord::Base.connection.adapter_name
205205
run_without_connection do |orig_connection|
206-
case orig_connection_class::ADAPTER_NAME
206+
case adapter_name
207207
when "Trilogy"
208208
ActiveRecord::Base.establish_connection(
209209
orig_connection.merge(multi_statement: false)
@@ -223,7 +223,7 @@ def test_bulk_insert_with_multi_statements_disabled
223223
assert_raises(ActiveRecord::StatementInvalid) do
224224
conn = ActiveRecord::Base.connection
225225
conn.execute("SELECT 1; SELECT 2;")
226-
case orig_connection_class::ADAPTER_NAME
226+
case adapter_name
227227
when "Trilogy"
228228
conn.raw_connection.next_result while conn.raw_connection.more_results_exist?
229229
else
@@ -239,7 +239,7 @@ def test_bulk_insert_with_multi_statements_disabled
239239
assert_raises(ActiveRecord::StatementInvalid) do
240240
conn = ActiveRecord::Base.connection
241241
conn.execute("SELECT 1; SELECT 2;")
242-
case orig_connection_class::ADAPTER_NAME
242+
case adapter_name
243243
when "Trilogy"
244244
conn.raw_connection.next_result while conn.raw_connection.more_results_exist?
245245
else

activerecord/test/cases/pooled_connections_test.rb

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,12 @@ class PooledConnectionsTest < ActiveRecord::TestCase
99
self.use_transactional_tests = false
1010

1111
def setup
12-
@per_test_teardown = []
1312
@connection = ActiveRecord::Base.remove_connection.configuration_hash
1413
end
1514

16-
teardown do
15+
def teardown
1716
ActiveRecord::Base.connection_handler.clear_all_connections!(:all)
1817
ActiveRecord::Base.establish_connection(@connection)
19-
@per_test_teardown.each(&:call)
20-
end
21-
22-
# Will deadlock due to lack of Monitor timeouts in 1.9
23-
def checkout_checkin_connections(pool_size, threads)
24-
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
25-
@connection_count = 0
26-
@timed_out = 0
27-
threads.times do
28-
Thread.new do
29-
conn = ActiveRecord::Base.connection_pool.checkout
30-
sleep 0.1
31-
ActiveRecord::Base.connection_pool.checkin conn
32-
@connection_count += 1
33-
rescue ActiveRecord::ConnectionTimeoutError
34-
@timed_out += 1
35-
end.join
36-
end
37-
end
38-
39-
def checkout_checkin_connections_loop(pool_size, loops)
40-
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
41-
@connection_count = 0
42-
@timed_out = 0
43-
loops.times do
44-
conn = ActiveRecord::Base.connection_pool.checkout
45-
ActiveRecord::Base.connection_pool.checkin conn
46-
@connection_count += 1
47-
ActiveRecord::Base.connection.data_sources
48-
rescue ActiveRecord::ConnectionTimeoutError
49-
@timed_out += 1
50-
end
5118
end
5219

5320
def test_pooled_connection_checkin_one
@@ -71,5 +38,37 @@ def test_pooled_connection_remove
7138
ActiveRecord::Base.connection_pool.remove(extra_connection)
7239
assert_equal ActiveRecord::Base.connection, old_connection
7340
end
41+
42+
private
43+
# Will deadlock due to lack of Monitor timeouts in 1.9
44+
def checkout_checkin_connections(pool_size, threads)
45+
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
46+
@connection_count = 0
47+
@timed_out = 0
48+
threads.times do
49+
Thread.new do
50+
conn = ActiveRecord::Base.connection_pool.checkout
51+
sleep 0.1
52+
ActiveRecord::Base.connection_pool.checkin conn
53+
@connection_count += 1
54+
rescue ActiveRecord::ConnectionTimeoutError
55+
@timed_out += 1
56+
end.join
57+
end
58+
end
59+
60+
def checkout_checkin_connections_loop(pool_size, loops)
61+
ActiveRecord::Base.establish_connection(@connection.merge(pool: pool_size, checkout_timeout: 0.5))
62+
@connection_count = 0
63+
@timed_out = 0
64+
loops.times do
65+
conn = ActiveRecord::Base.connection_pool.checkout
66+
ActiveRecord::Base.connection_pool.checkin conn
67+
@connection_count += 1
68+
ActiveRecord::Base.connection.data_sources
69+
rescue ActiveRecord::ConnectionTimeoutError
70+
@timed_out += 1
71+
end
72+
end
7473
end
7574
end

0 commit comments

Comments
 (0)