Skip to content

Commit 51b49aa

Browse files
committed
Fix logger issue
1 parent a29e832 commit 51b49aa

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

spec/cmab_client_spec.rb

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
describe Optimizely::DefaultCmabClient do
2222
let(:mock_http_client) { double('http_client') }
23-
let(:mock_logger) { double('logger') }
23+
let(:spy_logger) { spy('logger') }
2424
let(:retry_config) { Optimizely::CmabRetryConfig.new(max_retries: 3, retry_delay: 0.01, max_backoff: 1, backoff_multiplier: 2) }
25-
let(:client) { described_class.new(http_client: mock_http_client, logger: mock_logger, retry_config: nil) }
25+
let(:client) { described_class.new(http_client: mock_http_client, logger: spy_logger, retry_config: nil) }
2626
let(:rule_id) { 'test_rule' }
2727
let(:user_id) { 'user123' }
2828
let(:attributes) { {'attr1': 'value1', 'attr2': 'value2'} }
@@ -60,12 +60,12 @@
6060

6161
it 'should return HTTP exception without retrying' do
6262
allow(mock_http_client).to receive(:post).and_raise(StandardError.new('Connection error'))
63-
allow(mock_logger).to receive(:error)
63+
6464
expect do
6565
client.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
6666
end.to raise_error(Optimizely::CmabFetchError, /Connection error/)
6767
expect(mock_http_client).to have_received(:post).once
68-
expect(mock_logger).to have_received(:error).with(a_string_including('Connection error'))
68+
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Connection error'))
6969
end
7070

7171
it 'should not return 200 status without retrying' do
@@ -84,7 +84,7 @@
8484
timeout: 10
8585
)
8686
)
87-
expect(mock_logger).to have_received(:error).with(a_string_including('500'))
87+
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('500'))
8888
end
8989

9090
it 'should return invalid json without retrying' do
@@ -104,7 +104,7 @@
104104
timeout: 10
105105
)
106106
)
107-
expect(mock_logger).to have_received(:error).with(a_string_including('Invalid CMAB fetch response'))
107+
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response'))
108108
end
109109

110110
it 'should return invalid response structure without retrying' do
@@ -123,13 +123,13 @@
123123
timeout: 10
124124
)
125125
)
126-
expect(mock_logger).to have_received(:error).with(a_string_including('Invalid CMAB fetch response'))
126+
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Invalid CMAB fetch response'))
127127
end
128128

129129
it 'should return the variation id on first try with retry config but no retry needed' do
130130
client_with_retry = described_class.new(
131131
http_client: mock_http_client,
132-
logger: mock_logger,
132+
logger: spy_logger,
133133
retry_config: retry_config
134134
)
135135

@@ -155,7 +155,7 @@
155155
it 'should return the variation id on third try with retry config' do
156156
client_with_retry = described_class.new(
157157
http_client: mock_http_client,
158-
logger: mock_logger,
158+
logger: spy_logger,
159159
retry_config: retry_config
160160
)
161161

@@ -167,7 +167,6 @@
167167
call_sequence = [failure_response, failure_response, success_response]
168168
allow(mock_http_client).to receive(:post) { call_sequence.shift }
169169

170-
allow(mock_logger).to receive(:info)
171170
allow_any_instance_of(Object).to receive(:sleep)
172171

173172
result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
@@ -186,8 +185,8 @@
186185
)
187186

188187
# Verify retry logging
189-
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 1) after 0.01 seconds...')
190-
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 2) after 0.02 seconds...')
188+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...')
189+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...')
191190

192191
# Verify sleep was called with correct backoff times
193192
expect_any_instance_of(Object).to have_received(:sleep).with(0.01)
@@ -197,7 +196,7 @@
197196
it 'should exhausts all retry attempts' do
198197
client_with_retry = described_class.new(
199198
http_client: mock_http_client,
200-
logger: mock_logger,
199+
logger: spy_logger,
201200
retry_config: retry_config
202201
)
203202

@@ -206,8 +205,6 @@
206205

207206
# All attempts fail
208207
allow(mock_http_client).to receive(:post).and_return(failure_response)
209-
allow(mock_logger).to receive(:info)
210-
allow(mock_logger).to receive(:error)
211208
allow_any_instance_of(Object).to receive(:sleep)
212209

213210
expect do
@@ -218,16 +215,16 @@
218215
expect(mock_http_client).to have_received(:post).exactly(4).times
219216

220217
# Verify retry logging
221-
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 1) after 0.01 seconds...')
222-
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 2) after 0.02 seconds...')
223-
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 3) after 0.08 seconds...')
218+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 1) after 0.01 seconds...')
219+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 2) after 0.02 seconds...')
220+
expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Retrying CMAB request (attempt 3) after 0.08 seconds...')
224221

225222
# Verify sleep was called for each retry
226223
expect_any_instance_of(Object).to have_received(:sleep).with(0.01)
227224
expect_any_instance_of(Object).to have_received(:sleep).with(0.02)
228225
expect_any_instance_of(Object).to have_received(:sleep).with(0.08)
229226

230227
# Verify final error logging
231-
expect(mock_logger).to have_received(:error).with(a_string_including('Max retries exceeded for CMAB request'))
228+
expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Max retries exceeded for CMAB request'))
232229
end
233230
end

0 commit comments

Comments
 (0)