Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): Add a Starlette testcase that uses Uvicorn #3023

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
35 changes: 35 additions & 0 deletions tests/conftest.py
@@ -1,6 +1,8 @@
import contextlib
import json
import os
import socket
import time
from threading import Thread
from contextlib import contextmanager
from http.server import BaseHTTPRequestHandler, HTTPServer
Expand All @@ -19,6 +21,11 @@
except ImportError:
eventlet = None

try:
import uvicorn
except ImportError:
uvicorn = None

import sentry_sdk
from sentry_sdk.envelope import Envelope
from sentry_sdk.integrations import _processed_integrations # noqa: F401
Expand Down Expand Up @@ -614,3 +621,31 @@ def __eq__(self, other):

def __ne__(self, other):
return not self.__eq__(other)


if uvicorn is not None:

class UvicornServer(uvicorn.Server):
@contextlib.contextmanager
def run_in_thread(self):
thread = Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()

@pytest.fixture(scope="session")
def uvicorn_server(request):
app = request.param()

config = uvicorn.Config(
app, host="127.0.0.1", port=5000, log_level="info", loop="none"
)
server = UvicornServer(config=config)

with server.run_in_thread():
yield
22 changes: 22 additions & 0 deletions tests/integrations/starlette/test_starlette.py
Expand Up @@ -9,6 +9,7 @@
from unittest import mock

import pytest
import requests

from sentry_sdk import capture_message, get_baggage, get_traceparent
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
Expand Down Expand Up @@ -100,6 +101,9 @@ def starlette_app_factory(middleware=None, debug=True):
)
templates = Jinja2Templates(directory=template_dir)

async def _ok(request):
return starlette.responses.JSONResponse({"status": "ok"})

async def _homepage(request):
1 / 0
return starlette.responses.JSONResponse({"status": "ok"})
Expand Down Expand Up @@ -143,6 +147,7 @@ async def _render_template(request):
app = starlette.applications.Starlette(
debug=debug,
routes=[
starlette.routing.Route("/ok", _ok),
starlette.routing.Route("/some_url", _homepage),
starlette.routing.Route("/custom_error", _custom_error),
starlette.routing.Route("/message", _message),
Expand Down Expand Up @@ -1078,3 +1083,20 @@ def test_transaction_name_in_middleware(
assert (
transaction_event["transaction_info"]["source"] == expected_transaction_source
)


@pytest.mark.parametrize("uvicorn_server", [starlette_app_factory], indirect=True)
def test_with_uvicorn(sentry_init, capture_envelopes, uvicorn_server):
# Sanity check that the app works with uvicorn which does its own ASGI 2/3
# discovery. If we wrap the ASGI app incorrectly, everything might seem ok
# until you try to run the app with uvicorn.

sentry_init(
integrations=[StarletteIntegration()],
)

envelopes = capture_envelopes()

requests.get("http://127.0.0.1:5000/ok")

assert not envelopes
7 changes: 4 additions & 3 deletions tox.ini
Expand Up @@ -218,8 +218,8 @@ envlist =

# Starlette
{py3.7,py3.10}-starlette-v{0.19}
{py3.7,py3.11}-starlette-v{0.20,0.24,0.28}
{py3.8,py3.11,py3.12}-starlette-v{0.32}
{py3.7,py3.11}-starlette-v{0.24,0.28}
{py3.8,py3.11,py3.12}-starlette-v{0.32,0.36}
{py3.8,py3.11,py3.12}-starlette-latest

# Starlite
Expand Down Expand Up @@ -561,15 +561,16 @@ deps =
starlette: pytest-asyncio
starlette: python-multipart
starlette: requests
starlette: uvicorn
starlette: httpx
# (this is a dependency of httpx)
starlette: anyio<4.0.0
starlette: jinja2
starlette-v0.19: starlette~=0.19.0
starlette-v0.20: starlette~=0.20.0
starlette-v0.24: starlette~=0.24.0
starlette-v0.28: starlette~=0.28.0
starlette-v0.32: starlette~=0.32.0
starlette-v0.36: starlette~=0.36.0
starlette-latest: starlette

# Starlite
Expand Down