Merge "ThreadGroup's stop didn't recognise the current thread correctly"

This commit is contained in:
Jenkins 2015-09-25 09:20:39 +00:00 committed by Gerrit Code Review
commit ec697e0235
2 changed files with 28 additions and 2 deletions

View File

@ -19,6 +19,8 @@ Unit Tests for thread groups
import time
from eventlet import event
from oslotest import base as test_base
from oslo_service import threadgroup
@ -47,6 +49,25 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
self.assertEqual(('arg',), timer.args)
self.assertEqual({'kwarg': 'kwarg'}, timer.kw)
def test_stop_current_thread(self):
stop_event = event.Event()
quit_event = event.Event()
def stop_self(*args, **kwargs):
if args[0] == 1:
time.sleep(1)
self.tg.stop()
stop_event.send('stop_event')
quit_event.wait()
for i in range(0, 4):
self.tg.add_thread(stop_self, i, kwargs='kwargs')
stop_event.wait()
self.assertEqual(1, len(self.tg.threads))
quit_event.send('quit_event')
def test_stop_immediately(self):
def foo(*args, **kwargs):

View File

@ -43,6 +43,11 @@ class Thread(object):
def __init__(self, thread, group):
self.thread = thread
self.thread.link(_thread_done, group=group, thread=self)
self._ident = id(thread)
@property
def ident(self):
return self._ident
def stop(self):
self.thread.kill()
@ -100,7 +105,7 @@ class ThreadGroup(object):
# Iterate over a copy of self.threads so thread_done doesn't
# modify the list while we're iterating
for x in self.threads[:]:
if x is current:
if x.ident == current.ident:
# don't kill the current thread.
continue
try:
@ -148,7 +153,7 @@ class ThreadGroup(object):
# Iterate over a copy of self.threads so thread_done doesn't
# modify the list while we're iterating
for x in self.threads[:]:
if x is current:
if x.ident == current.ident:
continue
try:
x.wait()