MCPcopy Create free account
hub / github.com/IBM/mcp-cli / test_stream_execute_tools_cancellation

Function test_stream_execute_tools_cancellation

tests/tools/test_execution.py:409–444  ·  view source on GitHub ↗

Test stream_execute_tools handles cancellation gracefully.

()

Source from the content-addressed store, hash-verified

407
408@pytest.mark.asyncio
409async def test_stream_execute_tools_cancellation():
410 """Test stream_execute_tools handles cancellation gracefully."""
411 manager = MockToolManager()
412
413 # Create a slow tool that we can cancel
414 async def slow_execute(self, tool_name, arguments, namespace=None, timeout=None):
415 await asyncio.sleep(10) # Long delay
416 return ToolCallResult(tool_name=tool_name, success=True, result={})
417
418 manager.execute_tool = lambda *args, **kwargs: slow_execute(
419 manager, *args, **kwargs
420 )
421
422 calls = [
423 CTPToolCall(id="call_1", tool="slow_tool_1", arguments={}),
424 CTPToolCall(id="call_2", tool="slow_tool_2", arguments={}),
425 ]
426
427 results = []
428
429 async def collect_with_cancel():
430 async for result in stream_execute_tools(manager, calls):
431 results.append(result)
432 # Cancel after receiving anything (but we won't receive anything since they're slow)
433
434 task = asyncio.create_task(collect_with_cancel())
435 await asyncio.sleep(0.1) # Let tasks start
436 task.cancel()
437
438 try:
439 await task
440 except asyncio.CancelledError:
441 pass
442
443 # Results should be empty since we cancelled before any completed
444 assert len(results) == 0
445
446
447@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 3

slow_executeFunction · 0.85
collect_with_cancelFunction · 0.85
MockToolManagerClass · 0.70

Tested by

no test coverage detected