Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions tests/integrations/sanic/test_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from sentry_sdk import capture_message, configure_scope
from sentry_sdk.integrations.sanic import SanicIntegration

from sanic import Sanic, request, response
from sanic import Sanic, request, response, __version__ as SANIC_VERSION_RAW
from sanic.exceptions import abort

SANIC_VERSION = tuple(map(int, SANIC_VERSION_RAW.split(".")))


@pytest.fixture
def app():
Expand All @@ -34,7 +36,7 @@ def test_request_data(sentry_init, app, capture_events):
event, = events
assert event["transaction"] == "hi"
assert event["request"]["env"] == {"REMOTE_ADDR": ""}
assert set(event["request"]["headers"]) == {
assert set(event["request"]["headers"]) >= {
"accept",
"accept-encoding",
"host",
Expand Down Expand Up @@ -123,6 +125,17 @@ def myhandler(request, exception):


def test_concurrency(sentry_init, app):
"""
Make sure we instrument Sanic in a way where request data does not leak
between request handlers. This test also implicitly tests our concept of
how async code should be instrumented, so if it breaks it likely has
ramifications for other async integrations and async usercode.

We directly call the request handler instead of using Sanic's test client
because that's the only way we could reproduce leakage with such a low
amount of concurrent tasks.
"""

sentry_init(integrations=[SanicIntegration()])

@app.route("/context-check/<i>")
Expand All @@ -140,16 +153,21 @@ async def context_check(request, i):
async def task(i):
responses = []

await app.handle_request(
request.Request(
url_bytes="http://localhost/context-check/{i}".format(i=i).encode(
"ascii"
),
headers={},
version="1.1",
method="GET",
transport=None,
kwargs = {
"url_bytes": "http://localhost/context-check/{i}".format(i=i).encode(
"ascii"
),
"headers": {},
"version": "1.1",
"method": "GET",
"transport": None,
}

if SANIC_VERSION >= (19,):
kwargs["app"] = app

await app.handle_request(
request.Request(**kwargs),
write_callback=responses.append,
stream_callback=responses.append,
)
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ envlist =
{pypy,py2.7,py3.5,py3.6,py3.7}-falcon-1.4
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-falcon-2.0

{py3.5,py3.6,py3.7}-sanic-{0.8,18}
py3.5-sanic-{0.8,18}
{py3.6,py3.7}-sanic-{0.8,18,19}

{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-celery-{4.1,4.2,4.3,4.4}
{pypy,py2.7}-celery-3
Expand Down Expand Up @@ -97,6 +98,7 @@ deps =

sanic-0.8: sanic>=0.8,<0.9
sanic-18: sanic>=18.0,<19.0
sanic-19: sanic>=19.0,<20.0
{py3.5,py3.6}-sanic: aiocontextvars==0.2.1
sanic: aiohttp

Expand Down