Skip to content

Commit 3e398ab

Browse files
RUBY-3794 Update unit tests for phase 1 constants and removed API
1 parent 5efd898 commit 3e398ab

3 files changed

Lines changed: 15 additions & 113 deletions

File tree

spec/mongo/retryable/backpressure_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@
1212
expect(described_class::MAX_BACKOFF).to eq(10)
1313
end
1414

15-
it 'defines MAX_RETRIES as 5' do
16-
expect(described_class::MAX_RETRIES).to eq(5)
17-
end
18-
19-
it 'defines RETRY_TOKEN_RETURN_RATE as 0.1' do
20-
expect(described_class::RETRY_TOKEN_RETURN_RATE).to eq(0.1)
21-
end
22-
23-
it 'defines DEFAULT_RETRY_TOKEN_CAPACITY as 1000' do
24-
expect(described_class::DEFAULT_RETRY_TOKEN_CAPACITY).to eq(1000)
15+
it 'defines DEFAULT_MAX_RETRIES as 2' do
16+
expect(described_class::DEFAULT_MAX_RETRIES).to eq(2)
2517
end
2618
end
2719

spec/mongo/retryable/read_worker_overload_spec.rb

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ def make_retryable_error(message = 'not master')
6060
describe '#read_with_retry with overload errors' do
6161
context 'when an overload error is raised and then succeeds' do
6262
it 'retries with backoff and returns the result' do
63-
expect(worker).to receive(:sleep).at_least(:twice)
63+
expect(worker).to receive(:sleep).at_least(:once)
6464
call_count = 0
6565
result = worker.read_with_retry(session, server_selector, context) do |_server, _is_retry|
6666
call_count += 1
67-
raise make_overload_error if call_count <= 3
67+
raise make_overload_error if call_count <= 2
6868

6969
:success
7070
end
7171

7272
expect(result).to eq(:success)
73-
expect(call_count).to eq(4)
73+
expect(call_count).to eq(3)
7474
end
7575
end
7676

77-
context 'when overload errors exceed MAX_RETRIES' do
78-
it 'raises after MAX_RETRIES' do
79-
max = Mongo::Retryable::Backpressure::MAX_RETRIES
77+
context 'when overload errors exceed DEFAULT_MAX_RETRIES' do
78+
it 'raises after DEFAULT_MAX_RETRIES' do
79+
max = Mongo::Retryable::Backpressure::DEFAULT_MAX_RETRIES
8080
call_count = 0
8181

8282
expect do
@@ -105,42 +105,5 @@ def make_retryable_error(message = 'not master')
105105
expect(call_count).to eq(2)
106106
end
107107
end
108-
109-
context 'when adaptive retries are enabled and bucket is drained' do
110-
let(:retry_policy) { Mongo::Retryable::RetryPolicy.new(adaptive_retries: true) }
111-
112-
it 'raises when the token bucket is empty' do
113-
bucket = retry_policy.token_bucket
114-
bucket.tokens.to_i.times { bucket.consume(1) }
115-
116-
expect do
117-
worker.read_with_retry(session, server_selector, context) do |_server, _is_retry|
118-
raise make_overload_error
119-
end
120-
end.to raise_error(Mongo::Error::OperationFailure, /overloaded/)
121-
end
122-
end
123-
124-
context 'when record_success is called on success' do
125-
it 'calls record_success(is_retry: false) on first-attempt success' do
126-
expect(retry_policy).to receive(:record_success).with(is_retry: false)
127-
128-
worker.read_with_retry(session, server_selector, context) do |_server|
129-
:ok
130-
end
131-
end
132-
133-
it 'calls record_success(is_retry: true) after a retry succeeds' do
134-
call_count = 0
135-
expect(retry_policy).to receive(:record_success).with(is_retry: true)
136-
137-
worker.read_with_retry(session, server_selector, context) do |_server, _is_retry|
138-
call_count += 1
139-
raise make_overload_error if call_count == 1
140-
141-
:ok
142-
end
143-
end
144-
end
145108
end
146109
end

spec/mongo/retryable/write_worker_overload_spec.rb

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010
end
1111

12-
let(:retry_policy) { Mongo::Retryable::RetryPolicy.new(adaptive_retries: false) }
12+
let(:retry_policy) { Mongo::Retryable::RetryPolicy.new }
1313

