From 126fcd844c976ff2c0cc7005ea39de417dbeac52 Mon Sep 17 00:00:00 2001 From: Julien THOMAS Date: Wed, 6 Jul 2022 09:47:40 +0200 Subject: [PATCH] feat: ComAux ability to clear receiving buffer --- examples/test_suite_1/test_suite_1.py | 2 +- .../lib/auxiliaries/communication_auxiliary.py | 11 ++++++++++- .../test_coordinator/test_message_handler.py | 2 +- tests/test_com_aux.py | 15 ++++++++++----- tests/test_test_case.py | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/test_suite_1/test_suite_1.py b/examples/test_suite_1/test_suite_1.py index 1219cbe2f..aae651010 100644 --- a/examples/test_suite_1/test_suite_1.py +++ b/examples/test_suite_1/test_suite_1.py @@ -18,8 +18,8 @@ using TestApp. """ -import logging import importlib +import logging from itertools import cycle import pykiso diff --git a/src/pykiso/lib/auxiliaries/communication_auxiliary.py b/src/pykiso/lib/auxiliaries/communication_auxiliary.py index 4944f7d27..f61cc2fdc 100644 --- a/src/pykiso/lib/auxiliaries/communication_auxiliary.py +++ b/src/pykiso/lib/auxiliaries/communication_auxiliary.py @@ -50,18 +50,27 @@ def send_message(self, raw_msg: bytes) -> bool: return self.run_command("send", raw_msg, timeout_in_s=None) def receive_message( - self, blocking: bool = True, timeout_in_s: float = None + self, + blocking: bool = True, + timeout_in_s: float = None, + empty_queue: bool = True, ) -> Optional[bytes]: """Receive a raw message. :param blocking: wait for message till timeout elapses? :param timeout_in_s: maximum time in second to wait for a response + :param empty_queue: empty buffer flag :returns: raw message """ log.debug( f"retrieving message in {self} (blocking={blocking}, timeout={timeout_in_s})" ) + + if empty_queue: + log.info("Clearing receiving buffer.") + self.queue_out = self.queue_out.empty() + response = self.wait_and_get_report( blocking=blocking, timeout_in_s=timeout_in_s ) diff --git a/src/pykiso/test_coordinator/test_message_handler.py b/src/pykiso/test_coordinator/test_message_handler.py index 8ad7ad3b2..ff75e9451 100644 --- a/src/pykiso/test_coordinator/test_message_handler.py +++ b/src/pykiso/test_coordinator/test_message_handler.py @@ -11,7 +11,7 @@ Handle common communication with device under test ************************************************** -When using a Remote TestCase/TestSuite, the integration test framework handles +When using a Remote TestCase/TestSuite, the integration test framework handles internal messaging and control flow using a message format defined in :py:class:`pykiso.Message`. :py:mod:`pykiso.test_message_handler` defines the messaging protocol from a behavioral point of view. diff --git a/tests/test_com_aux.py b/tests/test_com_aux.py index 778d2a0df..09860ec8f 100644 --- a/tests/test_com_aux.py +++ b/tests/test_com_aux.py @@ -8,6 +8,7 @@ ########################################################################## import logging +import queue import pytest @@ -136,9 +137,7 @@ def test_run_command_valid( expected_function_return, ): com_aux_init.logger = logging.Logger("ok") - mock_channel = mocker.patch.object( - com_aux_init.channel, "cc_send", side_effect=side_effect_mock - ) + mocker.patch.object(com_aux_init.channel, "cc_send", side_effect=side_effect_mock) with caplog.at_level(logging.INFO): result_create_inst = com_aux_init._run_command("send") assert result_create_inst is expected_function_return @@ -183,9 +182,15 @@ def test_receive_message_exception(mocker, com_aux_init, caplog): ) -def test_receive_message_none(mocker, com_aux_init): +@pytest.mark.parametrize("clear_buffer", [True, False]) +def test_receive_message_none(mocker, com_aux_init, clear_buffer): mocker.patch.object(com_aux_init, "wait_and_get_report", return_value=None) - recv = com_aux_init.receive_message(2) + mock_empty = mocker.patch.object(queue.Queue, "empty") + + recv = com_aux_init.receive_message(timeout_in_s=2, empty_queue=clear_buffer) + + if clear_buffer: + mock_empty.assert_called_once() assert recv is None diff --git a/tests/test_test_case.py b/tests/test_test_case.py index 9afd23d12..dd2395a97 100644 --- a/tests/test_test_case.py +++ b/tests/test_test_case.py @@ -7,8 +7,8 @@ # SPDX-License-Identifier: EPL-2.0 ########################################################################## -import unittest import logging +import unittest from functools import partial import pytest