|
| 1 | +"""Tests for exception group collapsing utilities.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import anyio |
| 6 | + |
| 7 | +from mcp.shared._exception_utils import collapse_exception_group, create_task_group |
| 8 | + |
| 9 | + |
| 10 | +class TestCollapseExceptionGroup: |
| 11 | + """Tests for the collapse_exception_group function.""" |
| 12 | + |
| 13 | + @pytest.mark.anyio |
| 14 | + async def test_single_real_error_with_cancelled(self) -> None: |
| 15 | + """A single real error alongside Cancelled exceptions should be extracted.""" |
| 16 | + real_error = RuntimeError("connection failed") |
| 17 | + cancelled = anyio.get_cancelled_exc_class()() |
| 18 | + |
| 19 | + group = BaseExceptionGroup("test", [real_error, cancelled]) |
| 20 | + result = collapse_exception_group(group) |
| 21 | + |
| 22 | + assert result is real_error |
| 23 | + |
| 24 | + @pytest.mark.anyio |
| 25 | + async def test_single_real_error_only(self) -> None: |
| 26 | + """A single real error without Cancelled should be extracted.""" |
| 27 | + real_error = ValueError("bad value") |
| 28 | + |
| 29 | + group = BaseExceptionGroup("test", [real_error]) |
| 30 | + result = collapse_exception_group(group) |
| 31 | + |
| 32 | + assert result is real_error |
| 33 | + |
| 34 | + @pytest.mark.anyio |
| 35 | + async def test_multiple_real_errors_preserved(self) -> None: |
| 36 | + """Multiple non-cancellation errors should keep the group intact.""" |
| 37 | + err1 = RuntimeError("first") |
| 38 | + err2 = ValueError("second") |
| 39 | + |
| 40 | + group = BaseExceptionGroup("test", [err1, err2]) |
| 41 | + result = collapse_exception_group(group) |
| 42 | + |
| 43 | + assert result is group |
| 44 | + |
| 45 | + @pytest.mark.anyio |
| 46 | + async def test_all_cancelled_preserved(self) -> None: |
| 47 | + """All-cancelled groups should be returned as-is.""" |
| 48 | + cancelled_class = anyio.get_cancelled_exc_class() |
| 49 | + group = BaseExceptionGroup("test", [cancelled_class(), cancelled_class()]) |
| 50 | + result = collapse_exception_group(group) |
| 51 | + |
| 52 | + assert result is group |
| 53 | + |
| 54 | + @pytest.mark.anyio |
| 55 | + async def test_multiple_cancelled_one_real(self) -> None: |
| 56 | + """One real error with multiple Cancelled should extract the real error.""" |
| 57 | + cancelled_class = anyio.get_cancelled_exc_class() |
| 58 | + real_error = ConnectionError("lost connection") |
| 59 | + |
| 60 | + group = BaseExceptionGroup( |
| 61 | + "test", [cancelled_class(), real_error, cancelled_class()] |
| 62 | + ) |
| 63 | + result = collapse_exception_group(group) |
| 64 | + |
| 65 | + assert result is real_error |
| 66 | + |
| 67 | + |
| 68 | +class TestCreateTaskGroup: |
| 69 | + """Tests for the create_task_group context manager.""" |
| 70 | + |
| 71 | + @pytest.mark.anyio |
| 72 | + async def test_single_failure_unwrapped(self) -> None: |
| 73 | + """A single task failure should propagate the original exception, not a group.""" |
| 74 | + with pytest.raises(RuntimeError, match="task failed"): |
| 75 | + async with create_task_group() as tg: |
| 76 | + |
| 77 | + async def failing_task() -> None: |
| 78 | + raise RuntimeError("task failed") |
| 79 | + |
| 80 | + async def long_task() -> None: |
| 81 | + await anyio.sleep(100) |
| 82 | + |
| 83 | + tg.start_soon(failing_task) |
| 84 | + tg.start_soon(long_task) |
| 85 | + |
| 86 | + @pytest.mark.anyio |
| 87 | + async def test_no_failure_clean_exit(self) -> None: |
| 88 | + """Task group with no failures should exit cleanly.""" |
| 89 | + results: list[int] = [] |
| 90 | + async with create_task_group() as tg: |
| 91 | + |
| 92 | + async def worker(n: int) -> None: |
| 93 | + results.append(n) |
| 94 | + |
| 95 | + tg.start_soon(worker, 1) |
| 96 | + tg.start_soon(worker, 2) |
| 97 | + |
| 98 | + assert sorted(results) == [1, 2] |
| 99 | + |
| 100 | + @pytest.mark.anyio |
| 101 | + async def test_chained_cause(self) -> None: |
| 102 | + """The collapsed exception should chain to the original group via __cause__.""" |
| 103 | + with pytest.raises(RuntimeError) as exc_info: |
| 104 | + async with create_task_group() as tg: |
| 105 | + |
| 106 | + async def failing_task() -> None: |
| 107 | + raise RuntimeError("root cause") |
| 108 | + |
| 109 | + async def long_task() -> None: |
| 110 | + await anyio.sleep(100) |
| 111 | + |
| 112 | + tg.start_soon(failing_task) |
| 113 | + tg.start_soon(long_task) |
| 114 | + |
| 115 | + assert isinstance(exc_info.value.__cause__, BaseExceptionGroup) |
0 commit comments