1414
let(:client) do
1515
instance_double(Mongo::Client).tap do |c|
@@ -94,18 +94,6 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
9494
end
9595

9696
describe '#modern_write_with_retry' do
97-
context 'when the operation succeeds on first attempt' do
98-
it 'records success and returns the result' do
99-
expect(retry_policy).to receive(:record_success).with(is_retry: false)
100-
101-
result = worker.modern_write_with_retry(session, server, context) do |_conn, _txn, _ctx|
102-
:ok
103-
end
104-
105-
expect(result).to eq(:ok)
106-
end
107-
end
108-
10997
context 'when an overload error occurs' do
11098
it 'enters the overload retry loop' do
11199
call_count = 0
@@ -161,21 +149,16 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
161149

162150
expect(result).to eq(:write_ok)
163151
end
164-
165-
it 'records success on retry' do
166-
expect(retry_policy).to receive(:record_success).with(is_retry: true)
167-
call_overload_retry(worker) { |_c, _t, _x| :ok }
168-
end
169152
end
170153

171154
context 'with multiple overload errors' do
172155
it 'retries multiple times with backoff' do
173156
call_count = 0
174-
expect(worker).to receive(:sleep).exactly(3).times
157+
expect(worker).to receive(:sleep).twice
175158

176159
result = call_overload_retry(worker) do |_c, _t, _x|
177160
call_count += 1
178-
raise make_overload_error if call_count < 3
161+
raise make_overload_error if call_count < 2
179162

180163
:finally_ok
181164
end
@@ -184,9 +167,9 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
184167
end
185168
end
186169

187-
context 'when MAX_RETRIES (5) is exceeded' do
170+
context 'when DEFAULT_MAX_RETRIES (2) is exceeded' do
188171
it 'raises the last error' do
189-
max = Mongo::Retryable::Backpressure::MAX_RETRIES + 1
172+
max = Mongo::Retryable::Backpressure::DEFAULT_MAX_RETRIES + 1
190173

191174
expect do
192175
call_overload_retry(worker, error_count: max) { |_c, _t, _x| :should_not_reach }
@@ -205,7 +188,7 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
205188
end
206189
end.to raise_error(Mongo::Error::OperationFailure, /overloaded/)
207190

208-
expect(call_count).to eq(Mongo::Retryable::Backpressure::MAX_RETRIES)
191+
expect(call_count).to eq(Mongo::Retryable::Backpressure::DEFAULT_MAX_RETRIES)
209192
end
210193
end
211194

@@ -241,9 +224,8 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
241224
end
242225

243226
context 'when a non-overload retryable error occurs during overload loop' do
244-
it 'records non-overload failure and continues retrying' do
227+
it 'continues retrying' do
245228
call_count = 0
246-
expect(retry_policy).to receive(:record_non_overload_retry_failure).once
247229

248230
result = call_overload_retry(worker) do |_c, _t, _x|
249231
call_count += 1
@@ -256,39 +238,4 @@ def call_overload_retry(wkr, error: nil, error_count: 1, &block)
256238
end
257239
end
258240
end
259-
260-
describe 'record_success on retry_write path' do
261-
it 'records success after standard retry succeeds' do
262-
expect(retry_policy).to receive(:record_success).with(is_retry: true)
263-
call_count = 0
264-
265-
worker.modern_write_with_retry(session, server, context) do |_conn, _txn, _ctx|
266-
call_count += 1
267-
raise make_retryable_write_error if call_count == 1
268-
269-
:ok
270-
end
271-
end
272-
end
273-
274-
describe 'adaptive retries (token bucket)' do
275-
let(:retry_policy) { Mongo::Retryable::RetryPolicy.new(adaptive_retries: true) }
276-
277-
context 'when the token bucket is exhausted' do
278-
before { retry_policy.token_bucket.consume(retry_policy.token_bucket.capacity) }
279-
280-
it 'raises the error instead of retrying' do
281-
expect do
282-
call_overload_retry(worker) { |_c, _t, _x| :no }
283-
end.to raise_error(Mongo::Error::OperationFailure, /overloaded/)
284-
end
285-
end
286-
287-
context 'when there are tokens available' do
288-
it 'retries and records success' do
289-
expect(retry_policy).to receive(:record_success).with(is_retry: true)
290-
call_overload_retry(worker) { |_c, _t, _x| :ok }
291-
end
292-
end
293-
end
294241
end

0 commit comments

Comments
 (0)