blob: 05b44579c6249cc10fa5190545a3934d737d99fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Session do
after do
described_class.clear_session
end
describe '.current' do
it 'returns the current session' do
expect(described_class.current).to be_an_instance_of(described_class)
end
end
describe '.clear_session' do
it 'clears the current session' do
described_class.current
described_class.clear_session
expect(RequestStore[described_class::CACHE_KEY]).to be_nil
end
end
describe '.without_sticky_writes' do
it 'ignores sticky write events sent by a connection proxy' do
described_class.without_sticky_writes do
described_class.current.write!
end
session = described_class.current
expect(session).not_to be_using_primary
end
it 'still is aware of write that happened' do
described_class.without_sticky_writes do
described_class.current.write!
end
session = described_class.current
expect(session.performed_write?).to be true
end
end
describe '#use_primary?' do
it 'returns true when the primary should be used' do
instance = described_class.new
instance.use_primary!
expect(instance.use_primary?).to eq(true)
end
it 'returns false when a secondary should be used' do
expect(described_class.new.use_primary?).to eq(false)
end
it 'returns true when a write was performed' do
instance = described_class.new
instance.write!
expect(instance.use_primary?).to eq(true)
end
end
describe '#use_primary' do
let(:instance) { described_class.new }
context 'when primary was used before' do
before do
instance.write!
end
it 'restores state after use' do
expect { |blk| instance.use_primary(&blk) }.to yield_with_no_args
expect(instance.use_primary?).to eq(true)
end
end
context 'when primary was not used' do
it 'restores state after use' do
expect { |blk| instance.use_primary(&blk) }.to yield_with_no_args
expect(instance.use_primary?).to eq(false)
end
end
it 'uses primary during block' do
expect do |blk|
instance.use_primary do
expect(instance.use_primary?).to eq(true)
# call yield probe
blk.to_proc.call
end
end.to yield_control
end
it 'continues using primary when write was performed' do
instance.use_primary do
instance.write!
end
expect(instance.use_primary?).to eq(true)
end
end
describe '#performed_write?' do
it 'returns true if a write was performed' do
instance = described_class.new
instance.write!
expect(instance.performed_write?).to eq(true)
end
end
describe '#ignore_writes' do
it 'ignores write events' do
instance = described_class.new
instance.ignore_writes { instance.write! }
expect(instance).not_to be_using_primary
expect(instance.performed_write?).to eq true
end
it 'does not prevent using primary if an exception is raised' do
instance = described_class.new
begin
instance.ignore_writes { raise ArgumentError }
rescue ArgumentError
nil
end
instance.write!
expect(instance).to be_using_primary
end
end
describe '#use_replicas_for_read_queries' do
let(:instance) { described_class.new }
it 'sets the flag inside the block' do
expect do |blk|
instance.use_replicas_for_read_queries do
expect(instance.use_replicas_for_read_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.use_replicas_for_read_queries?).to eq(false)
end
it 'restores state after use' do
expect do |blk|
instance.use_replicas_for_read_queries do
instance.use_replicas_for_read_queries do
expect(instance.use_replicas_for_read_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
expect(instance.use_replicas_for_read_queries?).to eq(true)
end
end.to yield_control
expect(instance.use_replicas_for_read_queries?).to eq(false)
end
context 'when primary was used before' do
before do
instance.use_primary!
end
it 'sets the flag inside the block' do
expect do |blk|
instance.use_replicas_for_read_queries do
expect(instance.use_replicas_for_read_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.use_replicas_for_read_queries?).to eq(false)
end
end
context 'when a write query is performed before' do
before do
instance.write!
end
it 'sets the flag inside the block' do
expect do |blk|
instance.use_replicas_for_read_queries do
expect(instance.use_replicas_for_read_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.use_replicas_for_read_queries?).to eq(false)
end
end
end
describe '#fallback_to_replicas_for_ambiguous_queries' do
let(:instance) { described_class.new }
it 'sets the flag inside the block' do
expect do |blk|
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
it 'restores state after use' do
expect do |blk|
instance.fallback_to_replicas_for_ambiguous_queries do
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
# call yield probe
blk.to_proc.call
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
end
end.to yield_control
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
context 'when primary was used before' do
before do
instance.use_primary!
end
it 'uses primary during block' do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
expect do |blk|
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
end
context 'when a write was performed before' do
before do
instance.write!
end
it 'uses primary during block' do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
expect do |blk|
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
# call yield probe
blk.to_proc.call
end
end.to yield_control
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
end
context 'when primary was used inside the block' do
it 'uses primary aterward' do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
instance.use_primary!
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
it 'restores state after use' do
instance.fallback_to_replicas_for_ambiguous_queries do
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
instance.use_primary!
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
end
context 'when a write was performed inside the block' do
it 'uses primary aterward' do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
instance.write!
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
it 'restores state after use' do
instance.fallback_to_replicas_for_ambiguous_queries do
instance.fallback_to_replicas_for_ambiguous_queries do
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(true)
instance.write!
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
expect(instance.fallback_to_replicas_for_ambiguous_queries?).to eq(false)
end
end
end
end
|