active_record_host_pool 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -0
- data/lib/active_record_host_pool.rb +8 -0
- data/lib/active_record_host_pool/connection_adapter_mixin.rb +58 -0
- data/lib/active_record_host_pool/connection_proxy.rb +38 -0
- data/lib/active_record_host_pool/pool_proxy.rb +92 -0
- data/test/database.yml +39 -0
- data/test/helper.rb +69 -0
- data/test/schema.rb +5 -0
- data/test/test.log +19170 -0
- data/test/test_arhp.rb +108 -0
- metadata +166 -0
data/test/test_arhp.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class ActiveRecordHostPoolTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
arhp_create_databases
|
6
|
+
arhp_create_models
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_models_with_matching_hosts_should_share_a_connection
|
10
|
+
assert(Test1.connection.raw_connection == Test2.connection.raw_connection)
|
11
|
+
assert(Test3.connection.raw_connection == Test4.connection.raw_connection)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_models_without_matching_hosts_should_not_share_a_connection
|
15
|
+
assert(Test1.connection.raw_connection != Test4.connection.raw_connection)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_select_on_correct_database
|
19
|
+
action_should_use_correct_database(:select_all, "select 1")
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_insert_on_correct_database
|
23
|
+
action_should_use_correct_database(:insert, "insert into tests values(NULL, 'foo')")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_connection_returns_a_proxy
|
27
|
+
assert Test1.connection.is_a?(ActiveRecordHostPool::ConnectionProxy)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_object_creation
|
31
|
+
Test1.create(:val => 'foo')
|
32
|
+
assert_equal("arhp_test_1", current_database(Test1))
|
33
|
+
|
34
|
+
Test3.create(:val => 'bar')
|
35
|
+
assert_equal("arhp_test_1", current_database(Test1))
|
36
|
+
assert_equal("arhp_test_3", current_database(Test3))
|
37
|
+
|
38
|
+
Test2.create(:val => 'bar')
|
39
|
+
assert_equal("arhp_test_2", current_database(Test2))
|
40
|
+
assert Test2.find_by_val('bar')
|
41
|
+
assert !Test1.find_by_val('bar')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_disconnect
|
45
|
+
Test1.create(:val => 'foo')
|
46
|
+
unproxied = Test1.connection.unproxied
|
47
|
+
Test1.connection_handler.clear_all_connections!
|
48
|
+
Test1.create(:val => 'foo')
|
49
|
+
assert(unproxied != Test1.connection.unproxied)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_checkout
|
53
|
+
connection = ActiveRecord::Base.connection_pool.checkout
|
54
|
+
assert(connection.is_a?(ActiveRecordHostPool::ConnectionProxy))
|
55
|
+
ActiveRecord::Base.connection_pool.checkin(connection)
|
56
|
+
c2 = ActiveRecord::Base.connection_pool.checkout
|
57
|
+
assert(c2 == connection)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def test_underlying_assumption_about_test_db
|
62
|
+
debug_me = false
|
63
|
+
# ensure connection
|
64
|
+
Test1.first
|
65
|
+
|
66
|
+
# which is the "default" DB to connect to?
|
67
|
+
first_db = Test1.connection.unproxied.instance_variable_get("@connection_options")[3]
|
68
|
+
puts "\nOk, we started on #{first_db}" if debug_me
|
69
|
+
|
70
|
+
switch_to_klass = case first_db
|
71
|
+
when "arhp_test_2"
|
72
|
+
Test1
|
73
|
+
when "arhp_test_1"
|
74
|
+
Test2
|
75
|
+
end
|
76
|
+
expected_database = switch_to_klass.connection.instance_variable_get("@database")
|
77
|
+
|
78
|
+
# switch to the other database
|
79
|
+
switch_to_klass.first
|
80
|
+
puts "\nAnd now we're on #{current_database(switch_to_klass)}" if debug_me
|
81
|
+
|
82
|
+
# get the current thread id so we can shoot ourselves in the head
|
83
|
+
thread_id = switch_to_klass.connection.select_value("select @@pseudo_thread_id")
|
84
|
+
|
85
|
+
# now, disable our auto-switching and trigger a mysql reconnect
|
86
|
+
switch_to_klass.connection.unproxied.stubs(:_switch_connection).returns(true)
|
87
|
+
switch_to_klass.connection.execute("KILL #{thread_id}")
|
88
|
+
|
89
|
+
# and finally, did mysql reconnect correctly?
|
90
|
+
puts "\nAnd now we end up on #{current_database(switch_to_klass)}" if debug_me
|
91
|
+
assert_equal expected_database, current_database(switch_to_klass)
|
92
|
+
end
|
93
|
+
|
94
|
+
def teardown
|
95
|
+
arhp_drop_databases
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def action_should_use_correct_database(action, sql)
|
101
|
+
(1..4).each { |i|
|
102
|
+
klass = eval "Test#{i}"
|
103
|
+
desired_db = "arhp_test_#{i}"
|
104
|
+
klass.connection.send(action, sql)
|
105
|
+
assert_equal desired_db, current_database(klass)
|
106
|
+
}
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_host_pool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Osheroff
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-02 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 9
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 3
|
33
|
+
- 5
|
34
|
+
version: 2.3.5
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: bundler
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: shoulda
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: mocha
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: ruby-debug
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
107
|
+
description: ""
|
108
|
+
email:
|
109
|
+
- ben@gimbo.net
|
110
|
+
executables: []
|
111
|
+
|
112
|
+
extensions: []
|
113
|
+
|
114
|
+
extra_rdoc_files: []
|
115
|
+
|
116
|
+
files:
|
117
|
+
- lib/active_record_host_pool/connection_adapter_mixin.rb
|
118
|
+
- lib/active_record_host_pool/connection_proxy.rb
|
119
|
+
- lib/active_record_host_pool/pool_proxy.rb
|
120
|
+
- lib/active_record_host_pool.rb
|
121
|
+
- README.md
|
122
|
+
- test/database.yml
|
123
|
+
- test/helper.rb
|
124
|
+
- test/schema.rb
|
125
|
+
- test/test.log
|
126
|
+
- test/test_arhp.rb
|
127
|
+
has_rdoc: true
|
128
|
+
homepage: http://github.com/zendesk/active_record_host_pool
|
129
|
+
licenses: []
|
130
|
+
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
hash: 3
|
151
|
+
segments:
|
152
|
+
- 0
|
153
|
+
version: "0"
|
154
|
+
requirements: []
|
155
|
+
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 1.5.2
|
158
|
+
signing_key:
|
159
|
+
specification_version: 3
|
160
|
+
summary: When connecting to databases on one host, use just one connection
|
161
|
+
test_files:
|
162
|
+
- test/database.yml
|
163
|
+
- test/helper.rb
|
164
|
+
- test/schema.rb
|
165
|
+
- test/test.log
|
166
|
+
- test/test_arhp.rb
|