aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdi Feschiyan <edi.feschiyan@konsulko.com>2020-09-14 17:55:19 +0300
committerEdi Feschiyan <edi.feschiyan@konsulko.com>2020-09-23 12:55:12 +0300
commitc79ce262886092cc96e654c726220ef321967a04 (patch)
tree2061ccdccc3761be7f78c0b1c972730274d47e2c
parent9dd59f43f54455cde3168643802c57ad41429608 (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: I09a62c559a1e8ca4bebe6f696167ca61ffb82b59
-rw-r--r--pyagl/services/base.py23
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: