def test_error_running_36orless_compat(self, mocker, arbitrary_exception): # NOTE: This test is not necessary for 3.7 because asyncio.run() does this for us mock_new_event_loop = mocker.patch.object(asyncio, "new_event_loop") mock_set_event_loop = mocker.patch.object(asyncio, "set_event_loop") mock_loop = mock_new_event_loop.return_value mock_loop.run_until_complete.side_effect = arbitrary_exception mock_coro = mocker.MagicMock() with pytest.raises(type(arbitrary_exception)): asyncio_compat.run(mock_coro) assert mock_loop.close.call_count == 1 assert mock_set_event_loop.call_count == 2 # Once set, once to unset assert mock_set_event_loop.call_args_list[0] == mocker.call(mock_loop) assert mock_set_event_loop.call_args_list[1] == mocker.call(None)
def test_run_37_or_greater(self, mocker, dummy_coroutine): mock_asyncio_run = mocker.patch.object(asyncio, "run") coro = dummy_coroutine() result = asyncio_compat.run(coro) assert mock_asyncio_run.call_count == 1 assert mock_asyncio_run.call_args == mocker.call(coro) assert result == mock_asyncio_run.return_value
def test_run_37_or_greater(self, mocker): mock_asyncio_run = mocker.patch.object(asyncio, "run") mock_coro = mocker.MagicMock() result = asyncio_compat.run(mock_coro) assert mock_asyncio_run.call_count == 1 assert mock_asyncio_run.call_args == mocker.call(mock_coro) assert result == mock_asyncio_run.return_value
def test_run_36orless_compat(self, mocker, dummy_coroutine): mock_new_event_loop = mocker.patch.object(asyncio, "new_event_loop") mock_set_event_loop = mocker.patch.object(asyncio, "set_event_loop") mock_loop = mock_new_event_loop.return_value coro = dummy_coroutine() result = asyncio_compat.run(coro) # New event loop was created and set assert mock_new_event_loop.call_count == 1 assert mock_new_event_loop.call_args == mocker.call() assert mock_set_event_loop.call_count == 2 # (second time at the end) assert mock_set_event_loop.call_args_list[0] == mocker.call(mock_loop) # Coroutine was run on the event loop, with the result returned assert mock_loop.run_until_complete.call_count == 1 assert mock_loop.run_until_complete.call_args == mocker.call(coro) assert result == mock_loop.run_until_complete.return_value # Loop was closed after completion assert mock_loop.close.call_count == 1 assert mock_loop.close.call_args == mocker.call() # Event loop was set back to None assert mock_set_event_loop.call_args_list[1] == mocker.call(None)
def coro_wrapper(coro, arg): asyncio_compat.run(coro(arg))