|
260 | 260 | allow(SecureRandom).to receive(:uuid).and_return(test_uuid) |
261 | 261 | event_manager = Optimizely::OdpEventManager.new(logger: spy_logger) |
262 | 262 | retry_count = event_manager.instance_variable_get('@retry_count') |
263 | | - allow(event_manager.api_manager).to receive(:send_odp_events).exactly(retry_count).times.with(api_key, api_host, odp_events).and_return(true) |
| 263 | + allow(event_manager.api_manager).to receive(:send_odp_events).exactly(retry_count + 1).times.with(api_key, api_host, odp_events).and_return(true) |
264 | 264 | event_manager.start!(odp_config) |
265 | 265 |
|
266 | 266 | event_manager.send_event(**events[0]) |
267 | 267 | event_manager.send_event(**events[1]) |
268 | 268 | event_manager.flush |
269 | | - # Need to wait longer for retries with exponential backoff (200ms + 400ms = 600ms) |
270 | | - sleep(1) until event_manager.instance_variable_get('@event_queue').empty? |
| 269 | + sleep(0.1) until event_manager.instance_variable_get('@event_queue').empty? |
271 | 270 |
|
272 | 271 | expect(event_manager.instance_variable_get('@current_batch').length).to eq 0 |
273 | | - # Updated log message includes retry attempt and delay info |
274 | | - expect(spy_logger).to have_received(:log).with( |
275 | | - Logger::DEBUG, /Error dispatching ODP events, retrying/ |
276 | | - ).exactly(retry_count - 1).times |
| 272 | + expect(spy_logger).to have_received(:log).exactly(retry_count).times.with(Logger::DEBUG, 'Error dispatching ODP events, scheduled to retry.') |
277 | 273 | expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, "ODP event send failed (Failed after 3 retries: #{processed_events.to_json}).") |
278 | 274 |
|
279 | 275 | event_manager.stop! |
|
282 | 278 | it 'should retry on network failure' do |
283 | 279 | allow(SecureRandom).to receive(:uuid).and_return(test_uuid) |
284 | 280 | event_manager = Optimizely::OdpEventManager.new(logger: spy_logger) |
285 | | - allow(event_manager.api_manager).to receive(:send_odp_events).with(api_key, api_host, odp_events).and_return(true, true, false) |
| 281 | + allow(event_manager.api_manager).to receive(:send_odp_events).once.with(api_key, api_host, odp_events).and_return(true, true, false) |
286 | 282 | event_manager.start!(odp_config) |
287 | 283 |
|
288 | 284 | event_manager.send_event(**events[0]) |
289 | 285 | event_manager.send_event(**events[1]) |
290 | 286 | event_manager.flush |
291 | | - # Need to wait longer for retries with exponential backoff (200ms + 400ms = 600ms) |
292 | | - sleep(1) until event_manager.instance_variable_get('@event_queue').empty? |
| 287 | + sleep(0.1) until event_manager.instance_variable_get('@event_queue').empty? |
293 | 288 |
|
294 | 289 | expect(event_manager.instance_variable_get('@current_batch').length).to eq 0 |
295 | | - # Updated log message includes retry attempt and delay info |
296 | | - expect(spy_logger).to have_received(:log).with( |
297 | | - Logger::DEBUG, /Error dispatching ODP events, retrying/ |
298 | | - ).twice |
| 290 | + expect(spy_logger).to have_received(:log).twice.with(Logger::DEBUG, 'Error dispatching ODP events, scheduled to retry.') |
299 | 291 | expect(spy_logger).not_to have_received(:log).with(Logger::ERROR, anything) |
300 | 292 | expect(event_manager.running?).to be true |
301 | 293 | event_manager.stop! |
|
547 | 539 | expect(spy_logger).to have_received(:log).once.with(Logger::DEBUG, 'ODP event queue: cannot send before config has been set.') |
548 | 540 | expect(spy_logger).not_to have_received(:log).with(Logger::ERROR, anything) |
549 | 541 | end |
550 | | - |
551 | | - it 'should use exponential backoff between retries' do |
552 | | - allow(SecureRandom).to receive(:uuid).and_return(test_uuid) |
553 | | - event_manager = Optimizely::OdpEventManager.new(logger: spy_logger) |
554 | | - |
555 | | - # All requests fail to trigger retries |
556 | | - allow(event_manager.api_manager).to receive(:send_odp_events).with(api_key, api_host, odp_events).and_return(true) |
557 | | - event_manager.start!(odp_config) |
558 | | - |
559 | | - start_time = Time.now |
560 | | - event_manager.send_event(**events[0]) |
561 | | - event_manager.send_event(**events[1]) |
562 | | - event_manager.flush |
563 | | - |
564 | | - # Wait for all retries to complete (need at least 600ms for 200ms + 400ms delays) |
565 | | - sleep(1) until event_manager.instance_variable_get('@event_queue').empty? |
566 | | - elapsed_time = Time.now - start_time |
567 | | - |
568 | | - # Should have delays: 200ms + 400ms = 600ms minimum for 3 total attempts |
569 | | - expect(elapsed_time).to be >= 0.5 # Allow some tolerance |
570 | | - |
571 | | - # Should log retry attempts with delay info |
572 | | - expect(spy_logger).to have_received(:log).with( |
573 | | - Logger::DEBUG, /retrying \(attempt \d+ of \d+\) after/ |
574 | | - ).at_least(:once) |
575 | | - |
576 | | - event_manager.stop! |
577 | | - end |
578 | | - |
579 | | - it 'should calculate correct exponential backoff intervals' do |
580 | | - event_manager = Optimizely::OdpEventManager.new |
581 | | - |
582 | | - # First retry: 200ms |
583 | | - expect(event_manager.send(:calculate_retry_interval, 0)).to eq(0.2) |
584 | | - |
585 | | - # Second retry: 400ms |
586 | | - expect(event_manager.send(:calculate_retry_interval, 1)).to eq(0.4) |
587 | | - |
588 | | - # Third retry: 800ms |
589 | | - expect(event_manager.send(:calculate_retry_interval, 2)).to eq(0.8) |
590 | | - |
591 | | - # Fourth retry: capped at 1s |
592 | | - expect(event_manager.send(:calculate_retry_interval, 3)).to eq(1.0) |
593 | | - |
594 | | - # Fifth retry: still capped at 1s |
595 | | - expect(event_manager.send(:calculate_retry_interval, 4)).to eq(1.0) |
596 | | - end |
597 | 542 | end |
598 | 543 | end |
0 commit comments