-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
73 lines (58 loc) · 2.66 KB
/
test.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import unittest
import random
import os
import zmq
from rumrunner import Rumrunner
class TestRumrunner(unittest.TestCase):
def test_send_counter_metric(self):
ctx = zmq.Context()
recv_socket = ctx.socket(zmq.PULL)
tmp_metric_socket = '/var/tmp/test_metric_{0}'.format(random.random())
recv_socket.bind('ipc://{0}'.format(tmp_metric_socket))
try:
Rumrunner(tmp_metric_socket, 'test_app').counter('test_counter')
recv_socket.recv() # suck out empty string for write test
self.assertEqual(recv_socket.recv(),
b'["test_app", "test_counter", "COUNTER", 1]')
finally:
os.remove(tmp_metric_socket)
def test_error_out_on_not_writable_socket_disable(self):
ctx = zmq.Context()
recv_socket = ctx.socket(zmq.PULL)
tmp_metric_socket = '/var/tmp/test_metric_{0}'.format(random.random())
recv_socket.bind('ipc://{0}'.format(tmp_metric_socket))
try:
Rumrunner(tmp_metric_socket, 'test_app', strict_check_socket=False)
os.chmod(tmp_metric_socket, 0o444)
# Should not raise an exception due to permissions
Rumrunner(tmp_metric_socket, 'test_app', strict_check_socket=False)
finally:
os.remove(tmp_metric_socket)
def test_error_out_on_not_writable_socket(self):
ctx = zmq.Context()
recv_socket = ctx.socket(zmq.PULL)
tmp_metric_socket = '/var/tmp/test_metric_{0}'.format(random.random())
recv_socket.bind('ipc://{0}'.format(tmp_metric_socket))
try:
Rumrunner(tmp_metric_socket, 'test_app')
os.chmod(tmp_metric_socket, 0o444)
self.assertRaises(Exception, Rumrunner, tmp_metric_socket, 'test_app')
finally:
os.remove(tmp_metric_socket)
def test_mock_rumrunner(self):
from rumrunner import unmock_rumrunner, mock_rumrunner, MockRumrunner
self.assertTrue(isinstance(Rumrunner('/var/tmp/test', 'test_app',
strict_check_socket=False),
Rumrunner))
mock_rumrunner()
self.assertTrue(isinstance(Rumrunner('/var/tmp/test', 'test_app',
strict_check_socket=False),
MockRumrunner))
unmock_rumrunner()
self.assertTrue(isinstance(Rumrunner('/var/tmp/test', 'test_app',
strict_check_socket=False),
Rumrunner))
if __name__ == '__main__':
unittest.main()