Skip to content

Commit af2253d

Browse files
committed
added tests for timer and fixed issue where it was required to use "total" as a name for functions aswell
1 parent 7a6d68f commit af2253d

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

statsd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
]
2020

2121
__name__ = 'python-statsd'
22-
__version__ = '1.5.2'
22+
__version__ = '1.5.4'
2323
__author__ = 'Rick van Hattem'
2424
__author_email__ = 'Rick.van.Hattem@Fawo.nl'
2525
__description__ = ('''statsd is a client for Etsy's node-js statsd server. '''

statsd/timer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _decorator(*args, **kwargs):
7171
return function(*args, **kwargs)
7272
finally:
7373
# Stop the timer, send the message and cleanup
74-
timer.stop(name)
74+
timer.stop('')
7575
del timer
7676

7777
return _decorator

tests/timer.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)