def testNoneEndsProcess(self):
        """Putting None on the Queue should immediately end the consumption loop."""
        q = Queue.Queue()
        q.put(None)

        ts_mon_config._ConsumeMessages(q, [''], {})

        ts_mon_config.SetupTsMonGlobalState.assert_called_once_with(
            '', auto_flush=False)
        ts_mon_config.time.time.assert_not_called()
        ts_mon_config.metrics.Flush.assert_not_called()
Ejemplo n.º 2
0
  def testConsumeOneMetric(self):
    """Tests that sending one metric calls flush once."""
    q = Queue.Queue()
    mock_metric = mock.Mock()
    q.put((mock_metric, 'mock_name', ['arg1'], {'kwarg1': 'value'}))
    q.put(None)

    ts_mon_config._ConsumeMessages(q, [''], {})

    self.assertEqual(2, ts_mon_config.time.time.call_count)
    ts_mon_config.time.sleep.assert_called_once_with(
        ts_mon_config.FLUSH_INTERVAL - 1)
    ts_mon_config.metrics.flush.assert_called_once_with()
    mock_metric.mock_name.assert_called_once_with('arg1', kwarg1='value')
    def testConsumeOneMetric(self):
        """Tests that sending one metric calls flush once."""
        q = Queue.Queue()
        q.put(
            metrics.MetricCall('Boolean', [], {}, 'mock_name', ['arg1'],
                               {'kwarg1': 'value'}, False))
        q.put(None)

        ts_mon_config._ConsumeMessages(q, [''], {})

        self.assertEqual(2, ts_mon_config.time.time.call_count)
        ts_mon_config.time.sleep.assert_called_once_with(
            ts_mon_config.FLUSH_INTERVAL - 1)
        ts_mon_config.metrics.Flush.assert_called_once_with(reset_after=[])
        self.mock_metric.return_value.mock_name.assert_called_once_with(
            'arg1', kwarg1='value')
    def testResetAfter(self):
        """Tests that metrics with reset_after set are cleared after."""
        q = Queue.Queue()

        q.put(
            metrics.MetricCall('Boolean', [], {},
                               'mock_name', ['arg1'], {'kwarg1': 'value1'},
                               reset_after=True))
        q.put(None)

        ts_mon_config._ConsumeMessages(q, [''], {})

        self.assertEqual(
            [self.mock_metric.return_value],
            ts_mon_config.metrics.Flush.call_args[1]['reset_after'])
        self.mock_metric.return_value.mock_name.assert_called_once_with(
            'arg1', kwarg1='value1')
Ejemplo n.º 5
0
  def testCatchesException(self):
    """Tests that the _ConsumeMessages loop catches exceptions."""
    q = Queue.Queue()
    class RaisesException(object):
      """Class to raise an exception"""
      @staticmethod
      def raiseException(*_args, **_kwargs):
        raise Exception()
    q.put((RaisesException, 'raiseException', ['arg1'], {'kwarg1': 'value1'}))
    q.put(None)

    mock_logging = self.PatchObject(ts_mon_config.logging, 'exception')

    ts_mon_config._ConsumeMessages(q, [''], {})

    self.assertEqual(1, mock_logging.call_count)
    self.assertEqual(2, ts_mon_config.time.time.call_count)
    ts_mon_config.time.sleep.assert_called_once_with(
        ts_mon_config.FLUSH_INTERVAL - 1)
    ts_mon_config.metrics.flush.assert_called_once_with()
    def testCatchesException(self):
        """Tests that the _ConsumeMessages loop catches exceptions."""
        q = Queue.Queue()

        class RaisesException(object):
            """Class to raise an exception"""
            def raiseException(self, *_args, **_kwargs):
                raise Exception()

        metrics.RaisesException = RaisesException
        q.put(
            metrics.MetricCall('RaisesException', [], {}, 'raiseException',
                               ['arg1'], {'kwarg1': 'value1'}, False))
        q.put(None)

        mock_logging = self.PatchObject(ts_mon_config.logging, 'exception')

        ts_mon_config._ConsumeMessages(q, [''], {})

        self.assertEqual(1, mock_logging.call_count)
        self.assertEqual(2, ts_mon_config.time.time.call_count)
        ts_mon_config.time.sleep.assert_called_once_with(
            ts_mon_config.FLUSH_INTERVAL - 1)
        ts_mon_config.metrics.Flush.assert_called_once_with(reset_after=[])