From d4c04255f429ae22a08600db19212178ee6156bf Mon Sep 17 00:00:00 2001
From: Ihar Hrachyshka <ihrachys@redhat.com>
Date: Fri, 21 Mar 2014 18:57:39 +0100
Subject: [PATCH] Fixed inconsistent EventletContextManagerSpawnTest failures

The test spawns a greenthread and expects it to complete after
eventlet.sleep(0) call. The problem is that if there are other
greenthreads currently running, sleep(0) may switch to unexpected
thread, which may result in test thread not being invoked at all.
Instead of assuming no other green threads are running at the moment
of test execution, explicitly wait() for the test thread.

Change-Id: Id45307860733658638b74ed3a713493df8c1080d
Closes-Bug: #1282706
---
 oslo/messaging/_executors/impl_eventlet.py |  2 ++
 tests/test_executor.py                     | 18 ++++++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/oslo/messaging/_executors/impl_eventlet.py b/oslo/messaging/_executors/impl_eventlet.py
index 229238b80..fb70dff49 100644
--- a/oslo/messaging/_executors/impl_eventlet.py
+++ b/oslo/messaging/_executors/impl_eventlet.py
@@ -57,6 +57,8 @@ def spawn_with(ctxt, pool):
     thread = pool.spawn(callback)
     thread.link(complete, ctxt.__exit__)
 
+    return thread
+
 
 class EventletExecutor(base.ExecutorBase):
 
diff --git a/tests/test_executor.py b/tests/test_executor.py
index b6b043306..509ad0c9d 100644
--- a/tests/test_executor.py
+++ b/tests/test_executor.py
@@ -105,8 +105,8 @@ class EventletContextManagerSpawnTest(test_utils.BaseTestCase):
         self.mgr = context_mgr()
 
     def test_normal_run(self):
-        impl_eventlet.spawn_with(self.mgr, pool=eventlet)
-        eventlet.sleep(0)
+        thread = impl_eventlet.spawn_with(self.mgr, pool=eventlet)
+        thread.wait()
         self.assertEqual(self.before.call_count, 1)
         self.assertEqual(self.callback.call_count, 1)
         self.assertEqual(self.after.call_count, 1)
@@ -114,8 +114,11 @@ class EventletContextManagerSpawnTest(test_utils.BaseTestCase):
 
     def test_excepted_exception(self):
         self.callback.side_effect = ExceptedException
-        impl_eventlet.spawn_with(self.mgr, pool=eventlet)
-        eventlet.sleep(0)
+        thread = impl_eventlet.spawn_with(self.mgr, pool=eventlet)
+        try:
+            thread.wait()
+        except ExceptedException:
+            pass
         self.assertEqual(self.before.call_count, 1)
         self.assertEqual(self.callback.call_count, 1)
         self.assertEqual(self.after.call_count, 1)
@@ -123,8 +126,11 @@ class EventletContextManagerSpawnTest(test_utils.BaseTestCase):
 
     def test_unexcepted_exception(self):
         self.callback.side_effect = Exception
-        impl_eventlet.spawn_with(self.mgr, pool=eventlet)
-        eventlet.sleep(0)
+        thread = impl_eventlet.spawn_with(self.mgr, pool=eventlet)
+        try:
+            thread.wait()
+        except Exception:
+            pass
         self.assertEqual(self.before.call_count, 1)
         self.assertEqual(self.callback.call_count, 1)
         self.assertEqual(self.after.call_count, 0)