Example #1
0
    def test_bad_units(self):
        self.metric.units = metrics.MetricsDataUnits.GIBIBYTES
        with self.assertRaises(AssertionError):
            helpers.ScopedMeasureTime(self.metric)

        self.metric.units = ''
        with self.assertRaises(AssertionError):
            helpers.ScopedMeasureTime(self.metric)
Example #2
0
 def test_custom_status_and_exception(self):
     with self.assertRaises(_CustomException):
         with helpers.ScopedMeasureTime(self.metric,
                                        time_fn=self.time_fn) as sd:
             sd.set_status('foo')
             raise _CustomException()
     self.metric.add.assert_called_once_with(0.25, {'status': 'foo'})
Example #3
0
 def test_custom_success(self):
   self.metric.field_spec = [metrics.StringField('label')]
   self.metric.units = metrics.MetricsDataUnits.MILLISECONDS
   with helpers.ScopedMeasureTime(self.metric, 'label', 'ok', 'fail',
                                  time_fn=self.time_fn):
     pass
   self.metric.add.assert_called_once_with(250.0, {'label': 'ok'})
Example #4
0
 def test_custom_exception(self):
   self.metric.field_spec = [metrics.StringField('label')]
   self.metric.units = metrics.MetricsDataUnits.MICROSECONDS
   with self.assertRaises(_CustomException):
     with helpers.ScopedMeasureTime(self.metric, 'label', 'ok', 'fail',
                                    time_fn=self.time_fn):
       raise _CustomException()
   self.metric.add.assert_called_once_with(250000.0, {'label': 'fail'})
Example #5
0
 def test_extra_fields(self):
   self.metric.field_spec = [metrics.StringField('custom'),
                             metrics.StringField('type')]
   with helpers.ScopedMeasureTime(self.metric, 'custom', 'ok', 'fail',
                                  extra_fields_values={'type': 'normal'},
                                  time_fn=self.time_fn):
     pass
   self.metric.add.assert_called_once_with(0.25, {'custom': 'ok',
                                                  'type': 'normal'})
Example #6
0
 def test_exception(self):
     with self.assertRaises(_CustomException):
         with helpers.ScopedMeasureTime(self.metric, time_fn=self.time_fn):
             raise _CustomException()
     self.metric.add.assert_called_once_with(0.25, {'status': 'failure'})
Example #7
0
 def test_success(self):
     with helpers.ScopedMeasureTime(self.metric, time_fn=self.time_fn):
         pass
     self.metric.add.assert_called_once_with(0.25, {'status': 'success'})
Example #8
0
 def test_wrong_field(self):
     self.metric.field_spec = [metrics.StringField('wrong')]
     with self.assertRaises(AssertionError):
         helpers.ScopedMeasureTime(self.metric, 'status')
Example #9
0
 def test_multiple_custom_status_calls(self):
     with helpers.ScopedMeasureTime(self.metric,
                                    time_fn=self.time_fn) as sd:
         sd.set_status('foo')
         sd.set_status('bar')
     self.metric.add.assert_called_once_with(0.25, {'status': 'bar'})
Example #10
0
 def test_set_failure(self):
     with helpers.ScopedMeasureTime(self.metric,
                                    time_fn=self.time_fn) as sd:
         sd.set_failure()
     self.metric.add.assert_called_once_with(0.25, {'status': 'failure'})
Example #11
0
 def test_extra_fields_should_exclude_status(self):
   with self.assertRaises(AssertionError):
     helpers.ScopedMeasureTime(self.metric,
                               extra_fields_values={'status': 'x'})