async def test_wait_closed(loop): wrapped = Wrapped() wrapped.tasks = set() with mock.patch('async_lru._close_waited') as mocked: ret = await _wait_closed( wrapped, return_exceptions=True, loop=loop, ) assert ret == [] assert mocked.called_once() asyncio.set_event_loop(loop) with mock.patch('async_lru._close_waited') as mocked: ret = await _wait_closed( wrapped, return_exceptions=True, loop=None, ) assert ret == [] assert mocked.called_once() asyncio.set_event_loop(None) fut = create_future(loop=loop) fut.set_result(None) wrapped.tasks = {fut} with mock.patch('async_lru._close_waited') as mocked: ret = await _wait_closed( wrapped, return_exceptions=True, loop=loop, ) assert ret == [None] assert mocked.called_once() exc = ZeroDivisionError() fut = create_future(loop=loop) fut.set_exception(exc) wrapped.tasks = {fut} with mock.patch('async_lru._close_waited') as mocked: ret = await _wait_closed( wrapped, return_exceptions=True, loop=loop, ) assert ret == [exc] assert mocked.called_once() fut = create_future(loop=loop) fut.set_exception(ZeroDivisionError) wrapped.tasks = {fut} with mock.patch('async_lru._close_waited') as mocked: with pytest.raises(ZeroDivisionError): await _wait_closed( wrapped, return_exceptions=False, loop=loop, ) assert mocked.called_once()
def test_done_callback(loop): task = create_future(loop=loop) fut = create_future(loop=loop) task.add_done_callback(partial(_done_callback, fut)) task.set_result(1) test_utils.run_briefly(loop) assert fut.result() == 1
def test_done_callback_cancelled(loop): task = create_future(loop=loop) fut = create_future(loop=loop) task.add_done_callback(partial(_done_callback, fut)) task.cancel() test_utils.run_briefly(loop) assert fut.cancelled()
def test_create_future(loop): loop.create_future = mock.Mock() loop.create_future.side_effect = AttributeError fut = create_future(loop=loop) assert isinstance(fut, asyncio.Future) def _create_future(): return asyncio.Future(loop=loop) loop.create_future = _create_future fut = create_future(loop=loop) assert isinstance(fut, asyncio.Future)
def test_done_callback_exception(loop): task = create_future(loop=loop) fut = create_future(loop=loop) task.add_done_callback(partial(_done_callback, fut)) exc = ZeroDivisionError() task.set_exception(exc) test_utils.run_briefly(loop) with pytest.raises(ZeroDivisionError): loop.run_until_complete(fut) with pytest.raises(ZeroDivisionError): fut.result() assert fut.exception() is exc
def test_close(loop): wrapped = Wrapped() wrapped.closed = False wrapped.tasks = set() _close(wrapped, cancel=False, return_exceptions=True, loop=None) assert wrapped.closed with pytest.raises(RuntimeError): _close(wrapped, cancel=False, return_exceptions=True, loop=None) fut = create_future(loop=loop) wrapped.closed = False wrapped.tasks = {fut} _close(wrapped, cancel=True, return_exceptions=True, loop=None) assert fut.cancelled() fut = create_future(loop=loop) fut.set_result(None) wrapped.closed = False wrapped.tasks = {fut} _close(wrapped, cancel=True, return_exceptions=True, loop=None) assert not fut.cancelled() fut = create_future(loop=loop) fut.set_exception(ZeroDivisionError) wrapped.closed = False wrapped.tasks = {fut} _close(wrapped, cancel=True, return_exceptions=True, loop=None) assert not fut.cancelled()