Skip to content

Commit 0c83486

Browse files
committed
feat: Add flush_and_wait to DefaultAsyncEventProcessor
Triggers a flush and awaits delivery via a new inbox message, returning whether it completed within the timeout.
1 parent c7a025c commit 0c83486

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

ldclient/impl/events/async_event_processor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ async def _run_main_loop(self):
132132
self._context_keys.clear()
133133
elif message.type == 'diagnostic':
134134
self._send_and_reset_diagnostics()
135+
elif message.type == 'flush_and_wait':
136+
self._trigger_flush()
137+
await self._flush_workers.wait()
138+
message.param.set()
135139
elif message.type == 'test_sync':
136140
await self._flush_workers.wait()
137141
if self._diagnostic_flush_workers is not None:
@@ -204,6 +208,13 @@ def send_event(self, event: EventInput):
204208
def flush(self):
205209
self._post_to_inbox(EventProcessorMessage('flush', None))
206210

211+
async def flush_and_wait(self, timeout: float) -> bool:
212+
try:
213+
await asyncio.wait_for(self._post_message_and_wait('flush_and_wait'), timeout)
214+
return True
215+
except asyncio.TimeoutError:
216+
return False
217+
207218
async def stop(self):
208219
async with self._close_lock:
209220
if self._closed:

ldclient/testing/impl/events/test_async_event_processor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@ async def test_custom_event_is_queued():
174174
assert output[1]['metricValue'] == 1.5
175175

176176

177+
# ---------------------------------------------------------------------------
178+
# flush_and_wait
179+
# ---------------------------------------------------------------------------
180+
181+
async def test_flush_and_wait_delivers_events_and_returns_true():
182+
mock_http = MockAioHttp()
183+
async with make_processor(mock_http) as ep:
184+
ep.send_event(EventInputIdentify(timestamp, context))
185+
186+
delivered = await ep.flush_and_wait(5)
187+
188+
assert delivered is True
189+
assert mock_http.request_data is not None
190+
output = json.loads(mock_http.request_data)
191+
assert len(output) == 1
192+
assert output[0]['kind'] == 'identify'
193+
194+
195+
async def test_flush_and_wait_returns_false_on_timeout():
196+
mock_http = MockAioHttp()
197+
async with make_processor(mock_http) as ep:
198+
ep.send_event(EventInputIdentify(timestamp, context))
199+
200+
# A zero timeout can't complete the flush round-trip, so it reports False.
201+
delivered = await ep.flush_and_wait(0)
202+
203+
assert delivered is False
204+
205+
177206
async def test_two_events_for_same_context_only_produce_one_index_event():
178207
mock_http = MockAioHttp()
179208
async with make_processor(mock_http) as ep:

0 commit comments

Comments
 (0)