| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import asyncio |
| 12 |
import sys |
| 13 |
from collections.abc import Callable |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import errors, given, reject, strategies as st |
| 18 |
from hypothesis.internal.compat import ExceptionGroup |
| 19 |
from hypothesis.strategies import DataObject |
| 20 |
|
| 21 |
# this file is not typechecked by mypy, which only runs py310 |
| 22 |
|
| 23 |
if sys.version_info < (3, 11): |
| 24 |
pytest.skip("asyncio.TaskGroup not available on <py3.11", allow_module_level=True) |
| 25 |
|
| 26 |
|
| 27 |
def test_exceptiongroup_discard_frozen(): |
| 28 |
"""Basic test that raises Frozen+Unsatisfiable. |
| 29 |
Frozen is thrown out, and Unsatisfiable is raised""" |
| 30 |
|
| 31 |
@given(st.data()) |
| 32 |
def test_function(data): |
| 33 |
async def task(pred): |
| 34 |
return data.draw(st.booleans().filter(pred)) |
| 35 |
|
| 36 |
async def _main(): |
| 37 |
async with asyncio.TaskGroup() as tg: |
| 38 |
tg.create_task(task(bool)) |
| 39 |
tg.create_task(task(lambda _: False)) |
| 40 |
|
| 41 |
asyncio.run(_main()) |
| 42 |
|
| 43 |
with pytest.raises(errors.Unsatisfiable): |
| 44 |
test_function() |
| 45 |
|
| 46 |
|
| 47 |
def test_exceptiongroup_nested() -> None: |
| 48 |
@given(st.data()) |
| 49 |
def test_function(data: DataObject) -> None: |
| 50 |
async def task(pred: Callable[[bool], bool]) -> None: |
| 51 |
data.draw(st.booleans().filter(pred)) |
| 52 |
|
| 53 |
async def _main() -> None: |
| 54 |
async with asyncio.TaskGroup(), asyncio.TaskGroup() as tg2: |
| 55 |
tg2.create_task(task(bool)) |
| 56 |
tg2.create_task(task(lambda _: False)) |
| 57 |
|
| 58 |
asyncio.run(_main()) |
| 59 |
|
| 60 |
with pytest.raises(errors.Unsatisfiable): |
| 61 |
test_function() |
| 62 |
|
| 63 |
|
| 64 |
def test_exceptiongroup_user_originated() -> None: |
| 65 |
@given(st.data()) |
| 66 |
def test_function(data): |
| 67 |
raise ExceptionGroup("foo", [ValueError(), ValueError()]) |
| 68 |
|
| 69 |
with pytest.raises(ExceptionGroup) as exc_info: |
| 70 |
test_function() |
| 71 |
e = exc_info.value |
| 72 |
assert e.message == "foo" |
| 73 |
assert isinstance(e, ExceptionGroup) |
| 74 |
assert len(e.exceptions) == 2 |
| 75 |
assert all(isinstance(child_e, ValueError) for child_e in e.exceptions) |
| 76 |
|
| 77 |
@given(st.data()) |
| 78 |
def test_single_exc_group(data): |
| 79 |
raise ExceptionGroup("important message for user", [ValueError()]) |
| 80 |
|
| 81 |
with pytest.raises(ExceptionGroup) as exc_info: |
| 82 |
test_single_exc_group() |
| 83 |
e = exc_info.value |
| 84 |
assert e.message == "important message for user" |
| 85 |
assert isinstance(e, ExceptionGroup) |
| 86 |
assert len(e.exceptions) == 1 |
| 87 |
assert isinstance(e.exceptions[0], ValueError) |
| 88 |
|
| 89 |
|
| 90 |
def test_exceptiongroup_multiple_stop() -> None: |
| 91 |
@given(st.data()) |
| 92 |
def test_function(data): |
| 93 |
async def task(d: DataObject) -> None: |
| 94 |
d.conjecture_data.mark_invalid() |
| 95 |
|
| 96 |
async def _main(d: DataObject) -> None: |
| 97 |
async with asyncio.TaskGroup() as tg: |
| 98 |
tg.create_task(task(d)) |
| 99 |
tg.create_task(task(d)) |
| 100 |
|
| 101 |
asyncio.run(_main(data)) |
| 102 |
|
| 103 |
# multiple stoptests -> single stoptest -> unsatisfiable |
| 104 |
with pytest.raises(errors.Unsatisfiable): |
| 105 |
test_function() |
| 106 |
|
| 107 |
|
| 108 |
def test_exceptiongroup_stop_and_hypothesisexception() -> None: |
| 109 |
# group with stoptest+invalidargument -> invalidargument |
| 110 |
@given(st.data()) |
| 111 |
def test_function(data): |
| 112 |
async def task_stoptest(d: DataObject) -> None: |
| 113 |
# only mark some runs as invalid to not raise Unsatisfiable |
| 114 |
if d.draw(st.integers(min_value=0, max_value=1)) == 1: |
| 115 |
d.conjecture_data.mark_invalid() |
| 116 |
|
| 117 |
async def task_invalid_argument(d: DataObject) -> None: |
| 118 |
d.draw(st.integers(max_value=2, min_value=3)) |
| 119 |
|
| 120 |
async def _main(d: DataObject) -> None: |
| 121 |
async with asyncio.TaskGroup() as tg: |
| 122 |
tg.create_task(task_stoptest(d)) |
| 123 |
tg.create_task(task_invalid_argument(d)) |
| 124 |
|
| 125 |
asyncio.run(_main(data)) |
| 126 |
|
| 127 |
with pytest.raises(errors.InvalidArgument): |
| 128 |
test_function() |
| 129 |
|
| 130 |
|
| 131 |
def test_exceptiongroup_multiple_hypothesisexception() -> None: |
| 132 |
# multiple UnsatisfiedAssumption => first one is reraised => engine suppresses it |
| 133 |
|
| 134 |
@given(st.integers(min_value=0, max_value=3)) |
| 135 |
def test_function(val: int) -> None: |
| 136 |
async def task(value: int) -> None: |
| 137 |
if value == 0: |
| 138 |
reject() |
| 139 |
|
| 140 |
async def _main(value: int) -> None: |
| 141 |
async with asyncio.TaskGroup() as tg: |
| 142 |
tg.create_task(task(value)) |
| 143 |
tg.create_task(task(value)) |
| 144 |
|
| 145 |
asyncio.run(_main(val)) |
| 146 |
|
| 147 |
test_function() |
| 148 |
|
| 149 |
|
| 150 |
def test_exceptiongroup_multiple_InvalidArgument() -> None: |
| 151 |
# multiple InvalidArgument => only first one is reraised... which seems bad. |
| 152 |
# But raising a group might break ghostwriter(?) |
| 153 |
|
| 154 |
@given(st.data()) |
| 155 |
def test_function(data: DataObject) -> None: |
| 156 |
async def task1(d: DataObject) -> None: |
| 157 |
d.draw(st.integers(max_value=1, min_value=3)) |
| 158 |
|
| 159 |
async def task2(d: DataObject) -> None: |
| 160 |
d.draw(st.integers(max_value=2, min_value=3)) |
| 161 |
|
| 162 |
async def _main(d: DataObject) -> None: |
| 163 |
async with asyncio.TaskGroup() as tg: |
| 164 |
tg.create_task(task1(d)) |
| 165 |
tg.create_task(task2(d)) |
| 166 |
|
| 167 |
asyncio.run(_main(data)) |
| 168 |
|
| 169 |
with pytest.raises(errors.InvalidArgument): |
| 170 |
test_function() |