From 0f6c8e620d05f8602c62bffb493e35ca7982f111 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 17 Jul 2018 16:54:04 -0400 Subject: [PATCH] Move metrics test to pytest --- test/test_metricqueue.py | 62 --------------------------- util/metrics/test/test_metricqueue.py | 58 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 62 deletions(-) delete mode 100644 test/test_metricqueue.py create mode 100644 util/metrics/test/test_metricqueue.py diff --git a/test/test_metricqueue.py b/test/test_metricqueue.py deleted file mode 100644 index 0a8247fb4..000000000 --- a/test/test_metricqueue.py +++ /dev/null @@ -1,62 +0,0 @@ -import time -import unittest - -from mock import Mock -from trollius import coroutine, Return, get_event_loop, From - -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") - -@coroutine -def calls_decorated(): - yield From(duration_decorated()) - - -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 - - 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"] - - -if __name__ == '__main__': - unittest.main() diff --git a/util/metrics/test/test_metricqueue.py b/util/metrics/test/test_metricqueue.py new file mode 100644 index 000000000..62f1665d9 --- /dev/null +++ b/util/metrics/test/test_metricqueue.py @@ -0,0 +1,58 @@ +import time + +import pytest + +from mock import Mock +from trollius import coroutine, Return, get_event_loop, From + +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") + +@coroutine +def calls_decorated(): + yield From(duration_decorated()) + + +def test_duration_decorator(): + loop = get_event_loop() + 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(): + loop = get_event_loop() + mock_histogram.reset_mock() + + with pytest.raises(NonReturn): + loop.run_until_complete(duration_decorated_error()) + assert not mock_histogram.Observe.called + + +def test_duration_decorator_caller(): + mock_histogram.reset_mock() + + loop = get_event_loop() + 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"]