Exemplo n.º 1
0
async def test_defer_to_cancelled_simple_cancel():
    with trio.move_on_after(1) as cancel_scope:
        with defer_to_cancelled(ValueError):
            cancel_scope.cancel()
            await trio.sleep(0)
Exemplo n.º 2
0
async def test_defer_to_cancelled_simple_exception():
    with pytest.raises(ValueError):
        with defer_to_cancelled(ValueError):
            raise ValueError
Exemplo n.º 3
0
    return trio.Cancelled._create()  # type: ignore[attr-defined]


class MyExceptionBase(Exception):
    pass


class MyException(MyExceptionBase):
    pass


@pytest.mark.parametrize(
    "context, to_raise, expected_exception",
    [
        # simple exception
        (defer_to_cancelled(ValueError), ValueError, ValueError),
        # MultiError gets deferred
        (defer_to_cancelled(ValueError),
         trio.MultiError([_cancelled(), ValueError()]), trio.Cancelled),
        # multiple exception types
        (defer_to_cancelled(ValueError, KeyError),
         trio.MultiError([_cancelled(), ValueError(),
                          KeyError()]), trio.Cancelled),
        # nested MultiError
        (defer_to_cancelled(ValueError),
         trio.MultiError(
             [ValueError(),
              trio.MultiError([_cancelled(), _cancelled()])]), trio.Cancelled),
        # non-matching exception
        (defer_to_cancelled(ValueError),
         trio.MultiError([_cancelled(), KeyError()]), trio.MultiError),
Exemplo n.º 4
0
async def test_defer_to_cancelled_not_deferred():
    with pytest.raises(trio.MultiError):
        with defer_to_cancelled(ValueError):
            raise trio.MultiError([trio.Cancelled._create(), KeyError()])