From c12274ca2dfd270919608001e5bc5ab55ccfde55 Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Tue, 10 Jun 2014 13:15:52 +0800 Subject: [PATCH] Fixed unit test TestRealNotification The way how the unit test test_notification.TestRealNotification.test_notification_service was constructed would sometimes let the main test case check the result even before all the expected notification listeners finish processing. This would cause failure in gate test. We should make sure the test case check the result after all the expected listeners finish processing the notification. Change-Id: I5333d83365fc39264b1002030494556f90319312 Closes-Bug: #1327344 --- ceilometer/tests/test_notification.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ceilometer/tests/test_notification.py b/ceilometer/tests/test_notification.py index 4d7d8a88a..a4982e6ae 100644 --- a/ceilometer/tests/test_notification.py +++ b/ceilometer/tests/test_notification.py @@ -17,6 +17,7 @@ # under the License. """Tests for Ceilometer notify daemon.""" +import eventlet.semaphore import mock import oslo.messaging @@ -173,10 +174,14 @@ class TestRealNotification(tests_base.BaseTestCase): pipeline = yaml.dump([{ 'name': 'test_pipeline', 'interval': 5, - 'counters': ['*'], + 'counters': ['instance', 'memory'], 'transformers': [], 'publishers': ['test://'], }]) + + self.expected_samples = 2 + self.sem = eventlet.semaphore.Semaphore(0) + pipeline_cfg_file = fileutils.write_to_tempfile(content=pipeline, prefix="pipeline", suffix="yaml") @@ -194,13 +199,17 @@ class TestRealNotification(tests_base.BaseTestCase): fake_publisher = fake_publisher_cls.return_value fake_publisher.publish_samples.side_effect = \ - lambda *args: self.srv.stop() + lambda *args: self.sem.release() notifier = messaging.get_notifier("compute.vagrant-precise") notifier.info(context.RequestContext(), 'compute.instance.create.end', TEST_NOTICE_PAYLOAD) - - self.srv.listeners[0].wait() + # we should wait all the expected notification listeners finished + # processing the notification + for i in range(self.expected_samples): + self.sem.acquire(timeout=30) + # stop NotificationService + self.srv.stop() class SamplesMatcher(object): def __eq__(self, samples): @@ -209,10 +218,6 @@ class TestRealNotification(tests_base.BaseTestCase): return False return True - fake_publisher.publish_samples.assert_has_calls([ - mock.call(mock.ANY, SamplesMatcher()), - mock.call(mock.ANY, SamplesMatcher()), - mock.call(mock.ANY, SamplesMatcher()), - mock.call(mock.ANY, SamplesMatcher()), - mock.call(mock.ANY, SamplesMatcher()), - ]) + fake_publisher.publish_samples.assert_has_calls( + [mock.call(mock.ANY, SamplesMatcher())] * self.expected_samples + )