yangdx commited on
Commit
3d0eaea
·
1 Parent(s): 301e403

Add error handling and improve logging for concurrent request testing

Browse files

• Added McpError and ErrorCode classes
• Added detailed error collection logic
• Improved error reporting & formatting
• Added request ID tracking
• Enhanced test results visibility

Files changed (1) hide show
  1. test_lightrag_ollama_chat.py +67 -13
test_lightrag_ollama_chat.py CHANGED
@@ -17,6 +17,19 @@ from typing import Dict, Any, Optional, List, Callable
17
  from dataclasses import dataclass, asdict
18
  from datetime import datetime
19
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  DEFAULT_CONFIG = {
22
  "server": {
@@ -641,35 +654,76 @@ def test_generate_concurrent() -> None:
641
  async with aiohttp.ClientSession() as session:
642
  yield session
643
 
644
- async def make_request(session, prompt: str):
645
  url = get_base_url("generate")
646
  data = create_generate_request_data(prompt, stream=False)
647
  try:
648
  async with session.post(url, json=data) as response:
649
  if response.status != 200:
650
- response.raise_for_status()
651
- return await response.json()
 
 
 
 
 
 
 
 
 
652
  except Exception as e:
653
- return {"error": str(e)}
 
 
 
654
 
655
  async def run_concurrent_requests():
656
  prompts = ["第一个问题", "第二个问题", "第三个问题", "第四个问题", "第五个问题"]
657
-
658
  async with get_session() as session:
659
- tasks = [make_request(session, prompt) for prompt in prompts]
660
- results = await asyncio.gather(*tasks)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661
  return results
662
 
663
  if OutputControl.is_verbose():
664
  print("\n=== Testing concurrent generate requests ===")
665
 
666
  # Run concurrent requests
667
- results = asyncio.run(run_concurrent_requests())
668
-
669
- # Print results
670
- for i, result in enumerate(results, 1):
671
- print(f"\nRequest {i} result:")
672
- print_json_response(result)
 
 
 
673
 
674
 
675
  def get_test_cases() -> Dict[str, Callable]:
 
17
  from dataclasses import dataclass, asdict
18
  from datetime import datetime
19
  from pathlib import Path
20
+ from enum import Enum, auto
21
+
22
+ class ErrorCode(Enum):
23
+ """Error codes for MCP errors"""
24
+ InvalidRequest = auto()
25
+ InternalError = auto()
26
+
27
+ class McpError(Exception):
28
+ """Base exception class for MCP errors"""
29
+ def __init__(self, code: ErrorCode, message: str):
30
+ self.code = code
31
+ self.message = message
32
+ super().__init__(message)
33
 
34
  DEFAULT_CONFIG = {
35
  "server": {
 
654
  async with aiohttp.ClientSession() as session:
655
  yield session
656
 
657
+ async def make_request(session, prompt: str, request_id: int):
658
  url = get_base_url("generate")
659
  data = create_generate_request_data(prompt, stream=False)
660
  try:
661
  async with session.post(url, json=data) as response:
662
  if response.status != 200:
663
+ error_msg = f"Request {request_id} failed with status {response.status}"
664
+ if OutputControl.is_verbose():
665
+ print(f"\n{error_msg}")
666
+ raise McpError(ErrorCode.InternalError, error_msg)
667
+ result = await response.json()
668
+ if "error" in result:
669
+ error_msg = f"Request {request_id} returned error: {result['error']}"
670
+ if OutputControl.is_verbose():
671
+ print(f"\n{error_msg}")
672
+ raise McpError(ErrorCode.InternalError, error_msg)
673
+ return result
674
  except Exception as e:
675
+ error_msg = f"Request {request_id} failed: {str(e)}"
676
+ if OutputControl.is_verbose():
677
+ print(f"\n{error_msg}")
678
+ raise McpError(ErrorCode.InternalError, error_msg)
679
 
680
  async def run_concurrent_requests():
681
  prompts = ["第一个问题", "第二个问题", "第三个问题", "第四个问题", "第五个问题"]
682
+
683
  async with get_session() as session:
684
+ tasks = [make_request(session, prompt, i+1) for i, prompt in enumerate(prompts)]
685
+ results = await asyncio.gather(*tasks, return_exceptions=True)
686
+
687
+ # 收集成功和失败的结果
688
+ success_results = []
689
+ error_messages = []
690
+
691
+ for i, result in enumerate(results):
692
+ if isinstance(result, Exception):
693
+ error_messages.append(f"Request {i+1} failed: {str(result)}")
694
+ else:
695
+ success_results.append((i+1, result))
696
+
697
+ # 如果有任何错误,在打印完所有结果后抛出异常
698
+ if error_messages:
699
+ # 先打印成功的结果
700
+ for req_id, result in success_results:
701
+ if OutputControl.is_verbose():
702
+ print(f"\nRequest {req_id} succeeded:")
703
+ print_json_response(result)
704
+
705
+ # 打印所有错误信息
706
+ error_summary = "\n".join(error_messages)
707
+ raise McpError(
708
+ ErrorCode.InternalError,
709
+ f"Some concurrent requests failed:\n{error_summary}"
710
+ )
711
+
712
  return results
713
 
714
  if OutputControl.is_verbose():
715
  print("\n=== Testing concurrent generate requests ===")
716
 
717
  # Run concurrent requests
718
+ try:
719
+ results = asyncio.run(run_concurrent_requests())
720
+ # 如果没有异常,打印所有成功的结果
721
+ for i, result in enumerate(results, 1):
722
+ print(f"\nRequest {i} result:")
723
+ print_json_response(result)
724
+ except McpError as e:
725
+ # 错误信息已经在之前打印过了,这里直接抛出
726
+ raise
727
 
728
 
729
  def get_test_cases() -> Dict[str, Callable]: