|
110 | 110 | expected_batch.pop # Removes 11th element |
111 | 111 | expect(@event_processor.current_batch.size).to be 10 |
112 | 112 |
|
113 | | - expect(Optimizely::EventFactory).to have_received(:create_log_event).with(expected_batch, spy_logger).once |
| 113 | + expect(Optimizely::EventFactory).to have_received(:create_log_event).with(expected_batch, spy_logger).twice |
114 | 114 | expect(@event_dispatcher).to have_received(:dispatch_event).with( |
115 | 115 | Optimizely::EventFactory.create_log_event(expected_batch, spy_logger) |
116 | | - ).once |
| 116 | + ).twice |
117 | 117 | expect(spy_logger).to have_received(:log).with(Logger::DEBUG, 'Flushing on max batch size!').once |
118 | 118 | end |
119 | 119 |
|
|
296 | 296 | "Error dispatching event: #{log_event} Timeout::Error." |
297 | 297 | ) |
298 | 298 | end |
| 299 | + |
| 300 | + it 'should flush pending events when stop is called' do |
| 301 | + allow(Optimizely::EventFactory).to receive(:create_log_event).with(any_args) |
| 302 | + expected_batch = [] |
| 303 | + counter = 0 |
| 304 | + until counter >= 10 |
| 305 | + event['key'] = event['key'] + counter.to_s |
| 306 | + user_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, nil) |
| 307 | + expected_batch << user_event |
| 308 | + @event_processor.process(user_event) |
| 309 | + counter += 1 |
| 310 | + end |
| 311 | + |
| 312 | + sleep 0.25 |
| 313 | + |
| 314 | + # max batch size not occurred and batch is not dispatched. |
| 315 | + expect(@event_processor.current_batch.size).to be < 10 |
| 316 | + expect(@event_dispatcher).not_to have_received(:dispatch_event) |
| 317 | + |
| 318 | + # Stop should flush the queue! |
| 319 | + @event_processor.stop! |
| 320 | + sleep 0.75 |
| 321 | + |
| 322 | + expect(spy_logger).to have_received(:log).with(Logger::INFO, 'Exiting processing loop. Attempting to flush pending events.') |
| 323 | + expect(@event_dispatcher).to have_received(:dispatch_event).with( |
| 324 | + Optimizely::EventFactory.create_log_event(expected_batch, spy_logger) |
| 325 | + ) |
| 326 | + |
| 327 | + expect(spy_logger).not_to have_received(:log).with(Logger::DEBUG, 'Flushing on max batch size!') |
| 328 | + end |
299 | 329 | end |
0 commit comments