Beispiel #1
0
async def test_get_running_tasks() -> None:
    async def inspect() -> None:
        await wait_all_tasks_blocked()
        new_tasks = set(get_running_tasks()) - existing_tasks
        task_infos[:] = sorted(new_tasks, key=lambda info: info.name or "")
        event.set()

    event = Event()
    task_infos: List[TaskInfo] = []
    host_task = get_current_task()
    async with create_task_group() as tg:
        existing_tasks = set(get_running_tasks())
        tg.start_soon(event.wait, name="task1")
        tg.start_soon(event.wait, name="task2")
        tg.start_soon(inspect)

    assert len(task_infos) == 3
    expected_names = [
        "task1",
        "task2",
        "tests.test_debugging.test_get_running_tasks.<locals>.inspect",
    ]
    for task, expected_name in zip(task_infos, expected_names):
        assert task.parent_id == host_task.id
        assert task.name == expected_name
        assert repr(task) == f"TaskInfo(id={task.id}, name={expected_name!r})"
Beispiel #2
0
    def task_wrap():
        assert anyio.get_current_task().id == outer_task_id

        async def corofn():
            nonlocal inner_task_id
            inner_task_id = anyio.get_current_task().id

        return corofn()
Beispiel #3
0
    def task_wrap() -> Coroutine[object, object, None]:
        assert anyio.get_current_task().id == outer_task_id

        async def corofn() -> None:
            nonlocal inner_task_id
            inner_task_id = anyio.get_current_task().id

        return corofn()
Beispiel #4
0
async def test_start_soon_parent_id() -> None:
    root_task_id = get_current_task().id
    parent_id: Optional[int] = None

    async def subtask() -> None:
        nonlocal parent_id
        parent_id = get_current_task().parent_id

    async def starter_task() -> None:
        tg.start_soon(subtask)

    async with anyio.create_task_group() as tg:
        tg.start_soon(starter_task)

    assert parent_id == root_task_id
Beispiel #5
0
async def test_task_in_sync_spawn_callback():
    outer_task_id = anyio.get_current_task().id
    inner_task_id = None

    def task_wrap():
        assert anyio.get_current_task().id == outer_task_id

        async def corofn():
            nonlocal inner_task_id
            inner_task_id = anyio.get_current_task().id

        return corofn()

    async with create_task_group() as tg:
        tg.spawn(task_wrap)

    assert inner_task_id is not None
    assert inner_task_id != outer_task_id
Beispiel #6
0
async def test_task_in_sync_spawn_callback() -> None:
    outer_task_id = anyio.get_current_task().id
    inner_task_id = None

    def task_wrap() -> Coroutine[object, object, None]:
        assert anyio.get_current_task().id == outer_task_id

        async def corofn() -> None:
            nonlocal inner_task_id
            inner_task_id = anyio.get_current_task().id

        return corofn()

    async with create_task_group() as tg:
        tg.start_soon(task_wrap)

    assert inner_task_id is not None
    assert inner_task_id != outer_task_id
Beispiel #7
0
async def test_get_running_tasks():
    async def inspect():
        await wait_all_tasks_blocked()
        new_tasks = set(get_running_tasks()) - existing_tasks
        task_infos[:] = sorted(new_tasks, key=lambda info: info.name or '')
        event.set()

    event = create_event()
    task_infos = []
    host_task = get_current_task()
    async with create_task_group() as tg:
        existing_tasks = set(get_running_tasks())
        tg.spawn(event.wait, name='task1')
        tg.spawn(event.wait, name='task2')
        tg.spawn(inspect)

    assert len(task_infos) == 3
    expected_names = ['task1', 'task2', 'test_debugging.test_get_running_tasks.<locals>.inspect']
    for task, expected_name in zip(task_infos, expected_names):
        assert task.parent_id == host_task.id
        assert task.name == expected_name
        assert repr(task) == f'TaskInfo(id={task.id}, name={expected_name!r})'
Beispiel #8
0
async def test_start_parent_id() -> None:
    root_task_id = get_current_task().id
    starter_task_id: Optional[int] = None
    initial_parent_id: Optional[int] = None
    permanent_parent_id: Optional[int] = None

    async def subtask(*, task_status: TaskStatus) -> None:
        nonlocal initial_parent_id, permanent_parent_id
        initial_parent_id = get_current_task().parent_id
        task_status.started()
        permanent_parent_id = get_current_task().parent_id

    async def starter_task() -> None:
        nonlocal starter_task_id
        starter_task_id = get_current_task().id
        await tg.start(subtask)

    async with anyio.create_task_group() as tg:
        tg.start_soon(starter_task)

    assert initial_parent_id != permanent_parent_id
    assert initial_parent_id == starter_task_id
    assert permanent_parent_id == root_task_id
Beispiel #9
0
 async def non_main(*, task_status: TaskStatus) -> None:
     task_status.started(anyio.get_current_task().name)
Beispiel #10
0
 async def main() -> None:
     nonlocal task_name
     task_name = get_current_task().name
Beispiel #11
0
 async def test_get_current_task(self) -> None:
     task = await maybe_async(get_current_task())
     assert type(task) is TaskInfo
Beispiel #12
0
 def taskfunc(*, task_status):
     task_status.started(get_current_task().name)
Beispiel #13
0
 async def taskfunc():
     nonlocal task_name
     task_name = get_current_task().name
Beispiel #14
0
 def taskfunc(*, task_status: TaskStatus) -> None:
     task_status.started(get_current_task().name)
Beispiel #15
0
 async def corofn() -> None:
     nonlocal inner_task_id
     inner_task_id = anyio.get_current_task().id
Beispiel #16
0
 async def starter_task() -> None:
     nonlocal starter_task_id
     starter_task_id = get_current_task().id
     await tg.start(subtask)
Beispiel #17
0
 async def subtask(*, task_status: TaskStatus) -> None:
     nonlocal initial_parent_id, permanent_parent_id
     initial_parent_id = get_current_task().parent_id
     task_status.started()
     permanent_parent_id = get_current_task().parent_id
Beispiel #18
0
 async def subtask() -> None:
     nonlocal parent_id
     parent_id = get_current_task().parent_id