From ec5b3c305aa2a2c70a9290dc7e883b539c034616 Mon Sep 17 00:00:00 2001 From: mihirpat1 <112018033+mihirpat1@users.noreply.github.com> Date: Tue, 9 Apr 2024 13:33:17 -0700 Subject: [PATCH] [202311] Fix intermittent build failure for test_SfpStateUpdateTask_task_run_stop (#461) * Fix intermittent built failure for test_SfpStateUpdateTask_task_run_stop Signed-off-by: Mihir Patel * Modified polling to forever --------- Signed-off-by: Mihir Patel --- sonic-xcvrd/tests/test_xcvrd.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sonic-xcvrd/tests/test_xcvrd.py b/sonic-xcvrd/tests/test_xcvrd.py index f6d39e9f7..fd43cd4c0 100644 --- a/sonic-xcvrd/tests/test_xcvrd.py +++ b/sonic-xcvrd/tests/test_xcvrd.py @@ -1552,17 +1552,24 @@ def test_SfpStateUpdateTask_handle_port_change_event(self, mock_update_status_hw assert not task.port_mapping.logical_to_asic assert mock_update_status_hw.call_count == 1 - @patch('xcvrd.xcvrd_utilities.port_mapping.subscribe_port_config_change', MagicMock(return_value=(None, None))) def test_SfpStateUpdateTask_task_run_stop(self): - port_mapping = PortMapping() - stop_event = threading.Event() - sfp_error_event = threading.Event() - task = SfpStateUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, sfp_error_event) - task.start() - assert wait_until(5, 1, task.is_alive) - task.raise_exception() - task.join() - assert wait_until(5, 1, lambda: task.is_alive() is False) + def poll_forever(*args, **kwargs): + while True: + time.sleep(1) + # Redefine the XcvrTableHelper function to poll forever so that the task can be stopped by + # raising an exception in between. Also, XcvrTableHelper is the first function to be called after + # starting the task, so having the patch here will avoid the task crashing unexpectedly + # at a different location. + with patch('xcvrd.xcvrd.XcvrTableHelper', new=poll_forever): + port_mapping = PortMapping() + stop_event = threading.Event() + sfp_error_event = threading.Event() + task = SfpStateUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, sfp_error_event) + task.start() + assert wait_until(5, 1, task.is_alive) + task.raise_exception() + task.join() + assert wait_until(5, 1, lambda: task.is_alive() is False) @patch('xcvrd.xcvrd.XcvrTableHelper', MagicMock()) @patch('xcvrd.xcvrd.post_port_sfp_info_to_db')