1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from contextlib import contextmanager
 
from .. import _core
 
 
@contextmanager
def _assert_yields_or_not(expected):
    __tracebackhide__ = True
    task = _core.current_task()
    orig_cancel = task._cancel_points
    orig_schedule = task._schedule_points
    try:
        yield
        if expected and (
            task._cancel_points == orig_cancel or task._schedule_points == orig_schedule
        ):
            raise AssertionError("assert_checkpoints block did not yield!")
    finally:
        if not expected and (
            task._cancel_points != orig_cancel or task._schedule_points != orig_schedule
        ):
            raise AssertionError("assert_no_checkpoints block yielded!")
 
 
def assert_checkpoints():
    """Use as a context manager to check that the code inside the ``with``
    block either exits with an exception or executes at least one
    :ref:`checkpoint <checkpoints>`.
 
    Raises:
      AssertionError: if no checkpoint was executed.
 
    Example:
      Check that :func:`trio.sleep` is a checkpoint, even if it doesn't
      block::
 
         with trio.testing.assert_checkpoints():
             await trio.sleep(0)
 
    """
    __tracebackhide__ = True
    return _assert_yields_or_not(True)
 
 
def assert_no_checkpoints():
    """Use as a context manager to check that the code inside the ``with``
    block does not execute any :ref:`checkpoints <checkpoints>`.
 
    Raises:
      AssertionError: if a checkpoint was executed.
 
    Example:
      Synchronous code never contains any checkpoints, but we can double-check
      that::
 
         send_channel, receive_channel = trio.open_memory_channel(10)
         with trio.testing.assert_no_checkpoints():
             send_channel.send_nowait(None)
 
    """
    __tracebackhide__ = True
    return _assert_yields_or_not(False)