2017-09-11 08:17:33 +01:00
|
|
|
import asyncio
|
2021-03-22 23:20:17 +00:00
|
|
|
import logging
|
|
|
|
from time import sleep
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
import pytest
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic import Sanic
|
2017-09-11 08:17:33 +01:00
|
|
|
from sanic.exceptions import ServiceUnavailable
|
2021-03-22 23:20:17 +00:00
|
|
|
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.response import text
|
|
|
|
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def response_timeout_app():
|
|
|
|
app = Sanic("test_response_timeout")
|
|
|
|
app.config.RESPONSE_TIMEOUT = 1
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.route("/1")
|
|
|
|
async def handler_1(request):
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
return text("OK")
|
2018-12-30 18:37:30 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.exception(ServiceUnavailable)
|
|
|
|
def handler_exception(request, exception):
|
|
|
|
return text("Response Timeout from error_handler.", 503)
|
2021-03-22 23:20:17 +00:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
return app
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def response_timeout_default_app():
|
|
|
|
app = Sanic("test_response_timeout_default")
|
|
|
|
app.config.RESPONSE_TIMEOUT = 1
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.route("/1")
|
|
|
|
async def handler_2(request):
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
return text("OK")
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
return app
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def response_handler_cancelled_app():
|
|
|
|
app = Sanic("test_response_handler_cancelled")
|
|
|
|
app.config.RESPONSE_TIMEOUT = 1
|
|
|
|
app.ctx.flag = False
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.exception(asyncio.CancelledError)
|
|
|
|
def handler_cancelled(request, exception):
|
|
|
|
# If we get a CancelledError, it means sanic has already sent a response,
|
|
|
|
# we should not ever have to handle a CancelledError.
|
|
|
|
response_handler_cancelled_app.ctx.flag = True
|
|
|
|
return text("App received CancelledError!", 500)
|
|
|
|
# The client will never receive this response, because the socket
|
|
|
|
# is already closed when we get a CancelledError.
|
2018-08-06 06:02:12 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.route("/1")
|
|
|
|
async def handler_3(request):
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
return text("OK")
|
2018-08-06 06:02:12 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
return app
|
2018-08-06 06:02:12 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_server_error_response_timeout(response_timeout_app):
|
2021-03-22 23:20:17 +00:00
|
|
|
request, response = response_timeout_app.test_client.get("/1")
|
|
|
|
assert response.status == 503
|
|
|
|
assert response.text == "Response Timeout from error_handler."
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_default_server_error_response_timeout(response_timeout_default_app):
|
2021-03-22 23:20:17 +00:00
|
|
|
request, response = response_timeout_default_app.test_client.get("/1")
|
|
|
|
assert response.status == 503
|
|
|
|
assert "Response Timeout" in response.text
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_response_handler_cancelled(response_handler_cancelled_app):
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = response_handler_cancelled_app.test_client.get("/1")
|
2018-08-06 06:02:12 +01:00
|
|
|
assert response.status == 503
|
2020-01-20 14:58:14 +00:00
|
|
|
assert "Response Timeout" in response.text
|
2021-03-22 23:20:17 +00:00
|
|
|
assert response_handler_cancelled_app.ctx.flag is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_response_timeout_not_applied(caplog):
|
|
|
|
modified_config = LOGGING_CONFIG_DEFAULTS
|
|
|
|
modified_config["loggers"]["sanic.root"]["level"] = "DEBUG"
|
|
|
|
|
|
|
|
app = Sanic("test_logging", log_config=modified_config)
|
|
|
|
app.config.RESPONSE_TIMEOUT = 1
|
|
|
|
app.ctx.event = asyncio.Event()
|
|
|
|
|
|
|
|
@app.websocket("/ws")
|
|
|
|
async def ws_handler(request, ws):
|
|
|
|
sleep(2)
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
request.app.ctx.event.set()
|
|
|
|
|
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
_ = app.test_client.websocket("/ws")
|
|
|
|
assert app.ctx.event.is_set()
|
|
|
|
assert (
|
|
|
|
"sanic.root",
|
|
|
|
10,
|
|
|
|
"Handling websocket. Timeouts disabled.",
|
|
|
|
) in caplog.record_tuples
|