diff options
author | Edi Feschiyan <edi.feschiyan@konsulko.com> | 2020-09-14 17:55:19 +0300 |
---|---|---|
committer | Scott Murray <scott.murray@konsulko.com> | 2020-10-01 19:47:16 -0400 |
commit | 69f6e12b6de60d2d28f7b6139ce1a481a82acd25 (patch) | |
tree | e7faa2c9936089893e4049a16a870ca712cc0f6d | |
parent | b8fd8f450d45275b6adfc5a34bf9d3546a2a8515 (diff) |
Add timeout ability to AGLBaseService.listener()
When doing subscription and event testing, there are cases which need
to test lack of emitted events, changing the .listener() async
generator to be able to return None.
Set to None by default which would use self.timeout
Adding extra data on ERROR response from afb if it is available
Changing AFBResponse to raise TypeError instead return
Bug-AGL: SPEC-3585
Signed-off-by: Edi Feschiyan <edi.feschiyan@konsulko.com>
Change-Id: I86c3e9d6242fcbe2ea0c402bcb211e2a89eb9d76
-rw-r--r-- | pyagl/services/base.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/pyagl/services/base.py b/pyagl/services/base.py index d25f222..b64c256 100644 --- a/pyagl/services/base.py +++ b/pyagl/services/base.py @@ -72,6 +72,8 @@ class AFBResponse: info: str def __init__(self, data: list): + if not isinstance(data, list): + raise TypeError('data parameter is not list type') if type(data[0]) is not int: logging.debug(f'Received a response with non-integer message type {binascii.hexlify(data[0])}') raise ValueError('Received a response with non-integer message type') @@ -99,7 +101,7 @@ class AFBResponse: self.msgid = int(data[1]) self.status = data[2]['request'].get('status') self.info = data[2]['request'].get('info') - + self.data = data[2]['request'].get('data') def __str__(self): # for debugging purposes if self.type == AFBT.EVENT: @@ -320,12 +322,21 @@ class AGLBaseService: result = check_output(f'systemctl start {servicename}', shell=True) print(result) - async def listener(self, stdout: bool = False): + async def listener(self, stdout: bool = False, timeout=None): while True: - raw = await self.response() - data = AFBResponse(raw) - if stdout: print(data) - yield data + raw = None + try: + if timeout is not None: + raw = await asyncio.wait_for(self.response(), timeout=timeout) + else: + raw = await asyncio.wait_for(self.response(), timeout=self.timeout) + data = AFBResponse(raw) + if stdout: print(data) + yield data + + except asyncio.TimeoutError: + self.logger.warning("Response wait timed out") + yield None async def response(self): try: |