|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import threading |
| 17 | +from urllib.request import HTTPError, urlopen |
| 18 | + |
| 19 | +import daphne.server |
| 20 | +import pytest |
| 21 | +from testing_support.fixtures import ( |
| 22 | + override_application_settings, |
| 23 | + raise_background_exceptions, |
| 24 | + validate_transaction_errors, |
| 25 | + validate_transaction_metrics, |
| 26 | + wait_for_background_threads, |
| 27 | +) |
| 28 | +from testing_support.sample_asgi_applications import ( |
| 29 | + AppWithCall, |
| 30 | + AppWithCallRaw, |
| 31 | + simple_app_v2_raw, |
| 32 | +) |
| 33 | +from testing_support.util import get_open_port |
| 34 | + |
| 35 | +from newrelic.common.object_names import callable_name |
| 36 | + |
| 37 | +DAPHNE_VERSION = tuple(int(v) for v in daphne.__version__.split(".")[:2]) |
| 38 | +skip_asgi_3_unsupported = pytest.mark.skipif(DAPHNE_VERSION < (3, 0), reason="ASGI3 unsupported") |
| 39 | +skip_asgi_2_unsupported = pytest.mark.skipif(DAPHNE_VERSION >= (3, 0), reason="ASGI2 unsupported") |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture( |
| 43 | + params=( |
| 44 | + pytest.param( |
| 45 | + simple_app_v2_raw, |
| 46 | + marks=skip_asgi_2_unsupported, |
| 47 | + ), |
| 48 | + pytest.param( |
| 49 | + AppWithCallRaw(), |
| 50 | + marks=skip_asgi_3_unsupported, |
| 51 | + ), |
| 52 | + pytest.param( |
| 53 | + AppWithCall(), |
| 54 | + marks=skip_asgi_3_unsupported, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ids=("raw", "class_with_call", "class_with_call_double_wrapped"), |
| 58 | +) |
| 59 | +def app(request, server_and_port): |
| 60 | + app = request.param |
| 61 | + server, _ = server_and_port |
| 62 | + server.application = app |
| 63 | + return app |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture(scope="session") |
| 67 | +def port(server_and_port): |
| 68 | + _, port = server_and_port |
| 69 | + return port |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(scope="session") |
| 73 | +def server_and_port(): |
| 74 | + port = get_open_port() |
| 75 | + |
| 76 | + servers = [] |
| 77 | + loops = [] |
| 78 | + ready = threading.Event() |
| 79 | + |
| 80 | + def server_run(): |
| 81 | + def on_ready(): |
| 82 | + if not ready.is_set(): |
| 83 | + loops.append(asyncio.get_event_loop()) |
| 84 | + servers.append(server) |
| 85 | + ready.set() |
| 86 | + |
| 87 | + async def fake_app(*args, **kwargs): |
| 88 | + raise RuntimeError("Failed to swap out app.") |
| 89 | + |
| 90 | + server = daphne.server.Server( |
| 91 | + fake_app, |
| 92 | + endpoints=["tcp:%d:interface=127.0.0.1" % port], |
| 93 | + ready_callable=on_ready, |
| 94 | + signal_handlers=False, |
| 95 | + verbosity=9, |
| 96 | + ) |
| 97 | + |
| 98 | + server.run() |
| 99 | + |
| 100 | + thread = threading.Thread(target=server_run, daemon=True) |
| 101 | + thread.start() |
| 102 | + assert ready.wait(timeout=10) |
| 103 | + yield servers[0], port |
| 104 | + |
| 105 | + reactor = daphne.server.reactor |
| 106 | + _ = [loop.call_soon_threadsafe(reactor.stop) for loop in loops] # Stop all loops |
| 107 | + thread.join(timeout=10) |
| 108 | + |
| 109 | + if thread.is_alive(): |
| 110 | + raise RuntimeError("Thread failed to exit in time.") |
| 111 | + |
| 112 | + |
| 113 | +@override_application_settings({"transaction_name.naming_scheme": "framework"}) |
| 114 | +def test_daphne_200(port, app): |
| 115 | + @validate_transaction_metrics(callable_name(app)) |
| 116 | + @raise_background_exceptions() |
| 117 | + @wait_for_background_threads() |
| 118 | + def response(): |
| 119 | + return urlopen("http://localhost:%d" % port, timeout=10) |
| 120 | + |
| 121 | + assert response().status == 200 |
| 122 | + |
| 123 | + |
| 124 | +@override_application_settings({"transaction_name.naming_scheme": "framework"}) |
| 125 | +@validate_transaction_errors(["builtins:ValueError"]) |
| 126 | +def test_daphne_500(port, app): |
| 127 | + @validate_transaction_metrics(callable_name(app)) |
| 128 | + @raise_background_exceptions() |
| 129 | + @wait_for_background_threads() |
| 130 | + def _test(): |
| 131 | + try: |
| 132 | + urlopen("http://localhost:%d/exc" % port) |
| 133 | + except HTTPError: |
| 134 | + pass |
| 135 | + |
| 136 | + _test() |
0 commit comments