|
| 1 | +"""Test Semantic Scholar with timeout controls""" |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +# Add project root to path |
| 7 | +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 8 | + |
| 9 | +# Load environment variables |
| 10 | +from dotenv import load_dotenv |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +from tools_impl.mcp_research_tools import get_mcp_research_tools |
| 14 | + |
| 15 | +async def test_with_timeout(timeout_seconds: int): |
| 16 | + """Test Semantic Scholar with a specific timeout""" |
| 17 | + print(f"\n{'='*60}") |
| 18 | + print(f"Testing with {timeout_seconds} second timeout") |
| 19 | + print(f"{'='*60}\n") |
| 20 | + |
| 21 | + try: |
| 22 | + # Initialize MCP tools |
| 23 | + print("Initializing MCP tools...") |
| 24 | + mcp_tools = await get_mcp_research_tools() |
| 25 | + |
| 26 | + # Get Semantic Scholar tool |
| 27 | + semantic_tool = mcp_tools.get_tool("semantic_scholar") |
| 28 | + if not semantic_tool: |
| 29 | + print("❌ Semantic Scholar tool not found!") |
| 30 | + print(f"Available tools: {mcp_tools.get_available_tools()}") |
| 31 | + return False |
| 32 | + |
| 33 | + print(f"✅ Semantic Scholar tool found: {semantic_tool.name}") |
| 34 | + print(f"Description: {semantic_tool.description}\n") |
| 35 | + |
| 36 | + # Test with timeout |
| 37 | + print(f"Executing search with {timeout_seconds}s timeout...") |
| 38 | + print(f"Query: 'transformer models natural language processing'") |
| 39 | + print(f"Num results: 5\n") |
| 40 | + |
| 41 | + start_time = asyncio.get_event_loop().time() |
| 42 | + |
| 43 | + try: |
| 44 | + # Run with timeout |
| 45 | + result = await asyncio.wait_for( |
| 46 | + semantic_tool.ainvoke({ |
| 47 | + "query": "transformer models natural language processing", |
| 48 | + "num_results": 5 |
| 49 | + }), |
| 50 | + timeout=timeout_seconds |
| 51 | + ) |
| 52 | + |
| 53 | + elapsed = asyncio.get_event_loop().time() - start_time |
| 54 | + print(f"\n✅ Completed in {elapsed:.2f} seconds") |
| 55 | + print(f"Results returned: {len(result) if isinstance(result, list) else 'N/A'}") |
| 56 | + return True |
| 57 | + |
| 58 | + except asyncio.TimeoutError: |
| 59 | + elapsed = asyncio.get_event_loop().time() - start_time |
| 60 | + print(f"\n⏱️ TIMEOUT after {elapsed:.2f} seconds") |
| 61 | + print(f"Expected timeout: {timeout_seconds}s") |
| 62 | + return False |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + print(f"\n❌ Error: {e}") |
| 66 | + import traceback |
| 67 | + print(traceback.format_exc()) |
| 68 | + return False |
| 69 | + |
| 70 | +async def main(): |
| 71 | + """Run timeout tests""" |
| 72 | + print("\n" + "="*60) |
| 73 | + print("SEMANTIC SCHOLAR TIMEOUT TEST") |
| 74 | + print("="*60) |
| 75 | + |
| 76 | + # Test 1: 30 second timeout (should fail with rate limiting) |
| 77 | + print("\nTest 1: 30 second timeout (likely to hit rate limits)") |
| 78 | + result_30s = await test_with_timeout(30) |
| 79 | + |
| 80 | + # Wait a bit between tests |
| 81 | + await asyncio.sleep(2) |
| 82 | + |
| 83 | + # Test 2: 120 second timeout (should complete despite rate limiting) |
| 84 | + print("\n\nTest 2: 120 second timeout (should handle rate limits)") |
| 85 | + result_120s = await test_with_timeout(120) |
| 86 | + |
| 87 | + # Summary |
| 88 | + print("\n" + "="*60) |
| 89 | + print("TEST SUMMARY") |
| 90 | + print("="*60) |
| 91 | + print(f"30s timeout: {'PASSED' if result_30s else 'FAILED (EXPECTED)'}") |
| 92 | + print(f"120s timeout: {'PASSED' if result_120s else 'FAILED'}") |
| 93 | + print("\nRecommendation: Use 120s timeout for Semantic Scholar to handle rate limiting") |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + asyncio.run(main()) |
0 commit comments