Skip to content

Commit

Permalink
Merge pull request #1251 from newrelic/feature-daphne-http2-testing
Browse files Browse the repository at this point in the history
Daphne HTTP/2 Testing
  • Loading branch information
hmstepanek authored Nov 11, 2024
2 parents 0e0ddb8 + 55abebb commit 4334b5e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1202,12 +1202,12 @@ jobs:

valkey:
env:
TOTAL_GROUPS: 2
TOTAL_GROUPS: 1

strategy:
fail-fast: false
matrix:
group-number: [1, 2]
group-number: [1]

runs-on: ubuntu-latest
container:
Expand Down
35 changes: 22 additions & 13 deletions tests/adapter_daphne/test_daphne.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

import asyncio
import threading
from urllib.request import HTTPError, urlopen

import daphne.server
import niquests
import pytest
from testing_support.certs import CERT_PATH
from testing_support.fixtures import (
override_application_settings,
raise_background_exceptions,
wait_for_background_threads,
)
from testing_support.http_23_testing import make_request
from testing_support.sample_asgi_applications import (
AppWithCall,
AppWithCallRaw,
Expand All @@ -38,8 +40,12 @@
)

from newrelic.common.object_names import callable_name
from newrelic.common.package_version_utils import (
get_package_version,
get_package_version_tuple,
)

DAPHNE_VERSION = tuple(int(v) for v in daphne.__version__.split(".")[:2])
DAPHNE_VERSION = get_package_version_tuple("daphne")
skip_asgi_3_unsupported = pytest.mark.skipif(DAPHNE_VERSION < (3, 0), reason="ASGI3 unsupported")
skip_asgi_2_unsupported = pytest.mark.skipif(DAPHNE_VERSION >= (3, 0), reason="ASGI2 unsupported")

Expand Down Expand Up @@ -98,7 +104,7 @@ async def fake_app(*args, **kwargs):

server = daphne.server.Server(
fake_app,
endpoints=[f"tcp:{port}:interface=127.0.0.1"],
endpoints=[f"ssl:{port}:privateKey={CERT_PATH}:certKey={CERT_PATH}:interface=127.0.0.1"],
ready_callable=on_ready,
signal_handlers=False,
verbosity=9,
Expand All @@ -119,32 +125,35 @@ async def fake_app(*args, **kwargs):
raise RuntimeError("Thread failed to exit in time.")


@pytest.mark.parametrize("http_version", [1, 2], ids=["HTTP/1", "HTTP/2"])
@override_application_settings({"transaction_name.naming_scheme": "framework"})
def test_daphne_200(port, app):
def test_daphne_200(port, app, http_version):
daphne_version = get_package_version("daphne")
assert daphne_version is not None

@validate_transaction_metrics(
callable_name(app),
custom_metrics=[
(f"Python/Dispatcher/Daphne/{daphne.__version__}", 1),
(f"Python/Dispatcher/Daphne/{daphne_version}", 1),
],
)
@raise_background_exceptions()
@wait_for_background_threads()
def response():
return urlopen(f"http://localhost:{port}", timeout=10) # nosec
return make_request(host="localhost", port=port, path="/", http_version=http_version, timeout=10)

assert response().status == 200
response().raise_for_status()


@pytest.mark.parametrize("http_version", [1, 2], ids=["HTTP/1", "HTTP/2"])
@override_application_settings({"transaction_name.naming_scheme": "framework"})
@validate_transaction_errors(["builtins:ValueError"])
def test_daphne_500(port, app):
def test_daphne_500(port, app, http_version):
@validate_transaction_errors(["builtins:ValueError"])
@validate_transaction_metrics(callable_name(app))
@raise_background_exceptions()
@wait_for_background_threads()
def _test():
try:
urlopen(f"http://localhost:{port}/exc") # nosec
except HTTPError:
pass
with pytest.raises(niquests.exceptions.HTTPError):
make_request(host="localhost", port=port, path="/exc", http_version=http_version, timeout=10)

_test()
1 change: 1 addition & 0 deletions tests/adapter_hypercorn/test_hypercorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def wait_for_port(port, retries=10):
@override_application_settings({"transaction_name.naming_scheme": "framework"})
def test_hypercorn_200(port, app, http_version):
hypercorn_version = get_package_version("hypercorn")
assert hypercorn_version is not None

@validate_transaction_metrics(
callable_name(app),
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ deps =
adapter_asgiref-asgiref0307: asgiref<3.8
adapter_cheroot: cheroot
adapter_daphne-daphnelatest: daphne
adapter_daphne-daphnelatest: Twisted[tls,http2]
adapter_daphne: niquests
adapter_gevent: WSGIProxy2
adapter_gevent: gevent
adapter_gevent: urllib3
Expand Down

0 comments on commit 4334b5e

Please sign in to comment.