From a7f01d96838feeb86b61a646942f52ecb1453786 Mon Sep 17 00:00:00 2001
From: Mark McLoughlin <markmc@redhat.com>
Date: Fri, 27 Jun 2014 06:50:39 +0100
Subject: [PATCH] Fix slow notification listener tests

The notification listener tests have a listener endpoint that will stop
the listener thread when a certain number of messages have been
received. If this doesn't work, the main thread will time out its
join() after 5 seconds and the only way sign of this is that the test
took over 5 seconds.

Let's make stop_listener() return a boolean indicating whether the the
thread has stopped or we timed out, and assert that the thread has
stopped.

Fix a few cases where the thread hasn't stopped. In the 'two exchanges'
case, we send three messages but only two are to the exchanges we are
listening on. In the tests using the HANDLED return value, the info()
handler which stops the thread was never been called because that
endpoint was last in the endpoints list.

Change-Id: Iea2486f23cee073c3891188d7aabb22ae945900d
---
 tests/notify/test_listener.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/tests/notify/test_listener.py b/tests/notify/test_listener.py
index 3219f8d7a..cd0e4c448 100644
--- a/tests/notify/test_listener.py
+++ b/tests/notify/test_listener.py
@@ -33,7 +33,7 @@ class ListenerSetupMixin(object):
             self._expect_messages = expect_messages
             self._received_msgs = 0
             self._listener = messaging.get_notification_listener(
-                transport, targets, endpoints + [self], allow_requeue=True)
+                transport, targets, [self] + endpoints, allow_requeue=True)
 
         def info(self, ctxt, publisher_id, event_type, payload, metadata):
             self._received_msgs += 1
@@ -61,6 +61,7 @@ class ListenerSetupMixin(object):
 
     def _stop_listener(self, thread):
         thread.join(timeout=5)
+        return thread.isAlive()
 
     def _setup_notifier(self, transport, topic='testtopic',
                         publisher_id='testpublisher'):
@@ -128,7 +129,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
         notifier = self._setup_notifier(transport)
         notifier.info({}, 'an_event.start', 'test message')
 
-        self._stop_listener(listener_thread)
+        self.assertFalse(self._stop_listener(listener_thread))
 
         endpoint.info.assert_called_once_with(
             {}, 'testpublisher', 'an_event.start', 'test message',
@@ -148,7 +149,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
         notifier = self._setup_notifier(transport, topic='topic2')
         notifier.info({'ctxt': '2'}, 'an_event.start2', 'test')
 
-        self._stop_listener(listener_thread)
+        self.assertFalse(self._stop_listener(listener_thread))
 
         endpoint.info.assert_has_calls([
             mock.call({'ctxt': '1'}, 'testpublisher',
@@ -168,7 +169,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
                                     exchange="exchange1"),
                    messaging.Target(topic="topic",
                                     exchange="exchange2")]
-        listener_thread = self._setup_listener(transport, [endpoint], 3,
+        listener_thread = self._setup_listener(transport, [endpoint], 2,
                                                targets=targets)
 
         notifier = self._setup_notifier(transport, topic="topic")
@@ -191,7 +192,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
         notifier.info({'ctxt': '2'},
                       'an_event.start', 'test message exchange2')
 
-        self._stop_listener(listener_thread)
+        self.assertFalse(self._stop_listener(listener_thread))
 
         endpoint.info.assert_has_calls([
             mock.call({'ctxt': '1'}, 'testpublisher', 'an_event.start',
@@ -214,7 +215,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
         notifier = self._setup_notifier(transport)
         notifier.info({}, 'an_event.start', 'test')
 
-        self._stop_listener(listener_thread)
+        self.assertFalse(self._stop_listener(listener_thread))
 
         endpoint1.info.assert_called_once_with(
             {}, 'testpublisher', 'an_event.start', 'test', {
@@ -242,7 +243,7 @@ class TestNotifyListener(test_utils.BaseTestCase, ListenerSetupMixin):
         notifier = self._setup_notifier(transport)
         notifier.info({}, 'an_event.start', 'test')
 
-        self._stop_listener(listener_thread)
+        self.assertFalse(self._stop_listener(listener_thread))
 
         endpoint.info.assert_has_calls([
             mock.call({}, 'testpublisher', 'an_event.start', 'test',