Skip to content

Commit 0aa4a7e

Browse files
committed
fix: flush queue on close.
1 parent 8991c18 commit 0aa4a7e

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/optimizely/event/batch_event_processor.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ def run
151151

152152
add_to_batch(item) if item.is_a? Optimizely::UserEvent
153153
end
154+
rescue SignalException
155+
@logger.log(Logger::INFO, 'Interrupted while processing buffer.')
156+
rescue Exception => e
157+
@logger.log(Logger::ERROR, "Uncaught exception processing buffer. #{e.message}")
158+
ensure
159+
@logger.log(
160+
Logger::INFO,
161+
'Exiting processing loop. Attempting to flush pending events.'
162+
)
163+
flush_queue!
154164
end
155165

156166
def flush_queue!

spec/event/batch_event_processor_spec.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@
110110
expected_batch.pop # Removes 11th element
111111
expect(@event_processor.current_batch.size).to be 10
112112

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
114114
expect(@event_dispatcher).to have_received(:dispatch_event).with(
115115
Optimizely::EventFactory.create_log_event(expected_batch, spy_logger)
116-
).once
116+
).twice
117117
expect(spy_logger).to have_received(:log).with(Logger::DEBUG, 'Flushing on max batch size!').once
118118
end
119119

@@ -296,4 +296,34 @@
296296
"Error dispatching event: #{log_event} Timeout::Error."
297297
)
298298
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
299329
end

0 commit comments

Comments
 (0)