2021-09-27 08:22:30 +01:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
from pytest import LogCaptureFixture
|
2021-09-27 08:22:30 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
from sanic.response import empty
|
2021-09-27 08:22:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
PORT = 42101
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_no_exceptions_when_cancel_pending_request(
|
|
|
|
app, caplog: LogCaptureFixture
|
|
|
|
):
|
2021-09-27 08:22:30 +01:00
|
|
|
app.config.GRACEFUL_SHUTDOWN_TIMEOUT = 1
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
async def handler(request):
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.listener("after_server_start")
|
|
|
|
async def _request(sanic, loop):
|
|
|
|
connect = asyncio.open_connection("127.0.0.1", 8000)
|
|
|
|
_, writer = await connect
|
|
|
|
writer.write(b"GET / HTTP/1.1\r\n\r\n")
|
2021-09-27 08:22:30 +01:00
|
|
|
app.stop()
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
app.run(single_process=True, access_log=True)
|
|
|
|
|
|
|
|
assert "Request: GET http:/// stopped. Transport is closed." in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
def test_completes_request(app, caplog: LogCaptureFixture):
|
|
|
|
app.config.GRACEFUL_SHUTDOWN_TIMEOUT = 1
|
2021-09-27 08:22:30 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.get("/")
|
|
|
|
async def handler(request):
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
return empty()
|
|
|
|
|
|
|
|
@app.listener("after_server_start")
|
|
|
|
async def _request(sanic, loop):
|
|
|
|
connect = asyncio.open_connection("127.0.0.1", 8000)
|
|
|
|
_, writer = await connect
|
|
|
|
writer.write(b"GET / HTTP/1.1\r\n\r\n")
|
|
|
|
app.stop()
|
2021-09-27 08:22:30 +01:00
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(single_process=True, access_log=True)
|
|
|
|
|
|
|
|
assert ("sanic.access", 20, "") in caplog.record_tuples
|
|
|
|
|
|
|
|
# Make sure that the server starts shutdown process before access log
|
|
|
|
index_stopping = 0
|
|
|
|
for idx, record in enumerate(caplog.records):
|
|
|
|
if record.message.startswith("Stopping worker"):
|
|
|
|
index_stopping = idx
|
|
|
|
break
|
|
|
|
index_request = caplog.record_tuples.index(("sanic.access", 20, ""))
|
|
|
|
assert index_request > index_stopping > 0
|