def wrapper(*args, **kwargs): channel = testsys.get_channel(chan) try: channel.open(1) except testsys.ConnectionFailedException as e: return util.error(e.message) try: return f(channel, *args, **kwargs) finally: channel.close()
def communicate(chan, request=None, check_empty=True): """Send request to testsys and get response. It returns tuple (state, answer), where *state* can be 'error' or 'ok. If state == 'error', *answer* contains formatted error message. If state == 'ok', answer contains tuple (answer, id). This function can be used in many ways: * providing a symbolic channel name as *channel*, e.g. 'MSG', and dict as *request*. This way it will open channel with that name, send request, return response and close channel. * providing :py:class:`testsys.Channel` as *channel* and dict as *request*. This way it will do the same, but on existing channel and will not close it at the end. * providing :py:class:`testsys.Channel` as *channel* and **None** as *request*. This way it will try to recieve something from *channel* and return the response. Optional *check_empty* arguments specifies whether the response should be checked for emptyness""" if isinstance(chan, testsys.Channel): channel = chan need_close = False else: channel = testsys.get_channel(chan) try: channel.open(1) except testsys.ConnectionFailedException as ex: return ('error', error(ex.message)) need_close = True if request: try: ans_id = channel.send(request) except testsys.CommunicationException as ex: return ('error', error(ex.message)) else: ans_id = 0 answer = channel.recv() if not answer and check_empty: return ('error', error("Empty response from testsys")) if 'Error' in answer: return ('error', testsys_error(answer['Error'])) if need_close: channel.close() return ('ok', (answer, ans_id))