|
1 | 1 | from contextlib import AsyncExitStack |
2 | 2 | from unittest.mock import AsyncMock, Mock |
3 | 3 |
|
| 4 | +import anyio |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from mcp import types |
@@ -124,6 +125,84 @@ async def test_call(call: types.CallToolRequest) -> types.ServerResult: |
124 | 125 | ) |
125 | 126 |
|
126 | 127 |
|
| 128 | +@pytest.mark.anyio |
| 129 | +async def test_async_cancel_in_progress(): |
| 130 | + """Tests basic async call""" |
| 131 | + |
| 132 | + async def slow_call(call: types.CallToolRequest) -> types.ServerResult: |
| 133 | + with anyio.move_on_after(10) as scope: |
| 134 | + await anyio.sleep(10) |
| 135 | + |
| 136 | + if scope.cancel_called: |
| 137 | + return types.ServerResult( |
| 138 | + types.CallToolResult( |
| 139 | + content=[ |
| 140 | + types.TextContent(type="text", text="should be discarded") |
| 141 | + ], |
| 142 | + isError=True, |
| 143 | + ) |
| 144 | + ) |
| 145 | + else: |
| 146 | + return types.ServerResult( |
| 147 | + types.CallToolResult( |
| 148 | + content=[types.TextContent(type="text", text="test")] |
| 149 | + ) |
| 150 | + ) |
| 151 | + |
| 152 | + async_call = types.CallToolAsyncRequest( |
| 153 | + method="tools/async/call", params=types.CallToolAsyncRequestParams(name="test") |
| 154 | + ) |
| 155 | + |
| 156 | + mock_session_1 = AsyncMock() |
| 157 | + mock_context_1 = Mock() |
| 158 | + mock_context_1.session = mock_session_1 |
| 159 | + |
| 160 | + result_cache = ResultCache(max_size=1, max_keep_alive=1) |
| 161 | + async with AsyncExitStack() as stack: |
| 162 | + await stack.enter_async_context(result_cache) |
| 163 | + async_call_ref = await result_cache.start_call( |
| 164 | + slow_call, async_call, mock_context_1 |
| 165 | + ) |
| 166 | + assert async_call_ref.token is not None |
| 167 | + |
| 168 | + await result_cache.cancel( |
| 169 | + notification=types.CancelToolAsyncNotification( |
| 170 | + method="tools/async/cancel", |
| 171 | + params=types.CancelToolAsyncNotificationParams( |
| 172 | + token=async_call_ref.token |
| 173 | + ), |
| 174 | + ), |
| 175 | + ) |
| 176 | + |
| 177 | + assert async_call_ref.token is not None |
| 178 | + await result_cache.notification_hook( |
| 179 | + session=mock_session_1, |
| 180 | + notification=types.ServerNotification( |
| 181 | + types.ProgressNotification( |
| 182 | + method="notifications/progress", |
| 183 | + params=types.ProgressNotificationParams( |
| 184 | + progressToken="test", progress=1 |
| 185 | + ), |
| 186 | + ) |
| 187 | + ), |
| 188 | + ) |
| 189 | + |
| 190 | + result = await result_cache.get_result( |
| 191 | + types.GetToolAsyncResultRequest( |
| 192 | + method="tools/async/get", |
| 193 | + params=types.GetToolAsyncResultRequestParams( |
| 194 | + token=async_call_ref.token |
| 195 | + ), |
| 196 | + ) |
| 197 | + ) |
| 198 | + |
| 199 | + assert result.isError |
| 200 | + assert not result.isPending |
| 201 | + assert len(result.content) == 1 |
| 202 | + assert type(result.content[0]) is types.TextContent |
| 203 | + assert result.content[0].text == "cancelled" |
| 204 | + |
| 205 | + |
127 | 206 | @pytest.mark.anyio |
128 | 207 | async def test_async_call_keep_alive(): |
129 | 208 | """Tests async call keep alive""" |
|
0 commit comments