MCPcopy Create free account
hub / github.com/reflex-dev/reflex / event_processing_harness

Function event_processing_harness

tests/benchmarks/test_event_processing.py:28–99  ·  view source on GitHub ↗

Set up the full event processing pipeline for benchmarking. Creates a ``BaseStateEventProcessor`` wired to a real ``StateManagerMemory`` with mock emit callbacks. Events are enqueued directly and deltas are collected via the emit callback. Yields: An async callable that en

()

Source from the content-addressed store, hash-verified

26
27@pytest_asyncio.fixture
28async def event_processing_harness():
29 """Set up the full event processing pipeline for benchmarking.
30
31 Creates a ``BaseStateEventProcessor`` wired to a real
32 ``StateManagerMemory`` with mock emit callbacks. Events are
33 enqueued directly and deltas are collected via the emit callback.
34
35 Yields:
36 An async callable that enqueues the given number of events
37 and waits for all expected deltas.
38 """
39 emitted_deltas: list[tuple[str, Mapping[str, Mapping[str, Any]]]] = []
40
41 async def emit_delta_impl( # noqa: RUF029
42 token: str, delta: Mapping[str, Mapping[str, Any]]
43 ) -> None:
44 emitted_deltas.append((token, delta))
45
46 async def emit_event_impl(token: str, *events: Event) -> None:
47 pass
48
49 def handle_backend_exception(ex: Exception) -> None:
50 formatted_exc = "\n".join(traceback.format_exception(ex))
51 pytest.fail(f"Event processor raised an unexpected exception:\n{formatted_exc}")
52
53 processor = BaseStateEventProcessor(
54 backend_exception_handler=handle_backend_exception,
55 graceful_shutdown_timeout=5,
56 )
57 # Mock _rehydrate so the processor doesn't try to push full state
58 # to a non-existent frontend on the first event.
59 with mock.patch.object(processor, "_rehydrate", new=mock.AsyncMock()):
60 state_manager = StateManagerMemory()
61 root_context = EventContext(
62 token="",
63 state_manager=state_manager,
64 enqueue_impl=processor.enqueue_many,
65 emit_delta_impl=emit_delta_impl,
66 emit_event_impl=emit_event_impl,
67 )
68 processor._root_context = root_context
69
70 token = "benchmark-token"
71 handler_name = format_event_handler(BenchmarkState.event_handlers["increment"])
72 event = Event(
73 name=handler_name,
74 router_data={
75 "query": {},
76 "path": "/",
77 },
78 payload={},
79 )
80
81 async def run_events(num_events: int, num_expected_deltas: int) -> None:
82 """Enqueue events and wait for all deltas to be emitted.
83
84 Args:
85 num_events: Number of increment events to enqueue.

Callers

nothing calls this directly

Calls 6

closeMethod · 0.95
StateManagerMemoryClass · 0.90
EventContextClass · 0.90
format_event_handlerFunction · 0.90
EventClass · 0.90

Tested by

no test coverage detected