Skip to content

Commit 98d74ed

Browse files
committed
test(transport): Add inital transport tests
GH-4601
1 parent b9f2ec7 commit 98d74ed

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

requirements-testing.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ asttokens
1212
responses
1313
pysocks
1414
socksio
15-
httpcore[http2]
15+
httpcore[http2,asyncio]>=1.0 # From 1.0, httpcore async is optional
1616
setuptools
1717
freezegun
1818
Brotli

tests/test_transport.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from sentry_sdk.transport import (
2929
KEEP_ALIVE_SOCKET_OPTIONS,
3030
_parse_rate_limits,
31+
AsyncHttpTransport,
3132
)
3233
from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger
3334

@@ -146,6 +147,92 @@ def test_transport_works(
146147
assert any("Sending envelope" in record.msg for record in caplog.records) == debug
147148

148149

150+
@pytest.mark.asyncio
151+
@pytest.mark.parametrize("debug", (True, False))
152+
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
153+
@pytest.mark.parametrize("use_pickle", (True, False))
154+
@pytest.mark.parametrize("compression_level", (0, 9, None))
155+
@pytest.mark.parametrize("compression_algo", ("gzip", "br", "<invalid>", None))
156+
@pytest.mark.parametrize("http2", [True, False] if PY38 else [False])
157+
async def test_transport_works_async(
158+
capturing_server,
159+
request,
160+
capsys,
161+
caplog,
162+
debug,
163+
make_client,
164+
client_flush_method,
165+
use_pickle,
166+
compression_level,
167+
compression_algo,
168+
http2,
169+
):
170+
caplog.set_level(logging.DEBUG)
171+
172+
experiments = {}
173+
if compression_level is not None:
174+
experiments["transport_compression_level"] = compression_level
175+
176+
if compression_algo is not None:
177+
experiments["transport_compression_algo"] = compression_algo
178+
179+
if http2:
180+
experiments["transport_http2"] = True
181+
182+
# Enable async transport
183+
experiments["transport_async"] = True
184+
185+
client = make_client(
186+
debug=debug,
187+
_experiments=experiments,
188+
)
189+
190+
if use_pickle:
191+
client = pickle.loads(pickle.dumps(client))
192+
193+
# Verify we're using async transport
194+
assert isinstance(
195+
client.transport, AsyncHttpTransport
196+
), "Expected AsyncHttpTransport"
197+
198+
sentry_sdk.get_global_scope().set_client(client)
199+
request.addfinalizer(lambda: sentry_sdk.get_global_scope().set_client(None))
200+
201+
add_breadcrumb(
202+
level="info", message="i like bread", timestamp=datetime.now(timezone.utc)
203+
)
204+
capture_message("löl")
205+
206+
if client_flush_method == "close":
207+
await client.close(timeout=2.0)
208+
else:
209+
if hasattr(client, "_flush_async"):
210+
await client._flush_async(timeout=2.0, callback=None)
211+
# Need to kill, as the end of the test will close the event loop, but the worker task is still alive
212+
client.transport._worker.kill()
213+
214+
out, err = capsys.readouterr()
215+
assert not err and not out
216+
assert capturing_server.captured
217+
should_compress = (
218+
# default is to compress with brotli if available, gzip otherwise
219+
(compression_level is None)
220+
or (
221+
# setting compression level to 0 means don't compress
222+
compression_level
223+
> 0
224+
)
225+
) and (
226+
# if we couldn't resolve to a known algo, we don't compress
227+
compression_algo
228+
!= "<invalid>"
229+
)
230+
231+
assert capturing_server.captured[0].compressed == should_compress
232+
233+
assert any("Sending envelope" in record.msg for record in caplog.records) == debug
234+
235+
149236
@pytest.mark.parametrize(
150237
"num_pools,expected_num_pools",
151238
(

0 commit comments

Comments
 (0)