Skip to content

Commit 3affe83

Browse files
committed
Fix other issues
1 parent 100d82e commit 3affe83

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

spec/cmab_client_spec.rb

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
it 'should return the variation id on success without retrying' do
5555
client = described_class.new(mock_http_client, nil, spy_logger)
5656
mock_response = double('response', status_code: 200, json: {'predictions' => [{'variationId' => 'abc123'}]})
57-
allow(mock_http_client).to receive(:post).and_return(mock_response)
57+
allow(mock_http_client).to receive(:post).and_return(mock_response)
5858
result = client.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
5959
expect(result).to eq('abc123')
6060
expect(mock_http_client).to have_received(:post).with(
@@ -65,6 +65,7 @@
6565
timeout: 10
6666
)
6767
).once
68+
expect(Kernel).not_to have_received(:sleep)
6869
end
6970

7071
it 'should return HTTP exception without retrying' do
@@ -76,6 +77,7 @@
7677
end.to raise_error(Optimizely::CmabFetchError, /Connection error/)
7778
expect(mock_http_client).to have_received(:post).once
7879
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Connection error'))
80+
expect(Kernel).not_to have_received(:sleep)
7981
end
8082

8183
it 'should not return 200 status without retrying' do
@@ -96,6 +98,7 @@
9698
)
9799
).once
98100
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('500'))
101+
expect(Kernel).not_to have_received(:sleep)
99102
end
100103

101104
it 'should return invalid json without retrying' do
@@ -117,6 +120,7 @@
117120
)
118121
).once
119122
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response'))
123+
expect(Kernel).not_to have_received(:sleep)
120124
end
121125

122126
it 'should return invalid response structure without retrying' do
@@ -137,6 +141,7 @@
137141
)
138142
).once
139143
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response'))
144+
expect(Kernel).not_to have_received(:sleep)
140145
end
141146

142147
it 'should return the variation id on first try with retry config but no retry needed' do
@@ -168,37 +173,21 @@
168173
success_response = double('response', status_code: 200, json: {'predictions' => [{'variationId' => 'xyz456'}]})
169174

170175
# First two calls fail, third succeeds
171-
call_sequence = [failure_response, failure_response, success_response]
172-
allow(mock_http_client).to receive(:post) { call_sequence.shift }
176+
allow(mock_http_client).to receive(:post).and_return(failure_response, failure_response, success_response)
173177

174178
result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
175179

176180
expect(result).to eq('xyz456')
177181
expect(mock_http_client).to have_received(:post).exactly(3).times
178182

179-
# Verify all HTTP calls used correct parameters
180-
# This expectation should not be here if you want to count calls.
181-
# It would be better to assert that the last call had the correct parameters,
182-
# or that *any* call had the correct parameters.
183-
# For this test, verifying the total call count and sleep times is more relevant.
184-
# If you want to check the arguments for each of the 3 calls, you'd need a different approach.
185-
# For now, let's remove this specific `with` expectation to avoid conflicts with `exactly(3).times`.
186-
# expect(mock_http_client).to have_received(:post).with(
187-
# expected_url,
188-
# hash_including(
189-
# json: expected_body,
190-
# headers: expected_headers,
191-
# timeout: 10
192-
# )
193-
# )
194-
195183
# Verify retry logging
196-
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...')
197-
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...')
184+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...').once
185+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...').once
198186

199187
# Verify sleep was called with correct backoff times
200188
expect(Kernel).to have_received(:sleep).with(0.01).once
201189
expect(Kernel).to have_received(:sleep).with(0.02).once
190+
expect(Kernel).not_to have_received(:sleep).with(0.08)
202191
end
203192

204193
it 'should exhausts all retry attempts' do
@@ -214,13 +203,13 @@
214203
client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
215204
end.to raise_error(Optimizely::CmabFetchError)
216205

217-
# Verify all attempts were made (1 initial + 3 retries)
206+
# Verify all attempts were made (1 initial + 3 retries = 4 calls)
218207
expect(mock_http_client).to have_received(:post).exactly(4).times
219208

220209
# Verify retry logging
221-
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...')
222-
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...')
223-
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 3) after 0.08 seconds...')
210+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...').once
211+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...').once
212+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 3) after 0.08 seconds...').once
224213

225214
# Verify sleep was called for each retry
226215
expect(Kernel).to have_received(:sleep).with(0.01).once

0 commit comments

Comments
 (0)