This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_statsd.py
105 lines (85 loc) · 3.64 KB
/
test_statsd.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import absolute_import
import pytest
from mock import MagicMock
from tchannel.errors import TChannelError
from tchannel.errors import TimeoutError
from tchannel.messages import ErrorCode
from tchannel.statsd import StatsdHook
from tchannel.tornado import Request
from tchannel.tornado import Response
from tchannel.tornado.response import StatusCode
@pytest.fixture
def req():
return Request(
endpoint="endpoint1",
service="test",
)
@pytest.fixture
def statsd_hook():
statsd = MagicMock()
return StatsdHook(
statsd=statsd
)
def test_before_send_request(statsd_hook, req):
statsd_hook.before_send_request(req)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.sent.no-service.test.endpoint1", 1
)
def test_after_receive_response(statsd_hook, req):
response = Response(code=StatusCode.ok)
statsd_hook.after_receive_response(req, response)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.success.no-service.test.endpoint1", 1
)
response = Response(code=StatusCode.error)
statsd_hook.after_receive_response(req, response)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.app-errors.no-service.test.endpoint1", 1
)
def test_after_receive_system_error(statsd_hook, req):
error = TChannelError.from_code(ErrorCode.bad_request)
statsd_hook.after_receive_system_error(req, error)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.system-errors.no-service." +
"test.endpoint1.bad-request", 1
)
def test_after_receive_system_error_per_attempt(statsd_hook, req):
error = TChannelError.from_code(code=ErrorCode.bad_request)
statsd_hook.after_receive_system_error_per_attempt(req, error)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.per-attempt.system-errors.no-service." +
"test.endpoint1.bad-request", 1
)
def test_on_operational_error(statsd_hook, req):
error = TimeoutError()
statsd_hook.on_operational_error(req, error)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.operational-errors.no-service." +
"test.endpoint1.timeout", 1
)
def test_on_operational_error_per_attempt(statsd_hook, req):
error = TimeoutError()
statsd_hook.on_operational_error_per_attempt(req, error)
statsd_hook._statsd.count.assert_called_with(
"tchannel.outbound.calls.per-attempt.operational-errors.no-service." +
"test.endpoint1.timeout", 1
)