|
| 1 | +from __future__ import with_statement |
| 2 | +import mock |
| 3 | +import statsd |
| 4 | + |
| 5 | +with mock.patch('statsd.Client') as mock_client: |
| 6 | + instance = mock_client.return_value |
| 7 | + instance._send.return_value = 1 |
| 8 | + |
| 9 | + # Some simple decorator tests |
| 10 | + timer0 = statsd.Timer('timer0') |
| 11 | + @timer0.decorate |
| 12 | + def a(): |
| 13 | + pass |
| 14 | + a() |
| 15 | + mock_client._send.assert_called_with(mock.ANY, {'timer0.a': '0|ms'}) |
| 16 | + |
| 17 | + |
| 18 | + timer1 = statsd.Timer('timer1') |
| 19 | + @timer1.decorate('spam') |
| 20 | + def b(): |
| 21 | + pass |
| 22 | + b() |
| 23 | + mock_client._send.assert_called_with(mock.ANY, {'timer1.spam': '0|ms'}) |
| 24 | + |
| 25 | + |
| 26 | + timer2 = timer1.get_client('eggs') |
| 27 | + @timer2.decorate |
| 28 | + def c(): |
| 29 | + pass |
| 30 | + c() |
| 31 | + mock_client._send.assert_called_with(mock.ANY, {'timer1.eggs.c': '0|ms'}) |
| 32 | + |
| 33 | + |
| 34 | + timer3 = timer1.get_client('eggs0') |
| 35 | + @timer3.decorate('d0') |
| 36 | + def d(): |
| 37 | + pass |
| 38 | + d() |
| 39 | + mock_client._send.assert_called_with(mock.ANY, {'timer1.eggs0.d0': '0|ms'}) |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + # More advanced usage with intermediate timers |
| 44 | + timer4 = statsd.Timer('timer4') |
| 45 | + timer4.start() |
| 46 | + timer4.stop() |
| 47 | + mock_client._send.assert_called_with(mock.ANY, {'timer4.total': '0|ms'}) |
| 48 | + |
| 49 | + |
| 50 | + timer5 = statsd.Timer('timer5') |
| 51 | + timer5.start() |
| 52 | + timer5.stop('test') |
| 53 | + mock_client._send.assert_called_with(mock.ANY, {'timer5.test': '0|ms'}) |
| 54 | + |
| 55 | + |
| 56 | + timer6 = statsd.Timer('timer6') |
| 57 | + timer6.start() |
| 58 | + timer6.intermediate('extras') |
| 59 | + mock_client._send.assert_called_with(mock.ANY, {'timer6.extras': '0|ms'}) |
| 60 | + timer6.stop() |
| 61 | + mock_client._send.assert_called_with(mock.ANY, {'timer6.total': '0|ms'}) |
| 62 | + |
| 63 | + |
| 64 | + timer7 = statsd.Timer('timer7') |
| 65 | + timer7.start() |
| 66 | + timer7.intermediate('extras') |
| 67 | + mock_client._send.assert_called_with(mock.ANY, {'timer7.extras': '0|ms'}) |
| 68 | + timer7.stop('test') |
| 69 | + mock_client._send.assert_called_with(mock.ANY, {'timer7.test': '0|ms'}) |
| 70 | + |
0 commit comments