Files
ZNJJ-api-server/test/storage/crash_worker.py
T

44 lines
1.6 KiB
Python
Raw Normal View History

"""Integration-test child: exit abruptly at a durable turn's commit boundary."""
import asyncio
import os
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.agent.graph import build_graph
from src.backends.chat import ChatInput
from src.backends.langgraph import LangGraphBackend
from src.services.app_service import AccidentAppService
from src.storage.runtime import StorageSettings, open_storage
class LLM:
async def complete(self, system, user, *, history=None):
if '【字段提取】' in system:
return '{"has_injury":false}'
return '<state>1002</state>crash-boundary-reply'
async def main():
kind, path, session, boundary = sys.argv[1:]
settings = StorageSettings(kind=kind, sqlite_path=path,
postgres_dsn=os.getenv('AGENT_TEST_POSTGRES_DSN',''), pool_size=2)
async with open_storage(settings) as storage:
service = AccidentAppService(LangGraphBackend(build_graph(storage.checkpointer), llm=LLM()),
storage.repository, locks=storage.locks)
original = storage.repository.commit_turn
async def crash(*args):
if boundary=='before':
os._exit(23)
await original(*args)
os._exit(24)
storage.repository.commit_turn = crash
await service.chat(ChatInput(session,'继续办理',request_key='crash-key'))
if __name__ == '__main__':
factory=asyncio.SelectorEventLoop if os.name=='nt' else None
with asyncio.Runner(loop_factory=factory) as runner:
runner.run(main())