async def test_cloud_connect(hass_platform_cloud_connection, config, aioclient_mock, caplog): hass = hass_platform_cloud_connection session = MockSession(aioclient_mock) manager = CloudManager(hass, config, session) with patch.object(manager, '_try_reconnect', return_value=None) as mock_reconnect: await manager.connect() mock_reconnect.assert_not_called() assert session.ws.headers['Authorization'] == 'Bearer foo' session = MockSession(aioclient_mock) manager = CloudManager(hass, config, session) with patch.object(session, 'ws_connect', side_effect=Exception()), patch.object( manager, '_try_reconnect', return_value=None) as mock_reconnect: await manager.connect() mock_reconnect.assert_called_once() caplog.clear() session = MockSession(aioclient_mock) manager = CloudManager(hass, config, session) with patch.object(session, 'ws_connect', side_effect=TimeoutError()), patch.object( manager, '_try_reconnect', return_value=None) as mock_reconnect: await manager.connect() mock_reconnect.assert_called_once() assert 'Failed to connect' in caplog.records[-1].message session = MockSession(aioclient_mock, ws_close_code=1000) manager = CloudManager(hass, config, session) with patch.object(manager, '_try_reconnect', return_value=None) as mock_reconnect: await manager.connect() mock_reconnect.assert_called_once()
async def request(self, method, url, data, headers, **kwargs): try: # print(url) with async_timeout.timeout(TIMEOUT): async with self._session.request(method, url, headers=headers, data=data) as response: # print(response) if response.status == 200 or response.status == 202: return await response.json(loads=json_loads) else: raise ClientResponseError( response.request_info, response.history, status=response.status, message=response.reason, ) except TimeoutError: raise TimeoutError("Timeout error") except Exception: raise
def test_formatter(self): formatter = ErrorFormatter(DefaultErrorFormatter()) formatter.register(ClientError, ClientErrorFormatter()) try: raise ConnectionError() from ClientConnectionError() except Exception as e: self.assertIn("aiohttp.client_exceptions.ClientConnectionError", ClientErrorFormatter().format(e)) try: raise ConnectionError() from TimeoutError() except Exception as e: self.assertIn("超时", ClientErrorFormatter().format(e)) try: raise HandlerError() from ValueError("输入错误") except Exception as e: self.assertIn("输入错误", DefaultErrorFormatter().format(e)) try: raise ValueError("输入错误") except Exception as e: self.assertIn("输入错误", DefaultErrorFormatter().format(e))
async def fetch(retry=0): proxy = 'http://{}'.format(Proxy.get_random()['address']) headers = {'user-agent': get_user_agent()} conn = aiohttp.ProxyConnector(proxy=proxy) url = 'http://httpbin.org/ip' try: with aiohttp.ClientSession(connector=conn) as session: with aiohttp.Timeout(TIMEOUT): async with session.get(url, headers=headers) as resp: return await resp.json() except (ProxyConnectionError, TimeoutError): try: p = Proxy.objects.get(address=proxy) if p: p.delete() except DoesNotExist: pass retry += 1 if retry > 5: raise TimeoutError() await asyncio.sleep(1) return await fetch(retry=retry)
def unreachable_get_ublox_token(m: aioresponses): """Mock the request made to keycloak to obtain a valid token""" m.post(TOKEN_REQUEST_URL, exception=TimeoutError("Timeout"))
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)
def timer_callback(f): if not f.done() and not f.cancelled(): f.set_exception(TimeoutError())
def unreachable_extract_details(m: aioresponses, journey_id: str): m.get(f"{URL_EXTRACT_DETAILS}/{journey_id}", exception=TimeoutError("Timeout"))
def unreachable_extract_mobility(m: aioresponses, journey_id: str): m.get(f"{URL_EXTRACT_MOBILITY}/{journey_id}", exception=TimeoutError("Timeout"))
def unreachable_store_user_in_the_anonengine(m: aioresponses): m.post(URL_STORE_DATA, exception=TimeoutError("Timeout"))
def unreachable_store_in_iota(m: aioresponses): """Mocked store in iota""" m.post(URL_STORE_IN_IOTA, exception=TimeoutError("Timeout"))
async def on_timeout(self) -> None: self.future.set_exception(TimeoutError())
def do_request(): raise TimeoutError()
def wait_for_events( cls, predict, handlers: List["EventHandler"], raise_errors: bool = True, wait_count: int = 1, timeout: float = None, ) -> List["EventHandler"]: """Waits for a specific event to be invoked on the handlers. Args: predict (Callable or str): If a string, waits for the specific event. Otherwise expects true when a matching event is found. (lambda task, name: ? true) handlers (list) : List of handlers to execute for. raise_errors (bool): True if to raise handler errors. wait_count (int): How many times should the event be triggered. Must be larger then zero. timeout (float): The timeout in seconds before throwing an error. Returns: The list of tasks sent to the method. """ if isinstance(handlers, EventHandler): handlers: List["Task"] = [handlers] assert wait_count > 0, "Wait count must be at least 1" if predict is not None and not isinstance(predict, Callable): predict_equals = predict def predict_event_by_name(sender: EventHandler, event: Event): return event.name == predict_equals predict = predict_event_by_name assert predict is None or callable( predict), "Predict must be a Callable or event name string" wait_queue = Queue() first_error = None matched_handlers = [] def stop_on_error(hndl: EventHandler, error): nonlocal first_error first_error = error wait_queue.put(False) def on_piped_event(handler: EventHandler, event: Event): if event.name == handler.error_event_name: stop_on_error(handler, event.args[1]) return try: if predict is None or predict(handler, event): matched_handlers.append(handler) if len(matched_handlers) == wait_count: wait_queue.put(True) except Exception as err: stop_on_error(handler, err) pipes = [] def bind(handler: EventHandler): pipe_handler = EventHandler( on_event=lambda event: on_piped_event(handler, event)) pipes.append(pipe_handler) handler.pipe( pipe_handler, use_weak_reference=True, ) for handler in handlers: bind(handler) try: wait_queue.get(timeout=timeout) except Empty: first_error = TimeoutError( f"Timeout while waiting for event: {predict}") if raise_errors and first_error is not None: raise first_error elif first_error is not None: return first_error return matched_handlers
def starvation_extract_from_ipt_anonymizer(m: aioresponses, url: str): m.post(url=url, exception=TimeoutError("Starvation"))
def starvation_store_in_ipt_anonymizer(m: aioresponses, url: str): m.post(url=url, exception=TimeoutError("Starvation"))
async def create(cls, ctx: commands.Context, query): """ Creates and returns a YTDLSource object, which represents audio content from YouTube. """ query = ' '.join(query) with YoutubeDL(cls.ytdl_opts) as ydl: await ctx.message.add_reaction("🔍") search_func = partial(ydl.extract_info, url=query, download=False) info = await ctx.bot.loop.run_in_executor(None, search_func) if 'entries' in info: # Grab the first video. info = info['entries'][0] confirmation_msg = None # Check the duration if info['duration'] > cls.CUTOFF_DURATION: fduration = datetime.timedelta(seconds=info['duration']) confirmation_msg = await ctx.reply( f'This video is {fduration} long. Are you sure you want to play it?', mention_author=True) for emoji in '✅❌': await confirmation_msg.add_reaction(emoji) def check_confirmation(reaction, user): if user == reaction.message.author: return False return reaction.message.id == confirmation_msg.id and ( reaction.emoji) in '✅❌' try: reaction, user = await ctx.bot.wait_for( 'reaction_add', timeout=30, check=check_confirmation) except TimeoutError: # After 30 seconds, trigger a timeout. await confirmation_msg.delete() raise TimeoutError( 'Confirmation to download content timed out.') if str(reaction.emoji) == '❌': await confirmation_msg.delete() raise UserCanceledDownloadError( 'Content download canceled by user.') if confirmation_msg: await confirmation_msg.delete() await ctx.message.remove_reaction('🔍', ctx.me) await ctx.message.add_reaction('⌛') # Download the video download_func = partial(ydl.extract_info, url=info['webpage_url']) # Interesting observation: Songs may play out of order depending on how long it takes them to download. download_data = await ctx.bot.loop.run_in_executor( None, download_func) path = ydl.prepare_filename(download_data) # await ctx.message.remove_reaction('🔍', ctx.me) # TODO might throw await ctx.message.remove_reaction('⌛', ctx.me) await ctx.message.add_reaction('✅') return cls(query, download_data, path)
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",
def MockInitialize(): raise TimeoutError()
def unreachable_get_iota_user(m: aioresponses, user: str): """Mocked get iota user""" m.get( f"{URL_GET_IOTA_USER}?user={user}-{datetime.now().date()}", exception=TimeoutError("Timeout"), )