def testSubprocessQuitsWhenNotSetup(self): self.PatchObject(ts_mon_config.logging, 'exception') self.PatchObject(ts_mon_config, '_WasSetup', False) ts_mon_config._SetupAndConsumeMessages(None, DEFAULT_OPTIONS) self.assertEqual(False, ts_mon_config._WasSetup) # The entry should not have been consumed by _ConsumeMessages self.assertEqual(0, ts_mon_config.logging.exception.call_count)
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 testNoneEndsProcess(self): """Putting None on the Queue should immediately end the consumption loop.""" q = Queue.Queue() q.put(None) ts_mon_config._SetupAndConsumeMessages(q, DEFAULT_OPTIONS) ts_mon_config._SetupTsMonFromOptions.assert_called_once_with( DEFAULT_OPTIONS, suppress_exception=True) self.assertFalse(ts_mon_config.time.time.called) self.assertFalse(ts_mon_config.metrics.Flush.called)
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')
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 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._SetupAndConsumeMessages(q, DEFAULT_OPTIONS) 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')