async def run(self): try: await service_status.wait_autoconnecting(self.app) service_status.set_connected(self.app, self._address) self.update_ticks() await self.welcome() while True: await asyncio.sleep(3600) except Exception as ex: # pragma: no cover LOGGER.error(strex(ex)) raise ex finally: service_status.set_disconnected(self.app)
async def test_timeout(app, client, syncher, mocker): async def m_wait_ack(app, wait=True): if wait: await asyncio.sleep(1) return False await disconnect(app) await syncher.end() mocker.patch(TESTED + '.HANDSHAKE_TIMEOUT_S', 0.1) mocker.patch(TESTED + '.PING_INTERVAL_S', 0.0001) mocker.patch(TESTED + '.service_status.wait_acknowledged', side_effect=m_wait_ack) mocker.patch.object(spark.fget(app), 'noop', AsyncMock(side_effect=RuntimeError)) service_status.set_connected(app, 'timeout test') with pytest.raises(asyncio.TimeoutError): await syncher.run()
async def startup(self, app: web.Application): await self._responder.startup(app) # Normally handled by communication service_status.set_connected(app, 'simulation:1234') service_status.set_acknowledged(app, make_device(app))
async def run(self): """Implements RepeaterFeature.run""" try: if self._retry_count >= CONNECT_RETRY_COUNT: raise ConnectionAbortedError() if self._retry_count == 1: LOGGER.info('Retrying connection...') if self._retry_count > 0: await asyncio.sleep(self.retry_interval) await service_status.wait_autoconnecting(self.app) result = await connect_funcs.connect(self.app) self._proc = result.process self._address = result.address self._reader = result.reader self._writer = result.writer self._parser = cbox_parser.ControlboxParser() service_status.set_connected(self.app, self._address) self._retry_count = 0 self.reset_retry_interval() LOGGER.info(f'{self} connected') while self.connected: # read() does not raise an exception when connection is closed # connected status must be checked explicitly later recv = await self._reader.read(100) # read() returns empty if EOF received if not recv: # pragma: no cover raise ConnectionError('EOF received') # Send to parser self._parser.push(recv.decode()) # Drain parsed messages for msg in self._parser.event_messages(): await self._on_event(msg) for msg in self._parser.data_messages(): await self._on_data(msg) raise ConnectionError('Connection closed') except ConnectionAbortedError: LOGGER.error('Connection aborted. Exiting now.') self.increase_retry_interval() raise web.GracefulExit() except connect_funcs.DiscoveryAbortedError as ex: LOGGER.error('Device discovery failed.') if ex.reboot_required: self._retry_count += 1 raise ex except Exception: self._retry_count += 1 raise finally: with suppress(Exception): self._writer.close() LOGGER.info(f'{self} closed stream writer') with suppress(Exception): self._proc.terminate() LOGGER.info(f'{self} terminated subprocess') service_status.set_disconnected(self.app) self._proc = None self._reader = None self._writer = None self._parser = None
async def connect(app, syncher): service_status.set_connected(app, 'synchronization test') await syncher.run() await asyncio.sleep(0.01)