From f29d8aa11860b0012d0774cb9759c791cd92461e Mon Sep 17 00:00:00 2001 From: Bodo Petermann Date: Mon, 8 Aug 2022 14:44:51 +0200 Subject: [PATCH] Fix sporadic unit test failure TestDriverListener.test_get_listener may fail if the serve_forever thread takes too long to start. Wait for up to 1 sec when checking that serve_forever was called. Story: 2010218 Task: 45965 Change-Id: Ib3f269cbe80222aafb22a36bb09444480a4bbb8d --- .../api/drivers/driver_agent/test_driver_listener.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/octavia/tests/unit/api/drivers/driver_agent/test_driver_listener.py b/octavia/tests/unit/api/drivers/driver_agent/test_driver_listener.py index da0f3db996..3514d0fb2b 100644 --- a/octavia/tests/unit/api/drivers/driver_agent/test_driver_listener.py +++ b/octavia/tests/unit/api/drivers/driver_agent/test_driver_listener.py @@ -13,6 +13,7 @@ # under the License. import errno import socket +import time from unittest import mock from oslo_config import cfg @@ -29,6 +30,13 @@ class TestDriverListener(base.TestCase): def setUp(self): super().setUp() + def _wait_until_called(self, mock_to_be_called): + """Wait for up to 1 sec for the given mock to be called.""" + for _ in range(10): + if mock_to_be_called.call_count > 0: + break + time.sleep(.1) + @mock.patch('octavia.api.drivers.driver_agent.driver_listener.memoryview') def test_recv(self, mock_memoryview): # TEST_STRING len() is 15 @@ -308,6 +316,7 @@ class TestDriverListener(base.TestCase): mock_exit_event.is_set.side_effect = [False, False, False, False, True] driver_listener.status_listener(mock_exit_event) + self._wait_until_called(mock_server.serve_forever) mock_server.serve_forever.assert_called() self.assertEqual(2, mock_cleanup.call_count) @@ -327,6 +336,7 @@ class TestDriverListener(base.TestCase): mock_exit_event.is_set.side_effect = [False, False, False, False, True] driver_listener.stats_listener(mock_exit_event) + self._wait_until_called(mock_server.serve_forever) mock_server.serve_forever.assert_called() @mock.patch('octavia.api.drivers.driver_agent.driver_listener.' @@ -345,4 +355,5 @@ class TestDriverListener(base.TestCase): mock_exit_event.is_set.side_effect = [False, False, False, False, True] driver_listener.get_listener(mock_exit_event) + self._wait_until_called(mock_server.serve_forever) mock_server.serve_forever.assert_called()