slave_pools 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +259 -0
- data/lib/slave_pools.rb +43 -0
- data/lib/slave_pools/active_record_extensions.rb +54 -0
- data/lib/slave_pools/connection_proxy.rb +280 -0
- data/lib/slave_pools/observer_extensions.rb +19 -0
- data/lib/slave_pools/query_cache_compat.rb +45 -0
- data/lib/slave_pools/slave_pool.rb +29 -0
- data/slave_pools.gemspec +24 -0
- data/spec/config/database.yml +43 -0
- data/spec/connection_proxy_spec.rb +330 -0
- data/spec/slave_pool_spec.rb +43 -0
- data/spec/slave_pools_spec.rb +69 -0
- data/spec/spec_helper.rb +17 -0
- metadata +132 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe SlavePoolsModule::SlavePool do
|
4
|
+
|
5
|
+
context "Multiple slaves" do
|
6
|
+
before do
|
7
|
+
@slaves = ["db1", "db2", "db3"]
|
8
|
+
@slave_pool = SlavePoolsModule::SlavePool.new("name", @slaves.clone)
|
9
|
+
end
|
10
|
+
specify {@slave_pool.pool_size.should == 3}
|
11
|
+
|
12
|
+
it "should return items in a round robin fashion" do
|
13
|
+
first = @slaves.shift
|
14
|
+
@slave_pool.current.should == first
|
15
|
+
@slaves.each do |item|
|
16
|
+
@slave_pool.next.should == item
|
17
|
+
end
|
18
|
+
@slave_pool.next.should == first
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "Single Slave" do
|
23
|
+
before do
|
24
|
+
@slaves = ["db1"]
|
25
|
+
@slave_pool = SlavePoolsModule::SlavePool.new("name", @slaves.clone)
|
26
|
+
end
|
27
|
+
specify {@slave_pool.pool_size.should == 1}
|
28
|
+
|
29
|
+
it "should return items in a round robin fashion" do
|
30
|
+
@slave_pool.current.should == "db1"
|
31
|
+
@slave_pool.next.should == "db1"
|
32
|
+
@slave_pool.next.should == "db1"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "shouldn't call next_reader! if there is only one slave" do
|
36
|
+
@slave_pool.should_not_receive(:next_index!)
|
37
|
+
@slave_pool.current.should == "db1"
|
38
|
+
@slave_pool.next.should == "db1"
|
39
|
+
@slave_pool.next.should == "db1"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe SlavePools do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
ActiveRecord::Base.configurations = SLAVE_POOLS_SPEC_CONFIG
|
7
|
+
ActiveRecord::Base.establish_connection :test
|
8
|
+
@sql = 'SELECT NOW()'
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with no setup" do
|
12
|
+
it "should not error out if next slave is called and SlavePools is not set up" do
|
13
|
+
SlavePools.should_receive(:active?).and_return(false)
|
14
|
+
SlavePools.next_slave!.should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not error out if current is called and SlavePools is not set up" do
|
18
|
+
SlavePools.should_receive(:active?).and_return(false)
|
19
|
+
SlavePools.current.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should yield on a with_pool call if slave_pools is not active" do
|
23
|
+
SlavePools.should_receive(:active?).and_return(false)
|
24
|
+
ActiveRecord::Base.connection.should_receive(:execute)
|
25
|
+
SlavePools.with_pool('admin') {ActiveRecord::Base.connection.execute(@sql)}
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should yield on a with_master call if slave_pools is not active" do
|
29
|
+
SlavePools.should_receive(:active?).and_return(false)
|
30
|
+
ActiveRecord::Base.connection.should_receive(:execute)
|
31
|
+
SlavePools.with_master {ActiveRecord::Base.connection.execute(@sql)}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "Slave Pool Wrapper calls" do
|
36
|
+
before(:each) do
|
37
|
+
SlavePools.setup!
|
38
|
+
@proxy = ActiveRecord::Base.connection_proxy
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should send next_slave! call to connection proxy' do
|
42
|
+
ActiveRecord::Base.should_receive(:respond_to?).exactly(1)
|
43
|
+
SlavePools.active?
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should send next_slave! call to connection proxy' do
|
47
|
+
@proxy.should_receive(:next_slave!).exactly(1)
|
48
|
+
SlavePools.next_slave!
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should send with_pool call to connection proxy' do
|
52
|
+
@proxy.should_receive(:with_pool).exactly(1)
|
53
|
+
SlavePools.with_pool('test')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should send with_master call to connection proxy' do
|
57
|
+
@proxy.should_receive(:with_master).exactly(1)
|
58
|
+
SlavePools.with_master
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should send current call to connection proxy' do
|
62
|
+
@proxy.should_receive(:current).exactly(1)
|
63
|
+
SlavePools.current
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
require 'slave_pools'
|
6
|
+
|
7
|
+
module Rails
|
8
|
+
def self.env
|
9
|
+
ActiveSupport::StringInquirer.new("test")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
SLAVE_POOLS_SPEC_DIR = File.dirname(__FILE__)
|
14
|
+
SLAVE_POOLS_SPEC_CONFIG = YAML::load(File.open(SLAVE_POOLS_SPEC_DIR + '/config/database.yml'))
|
15
|
+
|
16
|
+
ActiveRecord::Base.logger = Logger.new(SLAVE_POOLS_SPEC_DIR + "/debug.log")
|
17
|
+
ActiveRecord::Base.configurations = SLAVE_POOLS_SPEC_CONFIG
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slave_pools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dan Drabik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.12
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.12
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mysql2
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.3.11
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.3.11
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Connection proxy for ActiveRecord for single master / multiple slave
|
79
|
+
database groups
|
80
|
+
email: dan@kickstarter.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files:
|
84
|
+
- LICENSE
|
85
|
+
- README.rdoc
|
86
|
+
files:
|
87
|
+
- lib/slave_pools.rb
|
88
|
+
- lib/slave_pools/active_record_extensions.rb
|
89
|
+
- lib/slave_pools/connection_proxy.rb
|
90
|
+
- lib/slave_pools/observer_extensions.rb
|
91
|
+
- lib/slave_pools/query_cache_compat.rb
|
92
|
+
- lib/slave_pools/slave_pool.rb
|
93
|
+
- LICENSE
|
94
|
+
- README.rdoc
|
95
|
+
- spec/config/database.yml
|
96
|
+
- spec/connection_proxy_spec.rb
|
97
|
+
- spec/slave_pool_spec.rb
|
98
|
+
- spec/slave_pools_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- slave_pools.gemspec
|
101
|
+
homepage: https://github.com/kickstarter/slave_pools
|
102
|
+
licenses: []
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options:
|
105
|
+
- --line-numbers
|
106
|
+
- --inline-source
|
107
|
+
- --title
|
108
|
+
- slave_pools
|
109
|
+
- --main
|
110
|
+
- README.rdoc
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.2'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.8.24
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: Connection proxy for ActiveRecord for single master / multiple slave database
|
131
|
+
groups
|
132
|
+
test_files: []
|