Skip to content

Commit

Permalink
No need for optional publisher in publish_metrics().
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Apr 9, 2014
1 parent 96a0fff commit 2274401
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
14 changes: 7 additions & 7 deletions vumi/blinkenlights/metrics.py
Expand Up @@ -79,24 +79,24 @@ def stop(self):
self._task.stop()
self._task = None

def publish_metrics(self, publisher=None):
def publish_metrics(self):
"""
Publish all waiting metrics.
If a publisher is provided, it will be used instead of the manager's
publisher.
"""
msg = MetricMessage()
self._collect_oneshot_metrics(msg)
self._collect_polled_metrics(msg)
if publisher is None:
publisher = self._publisher
publisher.publish_message(msg)
self.publish_message(msg)
if self._on_publish is not None:
self._on_publish(self)

_publish_metrics = publish_metrics # For old tests that poke this.

def publish_message(self, msg):
if self._publisher is None:
raise ValueError("No publisher available.")
self._publisher.publish_message(msg)

def _collect_oneshot_metrics(self, msg):
oneshots, self._oneshot_msgs = self._oneshot_msgs, []
for metric, values in oneshots:
Expand Down
21 changes: 3 additions & 18 deletions vumi/blinkenlights/tests/test_metrics.py
Expand Up @@ -114,7 +114,7 @@ def test_start_manager_no_publisher_no_channel(self):
mm = metrics.MetricManager("vumi.test.")
self.assertEqual(mm._publisher, None)
self.assertEqual(mm._task, None)
self.assertRaises(Exception, mm.start)
self.assertRaises(RuntimeError, mm.start)

@inlineCallbacks
def test_start_manager_publisher_and_channel(self):
Expand All @@ -123,7 +123,7 @@ def test_start_manager_publisher_and_channel(self):
self.assertEqual(mm._publisher, publisher)
self.assertEqual(mm._task, None)
channel = yield get_stubbed_channel(self.worker_helper.broker)
self.assertRaises(Exception, mm.start, channel)
self.assertRaises(RuntimeError, mm.start, channel)

def test_start_manager_publisher_no_channel(self):
publisher = metrics.MetricPublisher()
Expand Down Expand Up @@ -218,22 +218,7 @@ def test_publish_metrics_not_started_no_publisher(self):
mm = metrics.MetricManager("vumi.test.")
self.assertEqual(mm._publisher, None)
mm.oneshot(metrics.Count("my.count"), 1)
self.assertRaises(Exception, mm.publish_metrics)

@inlineCallbacks
def test_publish_metrics_not_started_with_publisher(self):
mm = metrics.MetricManager("vumi.test.", 0.1, self.on_publish)
publisher = metrics.MetricPublisher()
channel = yield get_stubbed_channel(self.worker_helper.broker)
publisher.start(channel)
cnt = metrics.Count("my.count")

self.assertEqual(mm._publisher, None)
mm.oneshot(cnt, 1)
self.assertEqual(len(mm._oneshot_msgs), 1)
mm.publish_metrics(publisher)
self.assertEqual(mm._oneshot_msgs, [])
self._check_msg(mm, cnt, [1])
self.assertRaises(ValueError, mm.publish_metrics)

def test_stop_unstarted(self):
mm = metrics.MetricManager("vumi.test.", 0.1, self.on_publish)
Expand Down

0 comments on commit 2274401

Please sign in to comment.