Esempio n. 1
0
  def testCatchesException(self):
    """Tests that the _SetupAndConsumeMessages 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)

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

    ts_mon_config._SetupAndConsumeMessages(q, DEFAULT_OPTIONS)

    self.assertEqual(1, exception_log.call_count)
    # time.time is called once because we check if we need to Flush() before
    # receiving the None message.
    self.assertEqual(1, ts_mon_config.time.time.call_count)
    self.assertEqual(0, ts_mon_config.time.sleep.call_count)
    self.assertEqual(0, ts_mon_config.metrics.Flush.call_count)
    def testConsumeTwoMetrics(self):
        """Tests that sending two metrics only calls flush once."""
        q = Queue.Queue()
        q.put(
            metrics.MetricCall('Boolean', [], {}, 'mock_name1', ['arg1'],
                               {'kwarg1': 'value'}, False))
        q.put(
            metrics.MetricCall('Boolean', [], {}, 'mock_name2', ['arg2'],
                               {'kwarg2': 'value'}, False))
        q.put(None)

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

        self.assertEqual(3, ts_mon_config.time.time.call_count)
        ts_mon_config.time.sleep.assert_called_once_with(
            ts_mon_config.FLUSH_INTERVAL - 2)
        ts_mon_config.metrics.Flush.assert_called_once_with(reset_after=[])
        self.mock_metric.return_value.mock_name1.assert_called_once_with(
            'arg1', kwarg1='value')
        self.mock_metric.return_value.mock_name2.assert_called_once_with(
            'arg2', kwarg2='value')
Esempio n. 3
0
 def testSetOnceMetricKeepsEmitting(self):
   """Tests that a metric which is set once emits many times if left alone."""
   self.PatchObject(ts_mon_config, 'FLUSH_INTERVAL', 0)
   self.time_mock.time.side_effect = [1, 2, 3, 4, 5]
   q = Queue.Queue()
   q.put(metrics.MetricCall('Boolean', [], {},
                            '__class__', [], {},
                            False))
   try:
     ts_mon_config.MetricConsumer(q).Consume()
   except StopIteration:
     pass # No more time calls left.
   self.assertEqual(self.flush_mock.call_count, 5)
Esempio n. 4
0
  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._SetupAndConsumeMessages(q, DEFAULT_OPTIONS)

    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', fields=self.common_metric_fields, kwarg1='value')
Esempio n. 5
0
  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._SetupAndConsumeMessages(q, DEFAULT_OPTIONS)

    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', fields=self.common_metric_fields, kwarg1='value1')
    def testEnqueue(self):
        """Test that _Indirect enqueues messages correctly."""
        metric = metrics.Boolean

        with parallel.Manager() as manager:
            q = manager.Queue()
            self.PatchObject(metrics, 'MESSAGE_QUEUE', q)

            proxy_metric = metric('foo')
            proxy_metric.example('arg1', 'arg2')

            message = q.get(timeout=10)

        self.assertEqual(
            message,
            metrics.MetricCall(metric.__name__, ('foo', ), {}, 'example',
                               ('arg1', 'arg2'), {}, False))
Esempio n. 7
0
  def testFlushingProcessExits(self):
    """Tests that _CreateTsMonFlushingProcess cleans up the process."""
    processes = []
    original_process_function = multiprocessing.Process
    def SaveProcess(*args, **kwargs):
      p = original_process_function(*args, **kwargs)
      processes.append(p)
      return p

    self.PatchObject(multiprocessing, 'Process', SaveProcess)

    with ts_mon_config._CreateTsMonFlushingProcess(DEFAULT_OPTIONS) as q:
      q.put(metrics.MetricCall('Boolean', [], {},
                               '__class__', [], {},
                               False))

    # wait a bit for the process to close, since multiprocessing.Queue and
    # Process.join() is not synchronous.
    processes[0].join(5)

    self.assertEqual(0, processes[0].exitcode)
Esempio n. 8
0
  def testEnqueue(self):
    """Test that _Indirect enqueues messages correctly."""
    metric = metrics.Boolean

    with parallel.Manager() as manager:
      q = manager.Queue()
      self.PatchObject(metrics, 'MESSAGE_QUEUE', q)

      proxy_metric = metric('foo')
      proxy_metric.example('arg1', {'field_name': 'value'})

      message = q.get(timeout=10)

    expected_metric_kwargs = {
        'field_spec': [ts_mon.StringField('field_name')],
        'description': 'No description.',
    }
    self.assertEqual(
        message,
        metrics.MetricCall(metric.__name__, ('foo',), expected_metric_kwargs,
                           'example', ('arg1', {'field_name': 'value'}), {},
                           False))
    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=[])