Sanic Server WorkerManager refactor (#2499)
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
This commit is contained in:
@@ -3,69 +3,80 @@ import logging
|
||||
|
||||
from time import sleep
|
||||
|
||||
import pytest
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.exceptions import ServiceUnavailable
|
||||
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
||||
from sanic.response import text
|
||||
|
||||
|
||||
response_timeout_app = Sanic("test_response_timeout")
|
||||
response_timeout_default_app = Sanic("test_response_timeout_default")
|
||||
response_handler_cancelled_app = Sanic("test_response_handler_cancelled")
|
||||
@pytest.fixture
|
||||
def response_timeout_app():
|
||||
app = Sanic("test_response_timeout")
|
||||
app.config.RESPONSE_TIMEOUT = 1
|
||||
|
||||
response_timeout_app.config.RESPONSE_TIMEOUT = 1
|
||||
response_timeout_default_app.config.RESPONSE_TIMEOUT = 1
|
||||
response_handler_cancelled_app.config.RESPONSE_TIMEOUT = 1
|
||||
@app.route("/1")
|
||||
async def handler_1(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
response_handler_cancelled_app.ctx.flag = False
|
||||
@app.exception(ServiceUnavailable)
|
||||
def handler_exception(request, exception):
|
||||
return text("Response Timeout from error_handler.", 503)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@response_timeout_app.route("/1")
|
||||
async def handler_1(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
@pytest.fixture
|
||||
def response_timeout_default_app():
|
||||
app = Sanic("test_response_timeout_default")
|
||||
app.config.RESPONSE_TIMEOUT = 1
|
||||
|
||||
@app.route("/1")
|
||||
async def handler_2(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@response_timeout_app.exception(ServiceUnavailable)
|
||||
def handler_exception(request, exception):
|
||||
return text("Response Timeout from error_handler.", 503)
|
||||
@pytest.fixture
|
||||
def response_handler_cancelled_app():
|
||||
app = Sanic("test_response_handler_cancelled")
|
||||
app.config.RESPONSE_TIMEOUT = 1
|
||||
app.ctx.flag = False
|
||||
|
||||
@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.
|
||||
|
||||
@app.route("/1")
|
||||
async def handler_3(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@response_timeout_default_app.route("/1")
|
||||
async def handler_2(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
|
||||
@response_handler_cancelled_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.
|
||||
|
||||
|
||||
@response_handler_cancelled_app.route("/1")
|
||||
async def handler_3(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
|
||||
def test_server_error_response_timeout():
|
||||
def test_server_error_response_timeout(response_timeout_app):
|
||||
request, response = response_timeout_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert response.text == "Response Timeout from error_handler."
|
||||
|
||||
|
||||
def test_default_server_error_response_timeout():
|
||||
def test_default_server_error_response_timeout(response_timeout_default_app):
|
||||
request, response = response_timeout_default_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert "Response Timeout" in response.text
|
||||
|
||||
|
||||
def test_response_handler_cancelled():
|
||||
def test_response_handler_cancelled(response_handler_cancelled_app):
|
||||
request, response = response_handler_cancelled_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert "Response Timeout" in response.text
|
||||
|
||||
Reference in New Issue
Block a user