|
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 |
|
|
239 | 239 | event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher)
|
240 | 240 | expect(event_processor.flush_interval).to eq(30_000)
|
241 | 241 | event_processor.stop!
|
242 |
| - event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: 'test') |
| 242 | + event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: 'test', logger: spy_logger) |
243 | 243 | expect(event_processor.flush_interval).to eq(30_000)
|
244 | 244 | event_processor.stop!
|
245 |
| - event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: []) |
| 245 | + event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: [], logger: spy_logger) |
246 | 246 | expect(event_processor.flush_interval).to eq(30_000)
|
247 | 247 | event_processor.stop!
|
248 |
| - event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: 0) |
| 248 | + event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: 0, logger: spy_logger) |
249 | 249 | expect(event_processor.flush_interval).to eq(30_000)
|
250 | 250 | event_processor.stop!
|
251 |
| - event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: -5) |
| 251 | + event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: @event_dispatcher, flush_interval: -5, logger: spy_logger) |
252 | 252 | expect(event_processor.flush_interval).to eq(30_000)
|
253 | 253 | event_processor.stop!
|
| 254 | + expect(spy_logger).to have_received(:log).with(Logger::DEBUG, 'Setting to default flush_interval: 30000 ms.').exactly(4).times |
254 | 255 | end
|
255 | 256 |
|
256 | 257 | it 'should set flush interval when provided valid' do
|
|
295 | 296 | "Error dispatching event: #{log_event} Timeout::Error."
|
296 | 297 | )
|
297 | 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 |
298 | 329 | end
|
0 commit comments