Fix flaky timeout test in test_88_random_error (#1525)

This commit is contained in:
Max Isbey 2025-10-28 21:45:25 +00:00 • committed by GitHub
commit c44e68f766
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,6 +27,10 @@ async def test_notification_validation_error(tmp_path: Path):
2. The server can still handle new requests 2. The server can still handle new requests
3. The client can make new requests 3. The client can make new requests
4. No resources are leaked 4. No resources are leaked
Uses per-request timeouts to avoid race conditions:
- Fast operations use no timeout (reliable in any environment)
- Slow operations use minimal timeout (10ms) for quick test execution
""" """
server = Server(name="test") server = Server(name="test")
@ -79,29 +83,29 @@ async def test_notification_validation_error(tmp_path: Path):
write_stream: MemoryObjectSendStream[SessionMessage], write_stream: MemoryObjectSendStream[SessionMessage],
scope: anyio.CancelScope, scope: anyio.CancelScope,
): ):
# Use a timeout that's: # No session-level timeout to avoid race conditions with fast operations
# - Long enough for fast operations (>10ms) async with ClientSession(read_stream, write_stream) as session:
# - Short enough for slow operations (<200ms)
# - Not too short to avoid flakiness
async with ClientSession(read_stream, write_stream, read_timeout_seconds=timedelta(milliseconds=50)) as session:
await session.initialize() await session.initialize()
# First call should work (fast operation) # First call should work (fast operation, no timeout)
result = await session.call_tool("fast") result = await session.call_tool("fast", read_timeout_seconds=None)
assert result.content == [TextContent(type="text", text="fast 1")] assert result.content == [TextContent(type="text", text="fast 1")]
assert not slow_request_lock.is_set() assert not slow_request_lock.is_set()
# Second call should timeout (slow operation) # Second call should timeout (slow operation with minimal timeout)
# Use 10ms timeout to trigger quickly without waiting
with pytest.raises(McpError) as exc_info: with pytest.raises(McpError) as exc_info:
await session.call_tool("slow") await session.call_tool(
"slow", read_timeout_seconds=timedelta(microseconds=1)
) # artificial timeout that always fails
assert "Timed out while waiting" in str(exc_info.value) assert "Timed out while waiting" in str(exc_info.value)
# release the slow request not to have hanging process # release the slow request not to have hanging process
slow_request_lock.set() slow_request_lock.set()
# Third call should work (fast operation), # Third call should work (fast operation, no timeout),
# proving server is still responsive # proving server is still responsive
result = await session.call_tool("fast") result = await session.call_tool("fast", read_timeout_seconds=None)
assert result.content == [TextContent(type="text", text="fast 3")] assert result.content == [TextContent(type="text", text="fast 3")]
scope.cancel() scope.cancel()