fix(agent): stabilize live e2e tool execution and loop isolation

This commit is contained in:
zl-q
2026-03-08 22:41:59 +08:00
parent 14508c52f6
commit 2980213a5b
32 changed files with 3076 additions and 560 deletions
@@ -1,5 +1,7 @@
from __future__ import annotations
import asyncio
import pytest
from core.config.settings import RedisSettings
@@ -107,7 +109,9 @@ async def test_get_or_init_redis_client_initializes_when_needed(
async def _fake_initialize() -> bool:
return True
monkeypatch.setattr(type(redis_service), "is_initialized", property(lambda _: False))
monkeypatch.setattr(
type(redis_service), "is_initialized", property(lambda _: False)
)
monkeypatch.setattr(redis_service, "initialize", _fake_initialize)
monkeypatch.setattr(redis_service, "get_client", lambda: fake_client)
@@ -123,8 +127,40 @@ async def test_get_or_init_redis_client_raises_when_init_fails(
async def _fake_initialize() -> bool:
return False
monkeypatch.setattr(type(redis_service), "is_initialized", property(lambda _: False))
monkeypatch.setattr(
type(redis_service), "is_initialized", property(lambda _: False)
)
monkeypatch.setattr(redis_service, "initialize", _fake_initialize)
with pytest.raises(RuntimeError, match="Redis service initialization failed"):
await get_or_init_redis_client()
@pytest.mark.asyncio
async def test_get_or_init_redis_client_reinitializes_when_event_loop_changes(
monkeypatch: pytest.MonkeyPatch,
) -> None:
stale_client = _FakeRedisClient()
fresh_client = _FakeRedisClient()
call_count = {"initialize": 0}
async def _fake_initialize() -> bool:
call_count["initialize"] += 1
return True
class _Loop:
pass
loop_obj = _Loop()
monkeypatch.setattr(asyncio, "get_running_loop", lambda: loop_obj)
monkeypatch.setattr(redis_service, "initialize", _fake_initialize)
monkeypatch.setattr(redis_service, "get_client", lambda: fresh_client)
monkeypatch.setattr(redis_service, "_client", stale_client, raising=False)
monkeypatch.setattr(redis_service, "_loop_id", 123, raising=False)
monkeypatch.setattr(redis_service, "_initialized", True, raising=False)
client = await get_or_init_redis_client()
assert call_count["initialize"] == 1
assert client is fresh_client