fix: Execute event manager callbacks in asyncio thread (#5150)

Execute event manager callbacks in asyncio thread
This commit is contained in:
Christophe Bornet 2024-12-09 17:23:02 +01:00 committed by GitHub
commit b692ef7848
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 19 deletions

View file

@ -1,4 +1,5 @@
import asyncio
import time
from typing import Any
from unittest.mock import MagicMock
@ -17,6 +18,11 @@ async def create_event_queue():
return asyncio.Queue()
def blocking_cb(manager, event_type, data):
time.sleep(0.01)
manager.send_event(event_type=event_type, data=data)
class ComponentForTesting(Component):
"""Test component that implements basic functionality."""
@ -39,6 +45,8 @@ async def test_component_message_sending():
queue = await create_event_queue()
event_manager = EventManager(queue)
event_manager.register_event("on_message", "message", callback=blocking_cb)
# Create component
component = ComponentForTesting()
component.set_event_manager(event_manager)
@ -196,7 +204,8 @@ async def test_component_streaming_message():
"""Test component's streaming message functionality."""
queue = await create_event_queue()
event_manager = EventManager(queue)
event_manager.register_event("on_token", "token")
event_manager.register_event("on_token", "token", blocking_cb)
# Create a proper mock vertex with graph and flow_id
vertex = MagicMock()