def test_cancel_task_blocking_on_done_future(loop, future): task = aio.Task(_wait_and_sleep_after_cancel(future)) loop.call_soon(future.set_result, 9) loop.call_soon(task.cancel) assert loop.run_until_complete(task) == -9 assert not future.cancelled() assert not task.cancelled()
def test_task_add_done_callback(loop): results = [] task = aio.Task(_coro_returning(9)) task.add_done_callback(lambda f: results.append(f.result())) task.add_done_callback(lambda f: loop.stop()) loop.run_forever() assert results == [9]
def test_cancel_task(loop): task = aio.Task(_coro_pass()) assert task.cancel() is True with pytest.raises(aio.CancelledError): loop.run_until_complete(task) assert task.cancelled() assert task.cancel() is False
def test_task_remove_done_callback(loop): results = [] def add_result(f): results.append(f.result()) task = aio.Task(_coro_pass()) task.add_done_callback(add_result) task.add_done_callback(add_result) assert task.remove_done_callback(add_result) == 2 loop.run_until_complete(task) assert results == []
def test_all_tasks(loop): tasks = [] task = aio.Task(_coro_append_all_tasks(tasks)) loop.run_until_complete(task) assert tasks == [{task}]
def test_current_task(loop): task = aio.Task(_coro_return_current_task()) assert loop.run_until_complete(task) is task
def test_task_get_loop(loop): coro = _coro_pass() task = aio.Task(coro) assert task.get_loop() is loop coro.close()
def test_cancel_itself_after_await(loop, future): task = aio.Task(_coro_cancel_itself_after_sleep(future)) with pytest.raises(aio.CancelledError): loop.run_until_complete(task)
def test_cancel_task_cancels_future_blocking(loop, future): task = aio.Task(_wait(future)) loop.call_soon(task.cancel) with pytest.raises(aio.CancelledError): loop.run_until_complete(task) assert future.cancelled()
def test_cancel_task_blocking_on_future(loop): task = aio.Task(_coro_ignoring_cancelled_error()) loop.call_soon(task.cancel) assert loop.run_until_complete(task) == -9
def test_coroutine_with_task(loop): inner_task = aio.ensure_future(_coro_returning(9)) task = aio.Task(_wait(inner_task)) assert loop.run_until_complete(task) == 9
def test_coroutine_exception(loop): task = aio.Task(_coro_raising(ZeroDivisionError)) with pytest.raises(ZeroDivisionError): loop.run_until_complete(task) with pytest.raises(ZeroDivisionError): task.result()
def test_get_loop(coro): loop = aio.new_event_loop() task = aio.Task(coro, loop=loop) assert task.get_loop() is loop
def test_doesnt_support_set_exception(coro): task = aio.Task(coro) with pytest.raises(RuntimeError): task.set_exception(9)
def test_doesnt_support_set_result(coro): task = aio.Task(coro) with pytest.raises(RuntimeError): task.set_result(9)