]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
add coverage for collapsing
authorThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 15:31:41 +0000 (15:31 +0000)
committerThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 15:31:41 +0000 (15:31 +0000)
tests/test__utils.py

index 916f460d4112a8741f28cdda383a8bc0bcaddb34..e4652ff1bb267fee0750bf4236b74fa47f96c91f 100644 (file)
@@ -1,11 +1,15 @@
 import functools
+import sys
 from typing import Any
 
 import pytest
 
-from starlette._utils import get_route_path, is_async_callable
+from starlette._utils import create_collapsing_task_group, get_route_path, is_async_callable
 from starlette.types import Scope
 
+if sys.version_info < (3, 11):  # pragma: no cover
+    from exceptiongroups import ExceptionGroup
+
 
 def test_async_func() -> None:
     async def async_func() -> None: ...  # pragma: no cover
@@ -94,3 +98,31 @@ def test_async_nested_partial() -> None:
 )
 def test_get_route_path(scope: Scope, expected_result: str) -> None:
     assert get_route_path(scope) == expected_result
+
+
+@pytest.mark.anyio
+async def test_collapsing_task_group_one_exc() -> None:
+    class MyException(Exception):
+        pass
+
+    with pytest.raises(MyException):
+        async with create_collapsing_task_group():
+            raise MyException
+
+
+@pytest.mark.anyio
+async def test_collapsing_task_group_two_exc() -> None:
+    class MyException(Exception):
+        pass
+
+    async def raise_exc() -> None:
+        raise MyException
+
+    with pytest.raises(ExceptionGroup) as exc:
+        async with create_collapsing_task_group() as task_group:
+            task_group.start_soon(raise_exc)
+            raise MyException
+
+    exc1, exc2 = exc.value.exceptions
+    assert isinstance(exc1, MyException)
+    assert isinstance(exc2, MyException)