2016-09-29 19:44:06 +00:00
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from mock import Mock
|
2016-09-30 16:56:53 +00:00
|
|
|
from trollius import coroutine, Return, get_event_loop, From
|
2016-09-29 19:44:06 +00:00
|
|
|
|
|
|
|
from util.metrics.metricqueue import duration_collector_async
|
|
|
|
|
|
|
|
|
|
|
|
mock_histogram = Mock()
|
|
|
|
|
|
|
|
class NonReturn(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@coroutine
|
|
|
|
@duration_collector_async(mock_histogram, labelvalues=["testlabel"])
|
|
|
|
def duration_decorated():
|
|
|
|
time.sleep(1)
|
|
|
|
raise Return("fin")
|
|
|
|
|
|
|
|
|
|
|
|
@coroutine
|
|
|
|
@duration_collector_async(mock_histogram, labelvalues=["testlabel"])
|
|
|
|
def duration_decorated_error():
|
|
|
|
raise NonReturn("not a Return error")
|
|
|
|
|
2016-09-30 16:56:53 +00:00
|
|
|
@coroutine
|
|
|
|
def calls_decorated():
|
|
|
|
yield From(duration_decorated())
|
|
|
|
|
2016-09-29 19:44:06 +00:00
|
|
|
|
|
|
|
class DurationDecoratorTestCase(unittest.TestCase):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.etcd_client_mock = None
|
|
|
|
self.loop = get_event_loop()
|
|
|
|
super(DurationDecoratorTestCase, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def test_duration_decorator(self):
|
|
|
|
self.loop.run_until_complete(duration_decorated())
|
|
|
|
assert mock_histogram.Observe.called
|
|
|
|
assert 1 - mock_histogram.Observe.call_args[0][0] < 1 # duration should be close to 1s
|
|
|
|
assert mock_histogram.Observe.call_args[1]["labelvalues"] == ["testlabel"]
|
|
|
|
|
|
|
|
def test_duration_decorator_error(self):
|
|
|
|
mock_histogram.reset_mock()
|
|
|
|
|
|
|
|
with self.assertRaises(NonReturn):
|
|
|
|
self.loop.run_until_complete(duration_decorated_error())
|
|
|
|
assert not mock_histogram.Observe.called
|
2016-09-30 16:56:53 +00:00
|
|
|
|
|
|
|
def test_duration_decorator_caller(self):
|
|
|
|
mock_histogram.reset_mock()
|
|
|
|
|
|
|
|
self.loop.run_until_complete(calls_decorated())
|
|
|
|
assert mock_histogram.Observe.called
|
|
|
|
assert 1 - mock_histogram.Observe.call_args[0][0] < 1 # duration should be close to 1s
|
|
|
|
assert mock_histogram.Observe.call_args[1]["labelvalues"] == ["testlabel"]
|
|
|
|
|
2016-09-29 19:44:06 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|