forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transport.py
More file actions
187 lines (140 loc) · 5.14 KB
/
test_transport.py
File metadata and controls
187 lines (140 loc) · 5.14 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# coding: utf-8
import logging
import pickle
from datetime import datetime, timedelta
import pytest
from sentry_sdk import Hub, Client, add_breadcrumb, capture_message
from sentry_sdk.transport import _parse_rate_limits
from sentry_sdk.integrations.logging import LoggingIntegration
@pytest.fixture(params=[True, False])
def make_client(request):
def inner(*args, **kwargs):
client = Client(*args, **kwargs)
if request.param:
client = pickle.loads(pickle.dumps(client))
return client
return inner
@pytest.mark.forked
@pytest.mark.parametrize("debug", (True, False))
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
def test_transport_works(
httpserver,
request,
capsys,
caplog,
debug,
make_client,
client_flush_method,
maybe_monkeypatched_threading,
):
httpserver.serve_content("ok", 200)
caplog.set_level(logging.DEBUG)
client = make_client(
"http://foobar@{}/123".format(httpserver.url[len("http://") :]), debug=debug
)
Hub.current.bind_client(client)
request.addfinalizer(lambda: Hub.current.bind_client(None))
add_breadcrumb(level="info", message="i like bread", timestamp=datetime.utcnow())
capture_message("löl")
getattr(client, client_flush_method)()
out, err = capsys.readouterr()
assert not err and not out
assert httpserver.requests
assert any("Sending event" in record.msg for record in caplog.records) == debug
def test_transport_infinite_loop(httpserver, request):
httpserver.serve_content("ok", 200)
client = Client(
"http://foobar@{}/123".format(httpserver.url[len("http://") :]),
debug=True,
# Make sure we cannot create events from our own logging
integrations=[LoggingIntegration(event_level=logging.DEBUG)],
)
with Hub(client):
capture_message("hi")
client.flush()
assert len(httpserver.requests) == 1
NOW = datetime(2014, 6, 2)
@pytest.mark.parametrize(
"input,expected",
[
# Invalid rate limits
("", {}),
("invalid", {}),
(",,,", {}),
(
"42::organization, invalid, 4711:foobar;transaction;security:project",
{
None: NOW + timedelta(seconds=42),
"transaction": NOW + timedelta(seconds=4711),
"security": NOW + timedelta(seconds=4711),
# Unknown data categories
"foobar": NOW + timedelta(seconds=4711),
},
),
(
"4711:foobar;;transaction:organization",
{
"transaction": NOW + timedelta(seconds=4711),
# Unknown data categories
"foobar": NOW + timedelta(seconds=4711),
"": NOW + timedelta(seconds=4711),
},
),
],
)
def test_parse_rate_limits(input, expected):
assert dict(_parse_rate_limits(input, now=NOW)) == expected
def test_simple_rate_limits(httpserver, capsys, caplog):
client = Client(dsn="http://foobar@{}/123".format(httpserver.url[len("http://") :]))
httpserver.serve_content("no", 429, headers={"Retry-After": "4"})
client.capture_event({"type": "transaction"})
client.flush()
assert len(httpserver.requests) == 1
del httpserver.requests[:]
assert set(client.transport._disabled_until) == set([None])
client.capture_event({"type": "transaction"})
client.capture_event({"type": "event"})
client.flush()
assert not httpserver.requests
@pytest.mark.parametrize("response_code", [200, 429])
def test_data_category_limits(httpserver, capsys, caplog, response_code):
client = Client(
dict(dsn="http://foobar@{}/123".format(httpserver.url[len("http://") :]))
)
httpserver.serve_content(
"hm",
response_code,
headers={"X-Sentry-Rate-Limits": "4711:transaction:organization"},
)
client.capture_event({"type": "transaction"})
client.flush()
assert len(httpserver.requests) == 1
del httpserver.requests[:]
assert set(client.transport._disabled_until) == set(["transaction"])
client.transport.capture_event({"type": "transaction"})
client.transport.capture_event({"type": "transaction"})
client.flush()
assert not httpserver.requests
client.capture_event({"type": "event"})
client.flush()
assert len(httpserver.requests) == 1
@pytest.mark.parametrize("response_code", [200, 429])
def test_complex_limits_without_data_category(
httpserver, capsys, caplog, response_code
):
client = Client(
dict(dsn="http://foobar@{}/123".format(httpserver.url[len("http://") :]))
)
httpserver.serve_content(
"hm", response_code, headers={"X-Sentry-Rate-Limits": "4711::organization"},
)
client.capture_event({"type": "transaction"})
client.flush()
assert len(httpserver.requests) == 1
del httpserver.requests[:]
assert set(client.transport._disabled_until) == set([None])
client.transport.capture_event({"type": "transaction"})
client.transport.capture_event({"type": "transaction"})
client.capture_event({"type": "event"})
client.flush()
assert len(httpserver.requests) == 0