active_record_host_pool 1.2.5 → 2.0.0.pre.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +112 -0
- data/Readme.md +3 -3
- data/lib/active_record_host_pool/clear_query_cache_patch.rb +13 -14
- data/lib/active_record_host_pool/connection_adapter_mixin.rb +49 -43
- data/lib/active_record_host_pool/connection_proxy.rb +1 -1
- data/lib/active_record_host_pool/pool_proxy.rb +182 -5
- data/lib/active_record_host_pool/version.rb +1 -1
- data/lib/active_record_host_pool.rb +21 -9
- metadata +10 -33
- data/lib/active_record_host_pool/pool_proxy_6_1.rb +0 -159
- data/lib/active_record_host_pool/pool_proxy_legacy.rb +0 -156
- data/test/database.yml +0 -108
- data/test/helper.rb +0 -198
- data/test/schema.rb +0 -23
- data/test/test_arhp.rb +0 -186
data/test/test_arhp.rb
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'helper'
|
4
|
-
|
5
|
-
class ActiveRecordHostPoolTest < Minitest::Test
|
6
|
-
include ARHPTestSetup
|
7
|
-
def setup
|
8
|
-
if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
|
9
|
-
Phenix.rise! config_path: 'test/three_tier_database.yml'
|
10
|
-
else
|
11
|
-
Phenix.rise!
|
12
|
-
end
|
13
|
-
arhp_create_models
|
14
|
-
end
|
15
|
-
|
16
|
-
def teardown
|
17
|
-
ActiveRecord::Base.connection.disconnect!
|
18
|
-
ActiveRecordHostPool::PoolProxy.class_variable_set(:@@_connection_pools, {})
|
19
|
-
Phenix.burn!
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_process_forking_with_connections
|
23
|
-
# Ensure we have a connection already
|
24
|
-
assert_equal(true, ActiveRecord::Base.connected?)
|
25
|
-
|
26
|
-
# Verify that when we fork, the process doesn't crash
|
27
|
-
pid = Process.fork do
|
28
|
-
if ActiveRecord.version >= Gem::Version.new('5.2')
|
29
|
-
assert_equal(false, ActiveRecord::Base.connected?) # New to Rails 5.2
|
30
|
-
else
|
31
|
-
assert_equal(true, ActiveRecord::Base.connected?)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
Process.wait(pid)
|
35
|
-
# Cleanup any connections we may have left around
|
36
|
-
ActiveRecord::Base.connection_handler.clear_all_connections!
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_models_with_matching_hosts_ports_sockets_usernames_and_replica_status_should_share_a_connection
|
40
|
-
assert_equal(Pool1DbA.connection.raw_connection, Pool1DbB.connection.raw_connection)
|
41
|
-
assert_equal(Pool2DbD.connection.raw_connection, Pool2DbE.connection.raw_connection)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_models_with_different_ports_should_not_share_a_connection
|
45
|
-
refute_equal(Pool1DbA.connection.raw_connection, Pool2DbD.connection.raw_connection)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_models_with_different_usernames_should_not_share_a_connection
|
49
|
-
refute_equal(Pool2DbE.connection.raw_connection, Pool3DbE.connection.raw_connection)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_should_select_on_correct_database
|
53
|
-
Pool1DbA.connection.send(:select_all, 'select 1')
|
54
|
-
assert_equal 'arhp_test_db_a', current_database(Pool1DbA)
|
55
|
-
|
56
|
-
Pool2DbD.connection.send(:select_all, 'select 1')
|
57
|
-
assert_equal 'arhp_test_db_d', current_database(Pool2DbD)
|
58
|
-
|
59
|
-
Pool3DbE.connection.send(:select_all, 'select 1')
|
60
|
-
assert_equal 'arhp_test_db_e', current_database(Pool3DbE)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_should_insert_on_correct_database
|
64
|
-
Pool1DbA.connection.send(:insert, "insert into tests values(NULL, 'foo')")
|
65
|
-
assert_equal 'arhp_test_db_a', current_database(Pool1DbA)
|
66
|
-
|
67
|
-
Pool2DbD.connection.send(:insert, "insert into tests values(NULL, 'foo')")
|
68
|
-
assert_equal 'arhp_test_db_d', current_database(Pool2DbD)
|
69
|
-
|
70
|
-
Pool3DbE.connection.send(:insert, "insert into tests values(NULL, 'foo')")
|
71
|
-
assert_equal 'arhp_test_db_e', current_database(Pool3DbE)
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_connection_returns_a_proxy
|
75
|
-
assert_kind_of ActiveRecordHostPool::ConnectionProxy, Pool1DbA.connection
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_connection_proxy_handles_private_methods
|
79
|
-
# Relies on connection.class returning the real class
|
80
|
-
Pool1DbA.connection.class.class_eval do
|
81
|
-
private
|
82
|
-
|
83
|
-
def test_private_method
|
84
|
-
true
|
85
|
-
end
|
86
|
-
end
|
87
|
-
assert Pool1DbA.connection.respond_to?(:test_private_method, true)
|
88
|
-
refute Pool1DbA.connection.respond_to?(:test_private_method)
|
89
|
-
assert_includes(Pool1DbA.connection.private_methods, :test_private_method)
|
90
|
-
assert_equal true, Pool1DbA.connection.send(:test_private_method)
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_object_creation
|
94
|
-
Pool1DbA.create(val: 'foo')
|
95
|
-
assert_equal('arhp_test_db_a', current_database(Pool1DbA))
|
96
|
-
|
97
|
-
Pool2DbD.create(val: 'bar')
|
98
|
-
assert_equal('arhp_test_db_a', current_database(Pool1DbA))
|
99
|
-
assert_equal('arhp_test_db_d', current_database(Pool2DbD))
|
100
|
-
|
101
|
-
Pool1DbB.create!(val: 'bar_distinct')
|
102
|
-
assert_equal('arhp_test_db_b', current_database(Pool1DbB))
|
103
|
-
assert Pool1DbB.find_by_val('bar_distinct')
|
104
|
-
refute Pool1DbA.find_by_val('bar_distinct')
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_disconnect
|
108
|
-
Pool1DbA.create(val: 'foo')
|
109
|
-
unproxied = Pool1DbA.connection.unproxied
|
110
|
-
Pool1DbA.connection_handler.clear_all_connections!
|
111
|
-
Pool1DbA.create(val: 'foo')
|
112
|
-
assert(unproxied != Pool1DbA.connection.unproxied)
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_checkout
|
116
|
-
connection = ActiveRecord::Base.connection_pool.checkout
|
117
|
-
assert_kind_of(ActiveRecordHostPool::ConnectionProxy, connection)
|
118
|
-
ActiveRecord::Base.connection_pool.checkin(connection)
|
119
|
-
c2 = ActiveRecord::Base.connection_pool.checkout
|
120
|
-
assert(c2 == connection)
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_no_switch_when_creating_db
|
124
|
-
conn = Pool1DbA.connection
|
125
|
-
assert_called(conn, :execute_without_switching) do
|
126
|
-
refute_called(conn, :_switch_connection) do
|
127
|
-
assert conn._host_pool_current_database
|
128
|
-
conn.create_database(:some_args)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_no_switch_when_dropping_db
|
134
|
-
conn = Pool1DbA.connection
|
135
|
-
assert_called(conn, :execute_without_switching) do
|
136
|
-
refute_called(conn, :_switch_connection) do
|
137
|
-
assert conn._host_pool_current_database
|
138
|
-
conn.drop_database(:some_args)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_underlying_assumption_about_test_db
|
144
|
-
debug_me = false
|
145
|
-
# ensure connection
|
146
|
-
Pool1DbA.first
|
147
|
-
|
148
|
-
# which is the "default" DB to connect to?
|
149
|
-
first_db = Pool1DbA.connection.unproxied.instance_variable_get(:@_cached_current_database)
|
150
|
-
puts "\nOk, we started on #{first_db}" if debug_me
|
151
|
-
|
152
|
-
switch_to_klass = case first_db
|
153
|
-
when 'arhp_test_db_b'
|
154
|
-
Pool1DbA
|
155
|
-
when 'arhp_test_db_a'
|
156
|
-
Pool1DbB
|
157
|
-
else
|
158
|
-
raise "Expected a database name, got #{first_db.inspect}"
|
159
|
-
end
|
160
|
-
expected_database = switch_to_klass.connection.instance_variable_get(:@database)
|
161
|
-
|
162
|
-
# switch to the other database
|
163
|
-
switch_to_klass.first
|
164
|
-
puts "\nAnd now we're on #{current_database(switch_to_klass)}" if debug_me
|
165
|
-
|
166
|
-
# get the current thread id so we can shoot ourselves in the head
|
167
|
-
thread_id = switch_to_klass.connection.select_value('select @@pseudo_thread_id')
|
168
|
-
|
169
|
-
# now, disable our auto-switching and trigger a mysql reconnect
|
170
|
-
switch_to_klass.connection.unproxied.stub(:_switch_connection, true) do
|
171
|
-
Pool2DbD.connection.execute("KILL #{thread_id}")
|
172
|
-
end
|
173
|
-
|
174
|
-
# and finally, did mysql reconnect correctly?
|
175
|
-
puts "\nAnd now we end up on #{current_database(switch_to_klass)}" if debug_me
|
176
|
-
assert_equal expected_database, current_database(switch_to_klass)
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_release_connection
|
180
|
-
pool = ActiveRecord::Base.connection_pool
|
181
|
-
conn = pool.connection
|
182
|
-
assert_called_with(pool, :checkin, [conn]) do
|
183
|
-
pool.release_connection
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|