~landscape/txstatsd/trunk

« back to all changes in this revision

Viewing changes to txstatsd/tests/test_metrics.py

  • Committer: Sidnei da Silva
  • Date: 2011-07-27 20:35:23 UTC
  • mfrom: (11.2.6 txstatsd)
  • Revision ID: sidnei.da.silva@canonical.com-20110727203523-mmhr5clzkfyqva8f
- Merge from lp:txstatsd

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Tests for metrics context manager."""
2
 
 
3
 
from mocker import ANY, MockerTestCase
4
 
from txstatsd.metrics import Measure
5
 
 
6
 
 
7
 
class MeasureTest(MockerTestCase):
8
 
    """Test case for the L{Measure} context manager."""
9
 
 
10
 
    def test_measure(self):
11
 
        """Basic test."""
12
 
        operation_name = 'fake_function'
13
 
        meter = self.mocker.mock()
14
 
        meter.increment(operation_name)
15
 
        meter.timing(operation_name, ANY)
16
 
        meter.increment(operation_name + '.done')
17
 
        meter.decrement(operation_name)
18
 
        self.mocker.replay()
19
 
        result = []
20
 
 
21
 
        def fake_function():
22
 
            """Useles method."""
23
 
            result.append(0)
24
 
 
25
 
        with Measure('test_prefix', 'fake_function', meter):
26
 
            fake_function()
27
 
        self.assertEqual([0], result)
28
 
 
29
 
    def test_measure_timing(self):
30
 
        """Test the timing works."""
31
 
        operation_name = 'fake_function'
32
 
        MockTime = self.mocker.replace('time.time')  # pylint: disable=C0103
33
 
        self.expect(MockTime()).result(10)
34
 
        self.expect(MockTime()).result(15)
35
 
        meter = self.mocker.mock()
36
 
        meter.increment(operation_name)
37
 
        meter.timing(operation_name, 5)
38
 
        meter.increment(operation_name + '.done')
39
 
        meter.decrement(operation_name)
40
 
        self.mocker.replay()
41
 
 
42
 
        def fake_function():
43
 
            """Useless method."""
44
 
 
45
 
        with Measure('test_prefix', 'fake_function', meter):
46
 
            fake_function()
47
 
 
48
 
    def test_measure_handle_exceptions(self):
49
 
        """Test exceptions."""
50
 
 
51
 
        class TestException(Exception):
52
 
            """Exception used to test the wrapper."""
53
 
            pass
54
 
 
55
 
        operation_name = 'fake_function'
56
 
        meter = self.mocker.mock()
57
 
        meter.increment(operation_name)
58
 
        meter.increment(operation_name + '.error')
59
 
        meter.decrement(operation_name)
60
 
        self.mocker.replay()
61
 
 
62
 
        def fake_function():
63
 
            """Fake Method that raises an exception."""
64
 
            raise TestException()
65
 
 
66
 
        try:
67
 
            with Measure('test_prefix', 'fake_function', meter):
68
 
                fake_function()
69
 
        except TestException:
70
 
            self.assertTrue(True)
71
 
        else:
72
 
            self.fail('measure context manager did not reraise exception.')
 
1
"""Tests for the Metrics convenience class."""
 
2
 
 
3
from unittest import TestCase
 
4
 
 
5
from txstatsd.metrics.metrics import Metrics
 
6
 
 
7
 
 
8
class FakeStatsDClient(object):
 
9
 
 
10
    def connect(self):
 
11
        """Connect to the StatsD server."""
 
12
        pass
 
13
 
 
14
    def disconnect(self):
 
15
        """Disconnect from the StatsD server."""
 
16
        pass
 
17
 
 
18
    def write(self, data):
 
19
        """Send the metric to the StatsD server."""
 
20
        self.data = data
 
21
 
 
22
 
 
23
class TestMetrics(TestCase):
 
24
 
 
25
    def setUp(self):
 
26
        self.connection = FakeStatsDClient()
 
27
        self.metrics = Metrics(self.connection, 'txstatsd.tests')
 
28
 
 
29
    def test_gauge(self):
 
30
        """Test reporting of a gauge metric sample."""
 
31
        self.metrics.gauge('gauge', 102)
 
32
        self.assertEqual(self.connection.data,
 
33
                         'txstatsd.tests.gauge:102|g')
 
34
 
 
35
    def test_counter(self):
 
36
        """Test the increment and decrement operations."""
 
37
        self.metrics.increment('counter', 18)
 
38
        self.assertEqual(self.connection.data,
 
39
                         'txstatsd.tests.counter:18|c')
 
40
        self.metrics.decrement('counter', 9)
 
41
        self.assertEqual(self.connection.data,
 
42
                         'txstatsd.tests.counter:-9|c')
 
43
 
 
44
    def test_timing(self):
 
45
        """Test the timing operation."""
 
46
        self.metrics.timing('timing', 101123)
 
47
        self.assertEqual(self.connection.data,
 
48
                         'txstatsd.tests.timing:101123|ms')
 
49
 
 
50
    def test_empty_namespace(self):
 
51
        """Test reporting of an empty namespace."""
 
52
        self.metrics.namespace = None
 
53
        self.metrics.gauge('gauge', 213)
 
54
        self.assertEqual(self.connection.data,
 
55
                         'gauge:213|g')
 
56
 
 
57
        self.metrics.namespace = ''
 
58
        self.metrics.gauge('gauge', 413)
 
59
        self.assertEqual(self.connection.data,
 
60
                         'gauge:413|g')