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
This commit is contained in:
Lianhao Lu 2014-06-10 13:15:52 +08:00
parent 3d2723ed7d
commit c12274ca2d

View File

@ -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
)