Fix while loop in worker shutdown unit test
Change the while loop into a for loop to ensure the test exit after set time.
from bson.errors import InvalidStringData
import eventlet
import mock
+import os
from oslo_config import cfg
import tempfile
@@ -92,21 +93,29 @@ class WorkerTestCase(DbTestCase):
def test_worker_shutdown(self):
action_worker = actions_worker.get_worker()
+ temp_file = None
# Create a temporary file that is deleted when the file is closed and then set up an
# action to wait for this file to be deleted. This allows this test to run the action
# over a separate thread, run the shutdown sequence on the main thread, and then let
# the local runner to exit gracefully and allow _run_action to finish execution.
with tempfile.NamedTemporaryFile() as fp:
- params = {'cmd': 'while [ -e \'%s\' ]; do sleep 0.1; done' % fp.name}
+ temp_file = fp.name
+ self.assertIsNotNone(temp_file)
+ self.assertTrue(os.path.isfile(temp_file))
+
+ # Launch the action execution in a separate thread.
+ params = {'cmd': 'while [ -e \'%s\' ]; do sleep 0.1; done' % temp_file}
liveaction_db = self._get_liveaction_model(WorkerTestCase.local_action_db, params)
liveaction_db = LiveAction.add_or_update(liveaction_db)
executions.create_execution_object(liveaction_db)
runner_thread = eventlet.spawn(action_worker._run_action, liveaction_db)
- # Wait for the worker to add the liveaction to _running_liveactions.
- while len(action_worker._running_liveactions) <= 0:
+ # Wait for the worker up to 10s to add the liveaction to _running_liveactions.
+ for i in range(0, int(10 / 0.1)):
eventlet.sleep(0.1)
+ if len(action_worker._running_liveactions) > 0:
+ break
self.assertEqual(len(action_worker._running_liveactions), 1)
@@ -118,6 +127,9 @@ class WorkerTestCase(DbTestCase):
self.assertEqual(len(action_worker._running_liveactions), 0)
self.assertEqual(liveaction_db.status, action_constants.LIVEACTION_STATUS_ABANDONED)
+ # Make sure the temporary file has been deleted.
+ self.assertFalse(os.path.isfile(temp_file))
+
# Wait for the local runner to complete. This will activate the finally block in
# _run_action but will not result in KeyError because the discard method is used to
# to remove the liveaction from _running_liveactions.
