From da760973be09a98a1fc07c519e74a11899cb4aa5 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 1 Jun 2021 07:44:33 +0000 Subject: [PATCH] Use "multiprocessing.Queue" for "TestNeutronServer" related tests Instead of using a file to log the processes status and actions, "TestNeutronServer" now uses a "multiprocessing.Queue" that is safer than writting a single file accross multiple processes. Change-Id: I6d04df180cd9b2d593bb99c8d22a60a3534f22a0 Closes-Bug: #1930367 --- neutron/tests/functional/test_server.py | 50 ++++++++++--------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/neutron/tests/functional/test_server.py b/neutron/tests/functional/test_server.py index cfc9fe2acff..4a04f439832 100644 --- a/neutron/tests/functional/test_server.py +++ b/neutron/tests/functional/test_server.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import multiprocessing import os import signal import socket @@ -40,8 +41,8 @@ CONF = cfg.CONF # Those messages will be written to temporary file each time # start/reset methods are called. -FAKE_START_MSG = b"start" -FAKE_RESET_MSG = b"reset" +FAKE_START_MSG = 'start' +FAKE_RESET_MSG = 'reset' TARGET_PLUGIN = 'neutron.plugins.ml2.plugin.Ml2Plugin' @@ -51,7 +52,7 @@ class TestNeutronServer(base.BaseLoggingTestCase): super(TestNeutronServer, self).setUp() self.service_pid = None self.workers = None - self.temp_file = self.get_temp_file_path("test_server.tmp") + self._mp_queue = multiprocessing.Queue() self.health_checker = self._check_active self.pipein, self.pipeout = os.pipe() self.addCleanup(self._destroy_workers) @@ -132,12 +133,10 @@ class TestNeutronServer(base.BaseLoggingTestCase): return True def _fake_start(self): - with open(self.temp_file, 'ab') as f: - f.write(FAKE_START_MSG) + self._mp_queue.put(FAKE_START_MSG) def _fake_reset(self): - with open(self.temp_file, 'ab') as f: - f.write(FAKE_RESET_MSG) + self._mp_queue.put(FAKE_RESET_MSG) def _test_restart_service_on_sighup(self, service, workers=1): """Test that a service correctly (re)starts on receiving SIGHUP. @@ -159,38 +158,27 @@ class TestNeutronServer(base.BaseLoggingTestCase): # Wait for temp file to be created and its size reaching the expected # value expected_size = len(expected_msg) + ret_msg = '' - def is_temp_file_ok(): - LOG.debug("Checking file %s", self.temp_file) - if not os.path.isfile(self.temp_file): - LOG.debug("File %s not exists.", self.temp_file) - return False - temp_file_size = os.stat(self.temp_file).st_size - LOG.debug("Size of file %s is %s. Expected size: %s", - self.temp_file, temp_file_size, expected_size) - return temp_file_size == expected_size + def is_ret_buffer_ok(): + nonlocal ret_msg + LOG.debug('Checking returned buffer size') + while not self._mp_queue.empty(): + ret_msg += self._mp_queue.get() + LOG.debug('Size of buffer is %s. Expected size: %s', + len(ret_msg), expected_size) + return len(ret_msg) == expected_size try: - utils.wait_until_true(is_temp_file_ok, timeout=5, sleep=1) + utils.wait_until_true(is_ret_buffer_ok, timeout=5, sleep=1) except utils.WaitTimeout: - if not os.path.isfile(self.temp_file): - raise RuntimeError( - "Timed out waiting for file %(filename)s to be created" % - {'filename': self.temp_file}) - else: - raise RuntimeError( - "Expected size for file %(filename)s: %(size)s, current " - "size: %(current_size)s" % - {'filename': self.temp_file, - 'size': expected_size, - 'current_size': os.stat(self.temp_file).st_size}) + raise RuntimeError('Expected buffer size: %s, current size: %s' % + (len(ret_msg), expected_size)) # Verify that start has been called twice for each worker (one for # initial start, and the second one on SIGHUP after children were # terminated). - with open(self.temp_file, 'rb') as f: - res = f.readline() - self.assertEqual(expected_msg, res) + self.assertEqual(expected_msg, ret_msg) class TestWsgiServer(TestNeutronServer):