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

Because x is a custom Thread instance, and threading.current_thread()
returns a GreenThread instance, they will never be a same instance.

This patch add one more property method for Thread class to identify itself.

Change-Id: I173a0a40b417d613cd270d42e40295075cd5b71f
Signed-off-by: apporc <appleorchard2000@gmail.com>
This commit is contained in:
apporc 2015-09-06 12:20:52 +08:00
parent f5d273858c
commit 9e81bbae05
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()