switchman 1.2.10 → 1.2.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard.rb +2 -2
- data/lib/switchman/active_record/connection_handler.rb +18 -9
- data/lib/switchman/active_record/connection_pool.rb +3 -0
- data/lib/switchman/active_record/query_cache.rb +6 -1
- data/lib/switchman/active_record/query_methods.rb +1 -1
- data/lib/switchman/connection_pool_proxy.rb +1 -1
- data/lib/switchman/r_spec_helper.rb +73 -61
- data/lib/switchman/test_helper.rb +8 -2
- data/lib/switchman/version.rb +1 -1
- data/lib/tasks/switchman.rake +5 -0
- data/spec/dummy/config/database.yml +2 -4
- data/spec/dummy/config/environments/development.rb +0 -4
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/db/schema.rb +12 -12
- data/spec/dummy/db/shard_120.sqlite3 +0 -0
- data/spec/dummy/db/shard_156.sqlite3 +0 -0
- data/spec/dummy/db/shard_193.sqlite3 +0 -0
- data/spec/dummy/db/shard_23.sqlite3 +0 -0
- data/spec/dummy/db/shard_234.sqlite3 +0 -0
- data/spec/dummy/db/shard_257.sqlite3 +0 -0
- data/spec/dummy/db/shard_277.sqlite3 +0 -0
- data/spec/dummy/db/shard_311.sqlite3 +0 -0
- data/spec/dummy/db/shard_324.sqlite3 +0 -0
- data/spec/dummy/db/shard_342.sqlite3 +0 -0
- data/spec/dummy/db/shard_375.sqlite3 +0 -0
- data/spec/dummy/db/shard_389.sqlite3 +0 -0
- data/spec/dummy/db/shard_428.sqlite3 +0 -0
- data/spec/dummy/db/shard_452.sqlite3 +0 -0
- data/spec/dummy/db/shard_462.sqlite3 +0 -0
- data/spec/dummy/db/shard_483.sqlite3 +0 -0
- data/spec/dummy/db/shard_508.sqlite3 +0 -0
- data/spec/dummy/db/shard_531.sqlite3 +0 -0
- data/spec/dummy/db/shard_554.sqlite3 +0 -0
- data/spec/dummy/db/shard_577.sqlite3 +0 -0
- data/spec/dummy/db/shard_58.sqlite3 +0 -0
- data/spec/dummy/db/shard_600.sqlite3 +0 -0
- data/spec/dummy/db/shard_623.sqlite3 +0 -0
- data/spec/dummy/db/shard_646.sqlite3 +0 -0
- data/spec/dummy/db/shard_668.sqlite3 +0 -0
- data/spec/dummy/db/shard_680.sqlite3 +0 -0
- data/spec/dummy/db/shard_708.sqlite3 +0 -0
- data/spec/dummy/db/shard_735.sqlite3 +0 -0
- data/spec/dummy/db/shard_753.sqlite3 +0 -0
- data/spec/dummy/db/shard_76.sqlite3 +0 -0
- data/spec/dummy/db/shard_802.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +7 -0
- data/spec/dummy/log/test.log +106293 -0
- data/spec/dummy/tmp/cache/313/970/shard%2F30 +1 -0
- data/spec/dummy/tmp/cache/316/980/shard%2F15 +1 -0
- data/spec/dummy/tmp/cache/317/9D0/shard%2F52 +1 -0
- data/spec/dummy/tmp/cache/31C/A40/shard%2F75 +1 -0
- data/spec/dummy/tmp/cache/345/E00/shard%2F500 +1 -0
- data/spec/dummy/tmp/cache/346/DF0/shard%2F321 +1 -0
- data/spec/dummy/tmp/cache/349/DE0/shard%2F126 +1 -0
- data/spec/dummy/tmp/cache/349/E10/shard%2F153 +0 -0
- data/spec/dummy/tmp/cache/349/E20/shard%2F243 +1 -0
- data/spec/dummy/tmp/cache/34A/E20/shard%2F154 +0 -0
- data/spec/dummy/tmp/cache/34A/E40/shard%2F172 +1 -0
- data/spec/dummy/tmp/cache/34A/E70/shard%2F523 +1 -0
- data/spec/dummy/tmp/cache/34A/EB0/shard%2F721 +1 -0
- data/spec/dummy/tmp/cache/34A/EC0/shard%2F730 +1 -0
- data/spec/dummy/tmp/cache/34B/E40/shard%2F245 +0 -0
- data/spec/dummy/tmp/cache/34B/E60/shard%2F425 +1 -0
- data/spec/dummy/tmp/cache/34B/EC0/shard%2F722 +1 -0
- data/spec/dummy/tmp/cache/34C/E50/shard%2F246 +0 -0
- data/spec/dummy/tmp/cache/34C/E80/shard%2F273 +1 -0
- data/spec/dummy/tmp/cache/34C/E90/shard%2F363 +0 -0
- data/spec/dummy/tmp/cache/34C/E90/shard%2F444 +1 -0
- data/spec/dummy/tmp/cache/34C/EA0/shard%2F615 +1 -0
- data/spec/dummy/tmp/cache/34C/F00/shard%2F750 +1 -0
- data/spec/dummy/tmp/cache/34D/E80/shard%2F265 +1 -0
- data/spec/dummy/tmp/cache/34D/ED0/shard%2F391 +1 -0
- data/spec/dummy/tmp/cache/34D/EE0/shard%2F724 +1 -0
- data/spec/dummy/tmp/cache/34E/ED0/shard%2F383 +1 -0
- data/spec/dummy/tmp/cache/34E/EF0/shard%2F725 +1 -0
- data/spec/dummy/tmp/cache/34E/F00/shard%2F653 +1 -0
- data/spec/dummy/tmp/cache/34F/EE0/shard%2F546 +1 -0
- data/spec/dummy/tmp/cache/34F/F00/shard%2F807 +1 -0
- data/spec/dummy/tmp/cache/34F/F30/shard%2F672 +1 -0
- data/spec/dummy/tmp/cache/350/EA0/shard%2F178 +1 -0
- data/spec/dummy/tmp/cache/350/EB0/shard%2F349 +1 -0
- data/spec/dummy/tmp/cache/350/F00/shard%2F718 +0 -0
- data/spec/dummy/tmp/cache/350/F40/shard%2F592 +1 -0
- data/spec/dummy/tmp/cache/351/F00/shard%2F386 +0 -0
- data/spec/dummy/tmp/cache/351/F10/shard%2F638 +1 -0
- data/spec/dummy/tmp/cache/351/F10/shard%2F719 +0 -0
- data/spec/dummy/tmp/cache/352/F10/shard%2F387 +0 -0
- data/spec/dummy/tmp/cache/352/F10/shard%2F468 +1 -0
- data/spec/dummy/tmp/cache/352/F30/shard%2F729 +1 -0
- data/spec/dummy/tmp/cache/353/F10/shard%2F298 +1 -0
- data/spec/dummy/tmp/cache/353/F50/shard%2F739 +1 -0
- data/spec/dummy/tmp/cache/354/F50/shard%2F569 +1 -0
- data/spec/dummy/tmp/cache/354/F80/shard%2F677 +1 -0
- data/spec/dummy/tmp/cache/354/F90/shard%2F767 +1 -0
- data/spec/dummy/tmp/cache/358/FE0/shard%2F699 +1 -0
- data/spec/dummy/tmp/cache/3A5/EA0/shard%2F10004 +1 -0
- data/spec/dummy/tmp/cache/3A6/F40/shard%2F10410 +1 -0
- data/spec/dummy/tmp/cache/3A7/F30/shard%2F10231 +1 -0
- data/spec/dummy/tmp/cache/3A8/F90/shard%2F10520 +1 -0
- data/spec/dummy/tmp/cache/3A9/F40/shard%2F10062 +1 -0
- data/spec/dummy/tmp/cache/3A9/F80/shard%2F10341 +1 -0
- data/spec/dummy/tmp/cache/3AA/F10/shard%2F10027 +1 -0
- data/spec/dummy/tmp/cache/3AA/F10/shard%2F10108 +1 -0
- data/spec/dummy/tmp/cache/3AA/FB0/shard%2F10441 +1 -0
- data/spec/dummy/tmp/cache/3AA/FC0/shard%2F10612 +1 -0
- data/spec/dummy/tmp/cache/3AB/010/shard%2F10730 +1 -0
- data/spec/dummy/tmp/cache/3AB/F70/shard%2F10154 +1 -0
- data/spec/dummy/tmp/cache/3AC/020/shard%2F10650 +1 -0
- data/spec/dummy/tmp/cache/3AC/FC0/shard%2F10272 +1 -0
- data/spec/dummy/tmp/cache/3AD/000/shard%2F10543 +1 -0
- data/spec/dummy/tmp/cache/3AD/F50/shard%2F10039 +1 -0
- data/spec/dummy/tmp/cache/3AD/F90/shard%2F10318 +1 -0
- data/spec/dummy/tmp/cache/3AD/FA0/shard%2F10246 +1 -0
- data/spec/dummy/tmp/cache/3AE/060/shard%2F10751 +1 -0
- data/spec/dummy/tmp/cache/3AE/FF0/shard%2F10364 +1 -0
- data/spec/dummy/tmp/cache/3AF/030/shard%2F10635 +1 -0
- data/spec/dummy/tmp/cache/3B0/FE0/shard%2F10177 +1 -0
- data/spec/dummy/tmp/cache/3B1/030/shard%2F10295 +1 -0
- data/spec/dummy/tmp/cache/3B1/040/shard%2F10466 +1 -0
- data/spec/dummy/tmp/cache/3B1/090/shard%2F10673 +1 -0
- data/spec/dummy/tmp/cache/3B2/070/shard%2F10566 +1 -0
- data/spec/dummy/tmp/cache/3B3/060/shard%2F10387 +1 -0
- data/spec/dummy/tmp/cache/3B5/0B0/shard%2F10497 +1 -0
- data/spec/dummy/tmp/cache/3B6/100/shard%2F10696 +1 -0
- data/spec/dummy/tmp/cache/3B7/0E0/shard%2F10589 +1 -0
- data/spec/dummy/tmp/cache/3BA/160/shard%2F10799 +1 -0
- data/spec/lib/action_controller/caching_spec.rb +4 -4
- data/spec/lib/active_record/abstract_adapter_spec.rb +1 -1
- data/spec/lib/active_record/association_spec.rb +83 -67
- data/spec/lib/active_record/attribute_methods_spec.rb +29 -29
- data/spec/lib/active_record/base_spec.rb +20 -20
- data/spec/lib/active_record/calculations_spec.rb +79 -59
- data/spec/lib/active_record/connection_handler_spec.rb +14 -14
- data/spec/lib/active_record/connection_pool_spec.rb +7 -7
- data/spec/lib/active_record/finder_methods_spec.rb +8 -8
- data/spec/lib/active_record/query_cache_spec.rb +38 -38
- data/spec/lib/active_record/query_methods_spec.rb +45 -45
- data/spec/lib/active_record/relation_spec.rb +8 -8
- data/spec/lib/active_record/spawn_methods_spec.rb +11 -11
- data/spec/lib/connection_pool_proxy_spec.rb +2 -2
- data/spec/lib/database_server_spec.rb +37 -37
- data/spec/lib/default_shard_spec.rb +3 -3
- data/spec/lib/r_spec_helper_spec.rb +33 -7
- data/spec/lib/rails_spec.rb +5 -5
- data/spec/lib/shackles_spec.rb +35 -35
- data/spec/models/shard_spec.rb +104 -104
- data/spec/spec_helper.rb +1 -1
- metadata +254 -29
data/spec/models/shard_spec.rb
CHANGED
@@ -6,52 +6,52 @@ module Switchman
|
|
6
6
|
|
7
7
|
describe ".activate" do
|
8
8
|
it "should activate a hash of shard categories" do
|
9
|
-
Shard.current.
|
10
|
-
Shard.current(:other).
|
9
|
+
expect(Shard.current).to eq Shard.default
|
10
|
+
expect(Shard.current(:other)).to eq Shard.default
|
11
11
|
Shard.activate(:default => @shard1, :other => @shard2) do
|
12
|
-
Shard.current.
|
13
|
-
Shard.current(:other).
|
12
|
+
expect(Shard.current).to eq @shard1
|
13
|
+
expect(Shard.current(:other)).to eq @shard2
|
14
14
|
end
|
15
|
-
Shard.current.
|
16
|
-
Shard.current(:other).
|
15
|
+
expect(Shard.current).to eq Shard.default
|
16
|
+
expect(Shard.current(:other)).to eq Shard.default
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should not allow activating the unsharded category" do
|
20
|
-
Shard.current(:unsharded).
|
20
|
+
expect(Shard.current(:unsharded)).to eq Shard.default
|
21
21
|
Shard.activate(:unsharded => @shard1) do
|
22
|
-
Shard.current(:unsharded).
|
22
|
+
expect(Shard.current(:unsharded)).to eq Shard.default
|
23
23
|
end
|
24
|
-
Shard.current(:unsharded).
|
24
|
+
expect(Shard.current(:unsharded)).to eq Shard.default
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe "#activate" do
|
29
29
|
it "should activate the default category when no args are used" do
|
30
|
-
Shard.current.
|
30
|
+
expect(Shard.current).to eq Shard.default
|
31
31
|
@shard1.activate do
|
32
|
-
Shard.current.
|
32
|
+
expect(Shard.current).to eq @shard1
|
33
33
|
end
|
34
|
-
Shard.current.
|
34
|
+
expect(Shard.current).to eq Shard.default
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should activate other categories" do
|
38
|
-
Shard.current(:other).
|
38
|
+
expect(Shard.current(:other)).to eq Shard.default
|
39
39
|
@shard1.activate(:other) do
|
40
|
-
Shard.current(:other).
|
41
|
-
Shard.current.
|
40
|
+
expect(Shard.current(:other)).to eq @shard1
|
41
|
+
expect(Shard.current).to eq Shard.default
|
42
42
|
end
|
43
|
-
Shard.current(:other).
|
43
|
+
expect(Shard.current(:other)).to eq Shard.default
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should activate multiple categories" do
|
47
|
-
Shard.current.
|
48
|
-
Shard.current(:other).
|
47
|
+
expect(Shard.current).to eq Shard.default
|
48
|
+
expect(Shard.current(:other)).to eq Shard.default
|
49
49
|
@shard1.activate(:default, :other) do
|
50
|
-
Shard.current.
|
51
|
-
Shard.current(:other).
|
50
|
+
expect(Shard.current).to eq @shard1
|
51
|
+
expect(Shard.current(:other)).to eq @shard1
|
52
52
|
end
|
53
|
-
Shard.current.
|
54
|
-
Shard.current(:other).
|
53
|
+
expect(Shard.current).to eq Shard.default
|
54
|
+
expect(Shard.current(:other)).to eq Shard.default
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -60,9 +60,9 @@ module Switchman
|
|
60
60
|
# i.e. the instance var would not be set if we got this back from a cache
|
61
61
|
# that was populated pre-sharding
|
62
62
|
a = User.new
|
63
|
-
a.shard.
|
63
|
+
expect(a.shard).to eq Shard.default
|
64
64
|
a.instance_variable_set(:@shard, nil)
|
65
|
-
a.shard.
|
65
|
+
expect(a.shard).to eq Shard.default
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -73,57 +73,57 @@ module Switchman
|
|
73
73
|
shard = server.create_new_shard
|
74
74
|
shard.activate do
|
75
75
|
User.create!
|
76
|
-
User.count.
|
76
|
+
expect(User.count).to eq 1
|
77
77
|
end
|
78
78
|
shard.drop_database
|
79
79
|
shard.activate do
|
80
|
-
|
80
|
+
expect { User.count }.to raise_error
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
85
|
describe ".lookup" do
|
86
86
|
it "should work with pseudo-ids" do
|
87
|
-
Shard.lookup('default').
|
88
|
-
Shard.lookup('self').
|
87
|
+
expect(Shard.lookup('default')).to eq Shard.default
|
88
|
+
expect(Shard.lookup('self')).to eq Shard.current
|
89
89
|
@shard1.activate do
|
90
|
-
Shard.lookup('default').
|
91
|
-
Shard.lookup('self').
|
90
|
+
expect(Shard.lookup('default')).to eq Shard.default
|
91
|
+
expect(Shard.lookup('self')).to eq Shard.current
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should work with string ids" do
|
96
|
-
Shard.lookup(Shard.current.id.to_s).
|
97
|
-
Shard.lookup(@shard1.id.to_s).
|
96
|
+
expect(Shard.lookup(Shard.current.id.to_s)).to eq Shard.current
|
97
|
+
expect(Shard.lookup(@shard1.id.to_s)).to eq @shard1
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should raise an error for non-ids" do
|
101
|
-
|
101
|
+
expect { Shard.lookup('jacob') }.to raise_error(ArgumentError)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
105
|
describe ".with_each_shard" do
|
106
106
|
describe ":exception" do
|
107
107
|
it "should default to :raise" do
|
108
|
-
|
108
|
+
expect { Shard.with_each_shard { raise "error" } }.to raise_error
|
109
109
|
end
|
110
110
|
|
111
111
|
it "should :ignore" do
|
112
|
-
Shard.with_each_shard(exception: :ignore) { raise "error" }.
|
112
|
+
expect(Shard.with_each_shard(exception: :ignore) { raise "error" }).to eq []
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should :defer" do
|
116
116
|
counter = 0
|
117
|
-
|
117
|
+
expect { Shard.with_each_shard(exception: :defer) { counter += 1; raise "error" } }.to raise_error
|
118
118
|
# called more than once
|
119
|
-
counter.
|
119
|
+
expect(counter).to be > 1
|
120
120
|
end
|
121
121
|
|
122
122
|
it "should call a proc" do
|
123
123
|
counter = 0
|
124
|
-
Shard.with_each_shard(exception: -> { counter += 1 }) { raise "error" }.
|
124
|
+
expect(Shard.with_each_shard(exception: -> { counter += 1 }) { raise "error" }).to eq []
|
125
125
|
# called more than once
|
126
|
-
counter.
|
126
|
+
expect(counter).to be > 1
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
@@ -132,23 +132,23 @@ module Switchman
|
|
132
132
|
|
133
133
|
it "should disconnect when switching among different database servers" do
|
134
134
|
User.connection
|
135
|
-
User.connected
|
135
|
+
expect(User.connected?).to eq true
|
136
136
|
Shard.with_each_shard([Shard.default, @shard2]) {}
|
137
|
-
User.connected
|
137
|
+
expect(User.connected?).to eq false
|
138
138
|
end
|
139
139
|
|
140
140
|
it "should not disconnect when it's the current shard" do
|
141
141
|
User.connection
|
142
|
-
User.connected
|
142
|
+
expect(User.connected?).to eq true
|
143
143
|
Shard.with_each_shard([Shard.default]) {}
|
144
|
-
User.connected
|
144
|
+
expect(User.connected?).to eq true
|
145
145
|
end
|
146
146
|
|
147
147
|
it "should not disconnect for zero shards" do
|
148
148
|
User.connection
|
149
|
-
User.connected
|
149
|
+
expect(User.connected?).to eq true
|
150
150
|
Shard.with_each_shard([]) {}
|
151
|
-
User.connected
|
151
|
+
expect(User.connected?).to eq true
|
152
152
|
end
|
153
153
|
end
|
154
154
|
end
|
@@ -157,35 +157,35 @@ module Switchman
|
|
157
157
|
it "should work" do
|
158
158
|
ids = [2, 48, Shard::IDS_PER_SHARD * @shard1.id + 6, Shard::IDS_PER_SHARD * @shard1.id + 8, 10, 12]
|
159
159
|
results = Shard.partition_by_shard(ids) do |ids|
|
160
|
-
(ids.length == 4 || ids.length == 2).
|
160
|
+
expect(ids.length == 4 || ids.length == 2).to eq true
|
161
161
|
ids.map { |id| id + 1}
|
162
162
|
end
|
163
163
|
|
164
164
|
# could have done either shard first, but we can't sort, because we want to see the shards grouped together
|
165
|
-
(results == [3, 49, 11, 13, 7, 9] ||
|
166
|
-
results == [7, 9, 3, 49, 11, 13]).
|
165
|
+
expect(results == [3, 49, 11, 13, 7, 9] ||
|
166
|
+
results == [7, 9, 3, 49, 11, 13]).to eq true
|
167
167
|
end
|
168
168
|
|
169
169
|
it "should work for a partition_proc that returns a shard" do
|
170
170
|
array = [{:id => 1, :shard => @shard1}, {:id => 2, :shard => @shard2}]
|
171
171
|
results = Shard.partition_by_shard(array, lambda { |a| a[:shard] }) do |objects|
|
172
|
-
objects.length.
|
173
|
-
Shard.current.
|
172
|
+
expect(objects.length).to eq 1
|
173
|
+
expect(Shard.current).to eq objects.first[:shard]
|
174
174
|
objects.first[:id]
|
175
175
|
end
|
176
|
-
results.sort.
|
176
|
+
expect(results.sort).to eq [1, 2]
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should support shortened id syntax, and strings" do
|
180
180
|
ids = [@shard1.global_id_for(1), "#{@shard2.id}~2"]
|
181
181
|
result = Shard.partition_by_shard(ids) do |ids|
|
182
|
-
ids.length.
|
183
|
-
[@shard1, @shard2].include?(Shard.current).
|
184
|
-
ids.first.
|
185
|
-
ids.first.
|
182
|
+
expect(ids.length).to eq 1
|
183
|
+
expect([@shard1, @shard2].include?(Shard.current)).to eq true
|
184
|
+
expect(ids.first).to eq 1 if Shard.current == @shard1
|
185
|
+
expect(ids.first).to eq 2 if Shard.current == @shard2
|
186
186
|
ids.first
|
187
187
|
end
|
188
|
-
result.sort.
|
188
|
+
expect(result.sort).to eq [1, 2]
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should partition unrecognized types unchanged into current shard" do
|
@@ -194,7 +194,7 @@ module Switchman
|
|
194
194
|
result = Shard.partition_by_shard(items) do |shard_items|
|
195
195
|
[Shard.current, shard_items]
|
196
196
|
end
|
197
|
-
result.
|
197
|
+
expect(result).to eq [expected_shard, items]
|
198
198
|
end
|
199
199
|
|
200
200
|
it "should partition unrecognized strings unchanged into current shard" do
|
@@ -203,7 +203,7 @@ module Switchman
|
|
203
203
|
result = Shard.partition_by_shard(items) do |shard_items|
|
204
204
|
[Shard.current, shard_items]
|
205
205
|
end
|
206
|
-
result.
|
206
|
+
expect(result).to eq [expected_shard, items]
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should partition recognized ids with an invalid shard unchanged into current shard" do
|
@@ -213,28 +213,28 @@ module Switchman
|
|
213
213
|
result = Shard.partition_by_shard(items) do |shard_items|
|
214
214
|
[Shard.current, shard_items]
|
215
215
|
end
|
216
|
-
result.
|
216
|
+
expect(result).to eq [expected_shard, items]
|
217
217
|
end
|
218
218
|
end
|
219
219
|
|
220
220
|
describe "#name" do
|
221
221
|
it "the default shard should not be marked as dirty after reading its name" do
|
222
222
|
s = Shard.default
|
223
|
-
s.
|
223
|
+
expect(s).not_to be_new_record
|
224
224
|
s.name
|
225
|
-
s.
|
225
|
+
expect(s).not_to be_changed
|
226
226
|
end
|
227
227
|
|
228
228
|
it "should fall back to shard_name in the config if nil" do
|
229
229
|
db = DatabaseServer.new(config: { adapter: 'mysql', database: 'canvas', shard_name: 'yoyoyo' })
|
230
230
|
shard = Shard.new(database_server: db)
|
231
|
-
shard.name.
|
231
|
+
expect(shard.name).to eq 'yoyoyo'
|
232
232
|
end
|
233
233
|
|
234
234
|
it "should fall back to the database_server if nil" do
|
235
235
|
db = DatabaseServer.new(config: { adapter: 'mysql', database: 'canvas' })
|
236
236
|
shard = Shard.new(database_server: db)
|
237
|
-
shard.name.
|
237
|
+
expect(shard.name).to eq 'canvas'
|
238
238
|
end
|
239
239
|
|
240
240
|
it "should get it from the postgres connection if not otherwise specified" do
|
@@ -251,7 +251,7 @@ module Switchman
|
|
251
251
|
connection.stubs(:run_callbacks).returns(nil)
|
252
252
|
::ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:checkout).returns(connection)
|
253
253
|
begin
|
254
|
-
shard.name.
|
254
|
+
expect(shard.name).to eq 'canvas'
|
255
255
|
ensure
|
256
256
|
shard.activate { ::ActiveRecord::Base.connection_pool.current_pool.disconnect! }
|
257
257
|
end
|
@@ -260,12 +260,12 @@ module Switchman
|
|
260
260
|
|
261
261
|
describe ".shard_for" do
|
262
262
|
it "should work" do
|
263
|
-
Shard.shard_for(1).
|
264
|
-
Shard.shard_for(1, @shard1).
|
265
|
-
Shard.shard_for(@shard1.global_id_for(1)).
|
266
|
-
Shard.shard_for(Shard.default.global_id_for(1)).
|
267
|
-
Shard.shard_for(@shard1.global_id_for(1), @shard1).
|
268
|
-
Shard.shard_for(Shard.default.global_id_for(1), @shard1).
|
263
|
+
expect(Shard.shard_for(1)).to eq Shard.default
|
264
|
+
expect(Shard.shard_for(1, @shard1)).to eq @shard1
|
265
|
+
expect(Shard.shard_for(@shard1.global_id_for(1))).to eq @shard1
|
266
|
+
expect(Shard.shard_for(Shard.default.global_id_for(1))).to eq Shard.default
|
267
|
+
expect(Shard.shard_for(@shard1.global_id_for(1), @shard1)).to eq @shard1
|
268
|
+
expect(Shard.shard_for(Shard.default.global_id_for(1), @shard1)).to eq Shard.default
|
269
269
|
end
|
270
270
|
end
|
271
271
|
|
@@ -274,36 +274,36 @@ module Switchman
|
|
274
274
|
expected_id = 1
|
275
275
|
expected_shard = @shard2
|
276
276
|
id, shard = Shard.local_id_for("#{expected_shard.id}~#{expected_id}")
|
277
|
-
id.
|
278
|
-
shard.
|
277
|
+
expect(id).to eq expected_id
|
278
|
+
expect(shard).to eq expected_shard
|
279
279
|
end
|
280
280
|
|
281
281
|
it "should recognize global ids" do
|
282
282
|
expected_id = 1
|
283
283
|
expected_shard = @shard2
|
284
284
|
id, shard = Shard.local_id_for(Shard::IDS_PER_SHARD * expected_shard.id + expected_id)
|
285
|
-
id.
|
286
|
-
shard.
|
285
|
+
expect(id).to eq expected_id
|
286
|
+
expect(shard).to eq expected_shard
|
287
287
|
end
|
288
288
|
|
289
289
|
it "should recognize local ids with no shard" do
|
290
290
|
expected_id = 1
|
291
291
|
id, shard = Shard.local_id_for(expected_id)
|
292
|
-
id.
|
293
|
-
shard.
|
292
|
+
expect(id).to eq expected_id
|
293
|
+
expect(shard).to be_nil
|
294
294
|
end
|
295
295
|
|
296
296
|
it "should return nil for unrecognized input" do
|
297
297
|
id, shard = Shard.local_id_for("not an id")
|
298
|
-
id.
|
299
|
-
shard.
|
298
|
+
expect(id).to be_nil
|
299
|
+
expect(shard).to be_nil
|
300
300
|
end
|
301
301
|
|
302
302
|
it "should return nil for ids with bad shard values" do
|
303
303
|
bad_shard_id = @shard2.id + 10000
|
304
304
|
id, shard = Shard.local_id_for("#{bad_shard_id}~1")
|
305
|
-
id.
|
306
|
-
shard.
|
305
|
+
expect(id).to be_nil
|
306
|
+
expect(shard).to be_nil
|
307
307
|
end
|
308
308
|
end
|
309
309
|
|
@@ -315,69 +315,69 @@ module Switchman
|
|
315
315
|
|
316
316
|
describe ".integral_id" do
|
317
317
|
it "should return recognized ids" do
|
318
|
-
Shard.integral_id_for(@local_id).
|
319
|
-
Shard.integral_id_for(@local_id.to_s).
|
320
|
-
Shard.integral_id_for(@global_id).
|
321
|
-
Shard.integral_id_for(@global_id.to_s).
|
322
|
-
Shard.integral_id_for("#{@shard1.id}~#{@local_id}").
|
318
|
+
expect(Shard.integral_id_for(@local_id)).to eq @local_id
|
319
|
+
expect(Shard.integral_id_for(@local_id.to_s)).to eq @local_id
|
320
|
+
expect(Shard.integral_id_for(@global_id)).to eq @global_id
|
321
|
+
expect(Shard.integral_id_for(@global_id.to_s)).to eq @global_id
|
322
|
+
expect(Shard.integral_id_for("#{@shard1.id}~#{@local_id}")).to eq @global_id
|
323
323
|
end
|
324
324
|
|
325
325
|
it "should work even for shards that don't exist" do
|
326
326
|
shard = Shard.create!
|
327
327
|
shard.destroy
|
328
328
|
global_id = shard.global_id_for(1)
|
329
|
-
Shard.integral_id_for(global_id).
|
330
|
-
Shard.integral_id_for(global_id.to_s).
|
331
|
-
Shard.integral_id_for("#{shard.id}~1").
|
329
|
+
expect(Shard.integral_id_for(global_id)).to eq global_id
|
330
|
+
expect(Shard.integral_id_for(global_id.to_s)).to eq global_id
|
331
|
+
expect(Shard.integral_id_for("#{shard.id}~1")).to eq global_id
|
332
332
|
end
|
333
333
|
|
334
334
|
it "should return nil for unrecognized ids" do
|
335
|
-
Shard.integral_id_for('not an id').
|
335
|
+
expect(Shard.integral_id_for('not an id')).to eq nil
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
339
339
|
describe ".local_id_for" do
|
340
340
|
it "should return id without shard for local id" do
|
341
|
-
Shard.local_id_for(@local_id).
|
341
|
+
expect(Shard.local_id_for(@local_id)).to eq [@local_id, nil]
|
342
342
|
end
|
343
343
|
|
344
344
|
it "should return id with shard for global id" do
|
345
|
-
Shard.local_id_for(@global_id).
|
345
|
+
expect(Shard.local_id_for(@global_id)).to eq [@local_id, @shard1]
|
346
346
|
end
|
347
347
|
|
348
348
|
it "should return nil for shards that don't exist" do
|
349
349
|
shard = Shard.create!
|
350
350
|
shard.destroy
|
351
|
-
Shard.local_id_for(shard.global_id_for(1)).
|
351
|
+
expect(Shard.local_id_for(shard.global_id_for(1))).to eq [nil, nil]
|
352
352
|
end
|
353
353
|
|
354
354
|
it "should return nil for unrecognized ids" do
|
355
|
-
Shard.local_id_for('not an id').
|
355
|
+
expect(Shard.local_id_for('not an id')).to eq [nil, nil]
|
356
356
|
end
|
357
357
|
end
|
358
358
|
|
359
359
|
describe ".relative_id_for" do
|
360
360
|
it "should return recognized ids relative to the target shard" do
|
361
|
-
Shard.relative_id_for(@local_id, @shard1, @shard2).
|
362
|
-
Shard.relative_id_for(@local_id, @shard2, @shard2).
|
363
|
-
Shard.relative_id_for(@global_id, @shard1, @shard2).
|
364
|
-
Shard.relative_id_for(@global_id, @shard2, @shard2).
|
361
|
+
expect(Shard.relative_id_for(@local_id, @shard1, @shard2)).to eq @global_id
|
362
|
+
expect(Shard.relative_id_for(@local_id, @shard2, @shard2)).to eq @local_id
|
363
|
+
expect(Shard.relative_id_for(@global_id, @shard1, @shard2)).to eq @global_id
|
364
|
+
expect(Shard.relative_id_for(@global_id, @shard2, @shard2)).to eq @global_id
|
365
365
|
end
|
366
366
|
|
367
367
|
it "should return the original id for unrecognized ids" do
|
368
|
-
Shard.relative_id_for('not an id', @shard1, @shard2).
|
368
|
+
expect(Shard.relative_id_for('not an id', @shard1, @shard2)).to eq 'not an id'
|
369
369
|
end
|
370
370
|
end
|
371
371
|
|
372
372
|
describe ".short_id_for" do
|
373
373
|
it "should return shorted strings for global ids" do
|
374
|
-
Shard.short_id_for(@local_id).
|
375
|
-
Shard.short_id_for("#{@local_id}").
|
376
|
-
Shard.short_id_for(@global_id).
|
374
|
+
expect(Shard.short_id_for(@local_id)).to eq @local_id
|
375
|
+
expect(Shard.short_id_for("#{@local_id}")).to eq @local_id
|
376
|
+
expect(Shard.short_id_for(@global_id)).to eq "#{@shard1.id}~#{@local_id}"
|
377
377
|
end
|
378
378
|
|
379
379
|
it "should return the original id for unrecognized ids" do
|
380
|
-
Shard.short_id_for('not an id').
|
380
|
+
expect(Shard.short_id_for('not an id')).to eq 'not an id'
|
381
381
|
end
|
382
382
|
end
|
383
383
|
|
@@ -386,7 +386,7 @@ module Switchman
|
|
386
386
|
local_id = 5
|
387
387
|
Shard.with_each_shard do
|
388
388
|
global_id = Shard.current.global_id_for(local_id)
|
389
|
-
Shard.global_id_for(global_id).
|
389
|
+
expect(Shard.global_id_for(global_id)).to eq global_id
|
390
390
|
end
|
391
391
|
end
|
392
392
|
|
@@ -394,7 +394,7 @@ module Switchman
|
|
394
394
|
local_id = 5
|
395
395
|
Shard.with_each_shard do
|
396
396
|
next if Shard.current == Shard.default
|
397
|
-
Shard.shard_for(Shard.global_id_for(local_id)).
|
397
|
+
expect(Shard.shard_for(Shard.global_id_for(local_id))).to eq Shard.current
|
398
398
|
end
|
399
399
|
end
|
400
400
|
end
|