@pytest.mark.asyncio async def test_servers_cache_failure(client, protocol_client, backend_client, servers_cache): servers_cache.get.return_value = async_raise(AccessDenied()) await client.run() servers_cache.get.assert_called_once_with() backend_client.get_authentication_data.assert_not_called() protocol_client.authenticate.assert_not_called() protocol_client.run.assert_not_called() @pytest.mark.asyncio @pytest.mark.parametrize("exception", [ asyncio.TimeoutError(), IOError(), websockets.InvalidURI("wss://websocket_1"), websockets.InvalidHandshake() ]) async def test_connect_error(client, backend_client, protocol_client, servers_cache, mocker, exception): servers_cache.get.side_effect = [ async_return_value(["wss://websocket_1", "wss://websocket_2"]), ] connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[async_raise(exception), async_return_value(MagicMock())]) backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.return_value = async_raise( websockets.ConnectionClosedOK(1000, ""), 10)
await client.run() sleep.assert_any_call(RECONNECT_INTERVAL_SECONDS) @pytest.mark.asyncio async def test_servers_cache_failure(client, protocol_client, backend_client, servers_cache): servers_cache.get.return_value = async_raise(AccessDenied()) await client.run() servers_cache.get.assert_called_once_with() backend_client.get_authentication_data.assert_not_called() protocol_client.authenticate.assert_not_called() protocol_client.run.assert_not_called() @pytest.mark.asyncio @pytest.mark.parametrize("exception", [ asyncio.TimeoutError(), IOError(), websockets.InvalidURI("wss://websocket_1"), websockets.InvalidHandshake() ]) async def test_connect_error(client, backend_client, protocol_client, servers_cache, mocker, exception): servers_cache.get.side_effect = [ async_return_value(["wss://websocket_1", "wss://websocket_2"]), ] connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(MagicMock()) ] ) backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.return_value = async_raise(websockets.ConnectionClosedOK(1000, ""), 10)
from persistent_cache_state import PersistentCacheState async def async_raise(error, loop_iterations_delay=0): if loop_iterations_delay > 0: await skip_loop(loop_iterations_delay) raise error def wrap_address(addr): return "wss://{}/cmsocket/".format(addr) @pytest.mark.asyncio @pytest.mark.parametrize("exception", [ TimeoutError(), IOError(), websockets.InvalidURI(wrap_address("address_1")), websockets.InvalidHandshake() ]) async def test_no_cache_all_connect_failure(backend_client, mocker, exception): addresses = [ "address_1" ] persistent_cache = {} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses connect = mocker.patch( "protocol.websocket_client.websockets.connect", return_value=async_raise(exception)
async def async_raise(error, loop_iterations_delay=0): if loop_iterations_delay > 0: await skip_loop(loop_iterations_delay) raise error def wrap_address(addr): return "wss://{}/cmsocket/".format(addr) @pytest.mark.asyncio @pytest.mark.parametrize("exception", [ TimeoutError(), IOError(), websockets.InvalidURI(wrap_address("address_1")), websockets.InvalidHandshake() ]) async def test_no_cache_all_connect_failure(backend_client, mocker, exception): addresses = ["address_1"] persistent_cache = {} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses connect = mocker.patch("protocol.websocket_client.websockets.connect", return_value=async_raise(exception))