mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
fix(backend): use run_coroutine_threadsafe for conversation update callbacks (#13134)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from datetime import UTC, datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
@@ -169,15 +169,14 @@ async def test_exists(session_maker):
|
||||
class TestGetInstance:
|
||||
"""Tests for SaasConversationStore.get_instance method.
|
||||
|
||||
The get_instance method uses sync UserStore.get_user_by_id (not async)
|
||||
because it can be called from call_async_from_sync contexts which create
|
||||
a new event loop. Using async DB operations in that context would cause
|
||||
asyncpg connection errors.
|
||||
The get_instance method uses async UserStore.get_user_by_id_async because
|
||||
callers now use asyncio.run_coroutine_threadsafe() to dispatch to the main
|
||||
event loop where asyncpg connections work properly.
|
||||
"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_instance_uses_sync_get_user_by_id(self):
|
||||
"""Verify get_instance calls the sync get_user_by_id, not async version."""
|
||||
async def test_get_instance_uses_async_get_user_by_id(self):
|
||||
"""Verify get_instance calls the async get_user_by_id_async for proper event loop handling."""
|
||||
# Arrange
|
||||
user_id = '5594c7b6-f959-4b81-92e9-b09c206f5081'
|
||||
mock_user = MagicMock(spec=User)
|
||||
@@ -185,14 +184,16 @@ class TestGetInstance:
|
||||
mock_config = MagicMock(spec=OpenHandsConfig)
|
||||
|
||||
with patch(
|
||||
'storage.saas_conversation_store.UserStore.get_user_by_id',
|
||||
return_value=mock_user,
|
||||
) as mock_sync_get_user, patch('storage.saas_conversation_store.session_maker'):
|
||||
'storage.saas_conversation_store.UserStore.get_user_by_id_async',
|
||||
AsyncMock(return_value=mock_user),
|
||||
) as mock_async_get_user, patch(
|
||||
'storage.saas_conversation_store.session_maker'
|
||||
):
|
||||
# Act
|
||||
store = await SaasConversationStore.get_instance(mock_config, user_id)
|
||||
|
||||
# Assert
|
||||
mock_sync_get_user.assert_called_once_with(user_id)
|
||||
mock_async_get_user.assert_called_once_with(user_id)
|
||||
assert store.user_id == user_id
|
||||
assert store.org_id == mock_user.current_org_id
|
||||
|
||||
@@ -204,8 +205,8 @@ class TestGetInstance:
|
||||
mock_config = MagicMock(spec=OpenHandsConfig)
|
||||
|
||||
with patch(
|
||||
'storage.saas_conversation_store.UserStore.get_user_by_id',
|
||||
return_value=None,
|
||||
'storage.saas_conversation_store.UserStore.get_user_by_id_async',
|
||||
AsyncMock(return_value=None),
|
||||
), patch('storage.saas_conversation_store.session_maker'):
|
||||
# Act
|
||||
store = await SaasConversationStore.get_instance(mock_config, user_id)
|
||||
|
||||
Reference in New Issue
Block a user