def get_client(): client = Client() client.devices = set() client.monitored_datapoints = {} client.set_datapoint = AsyncMock() return client
async def test_timeouted_cache(backend_client, mocker): addresses = [ "echo.websocket.org" ] used_server_cell_id = "0" persistent_cache = { 'servers_cache': json.dumps({ used_server_cell_id:{ 'timeout': time.time() - 10, 'servers': [(wrap_address(address), 3.206969738006592) for address in addresses] } }) } persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses websocket = MagicMock() websocket.close = AsyncMock() websocket.close.return_value = None connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=lambda *args, **kwargs: async_return_value(websocket) ) assert await cache.get(used_server_cell_id) == [wrap_address(address) for address in addresses] backend_client.get_servers.assert_called_once_with(used_server_cell_id) assert json.loads(persistent_cache['servers_cache'])[used_server_cell_id]['timeout'] > time.time() assert persistent_cache_state.modified connect.assert_has_calls([call(wrap_address(address), ssl=ANY) for address in addresses])
async def test_no_cache_fist_connect_failure(backend_client, mocker, exception): addresses = [ "address_1", "address_2" ] persistent_cache = {} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses websocket = MagicMock() websocket.close = AsyncMock() websocket.close.return_value = None connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(websocket) ] ) used_server_cell_id = "0" assert await cache.get(used_server_cell_id) == [wrap_address(addresses[1])] backend_client.get_servers.assert_called_once_with(used_server_cell_id) assert 'servers_cache' in persistent_cache assert persistent_cache_state.modified connect.assert_has_calls([call(wrap_address(address), ssl=ANY) for address in addresses])
def vehi(client, mocker, result=None): ctx = MagicMock() resp = AsyncMock() ctx.__aenter__.return_value = resp ctx.__aexit__.return_value = resp ok = AsyncMock(return_value={"response": result}) resp.json = ok mocker.patch.object(client, "_get_headers", return_value={"hello": "you"}) mocker.patch.object(client, "authenticate", new=AsyncMock(return_value=None)) mocker.patch.object(client._session, "get", return_value=ctx) mocker.patch.object(client._session, "post", return_value=ctx) return Vehicle(client, FULL_DATA)
async def get_client(self): client = Client() client.devices = set() client.set_datapoint = AsyncMock() return client