Merge "[ThreadGroup] Don't remove timer when stop timers"

This commit is contained in:
Zuul 2018-05-10 20:03:29 +00:00 committed by Gerrit Code Review
commit 6d82217fbe
2 changed files with 14 additions and 4 deletions

View File

@ -80,6 +80,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time < 1)
self.assertEqual(0, len(self.tg.timers))
def test_stop_gracefully(self):
@ -92,6 +93,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time >= 1)
self.assertEqual(0, len(self.tg.timers))
def test_cancel_early(self):

View File

@ -125,9 +125,11 @@ class ThreadGroup(object):
lambda x: x.stop(),
lambda x: LOG.exception('Error stopping thread.'))
def stop_timers(self):
def stop_timers(self, wait=False):
for timer in self.timers:
timer.stop()
if wait:
self._wait_timers()
self.timers = []
def stop(self, graceful=False):
@ -137,17 +139,17 @@ class ThreadGroup(object):
Never kill threads.
* In case of graceful=False, kill threads immediately.
"""
self.stop_timers()
self.stop_timers(wait=graceful)
if graceful:
# In case of graceful=True, wait for all threads to be
# finished, never kill threads
self.wait()
self._wait_threads()
else:
# In case of graceful=False(Default), kill threads
# immediately
self._stop_threads()
def wait(self):
def _wait_timers(self):
for x in self.timers:
try:
x.wait()
@ -156,10 +158,16 @@ class ThreadGroup(object):
pass
except Exception:
LOG.exception('Error waiting on timer.')
def _wait_threads(self):
self._perform_action_on_threads(
lambda x: x.wait(),
lambda x: LOG.exception('Error waiting on thread.'))
def wait(self):
self._wait_timers()
self._wait_threads()
def _any_threads_alive(self):
current = threading.current_thread()
for x in self.threads[:]: