-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_dogstatsd.py
More file actions
59 lines (43 loc) · 1.83 KB
/
test_dogstatsd.py
File metadata and controls
59 lines (43 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import unittest
from collections import deque
from datadog_lambda.dogstatsd import statsd
class FakeSocket(object):
def __init__(self):
self.payloads = deque()
def send(self, payload):
self.payloads.append(payload)
def recv(self, count=1, reset_wait=False, no_wait=False):
out = []
for _ in range(count):
out.append(self.payloads.popleft().decode("utf-8"))
return "\n".join(out)
def close(self):
pass
class TestDogStatsd(unittest.TestCase):
def setUp(self):
statsd.socket = FakeSocket()
def tearDown(self):
statsd.close_socket()
def recv(self, *args, **kwargs):
return statsd.socket.recv(*args, **kwargs)
def test_init(self):
self.assertEqual(statsd.host, "localhost")
self.assertEqual(statsd.port, 8125)
self.assertEqual(statsd.encoding, "utf-8")
def _checkOnlyOneMetric(self, value):
payload = self.recv()
metrics = payload.split("\n")
self.assertEqual(len(metrics), 1)
self.assertEqual(value, metrics[0])
def test_distribution_no_tags(self):
statsd.distribution("my.test.metric", 3)
self._checkOnlyOneMetric("my.test.metric:3|d")
def test_distribution_with_tags(self):
statsd.distribution("my.test.tags.metric", 3, tags=["taga:valuea,tagb:valueb"])
self._checkOnlyOneMetric("my.test.tags.metric:3|d|#taga:valuea_tagb:valueb")
def test_distribution_with_timestamp(self):
statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789)
self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")
def test_distribution_with_float_timestamp(self):
statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789.123)
self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")