|
28 | 28 | from sentry_sdk.transport import (
|
29 | 29 | KEEP_ALIVE_SOCKET_OPTIONS,
|
30 | 30 | _parse_rate_limits,
|
| 31 | + AsyncHttpTransport, |
31 | 32 | )
|
32 | 33 | from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger
|
33 | 34 |
|
@@ -146,6 +147,92 @@ def test_transport_works(
|
146 | 147 | assert any("Sending envelope" in record.msg for record in caplog.records) == debug
|
147 | 148 |
|
148 | 149 |
|
| 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 | + |
149 | 236 | @pytest.mark.parametrize(
|
150 | 237 | "num_pools,expected_num_pools",
|
151 | 238 | (
|
|
0 commit comments