forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close HTTP loop when connection task cancelled (sanic-org#2245)
* Terminate loop when no transport exists * Add log when closing HTTP loop because of shutdown * Add unit test
- Loading branch information
Showing
4 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import asyncio | ||
import logging | ||
import time | ||
|
||
from collections import Counter | ||
from multiprocessing import Process | ||
|
||
import httpx | ||
|
||
|
||
PORT = 42101 | ||
|
||
|
||
def test_no_exceptions_when_cancel_pending_request(app, caplog): | ||
app.config.GRACEFUL_SHUTDOWN_TIMEOUT = 1 | ||
|
||
@app.get("/") | ||
async def handler(request): | ||
await asyncio.sleep(5) | ||
|
||
@app.after_server_start | ||
def shutdown(app, _): | ||
time.sleep(0.2) | ||
app.stop() | ||
|
||
def ping(): | ||
time.sleep(0.1) | ||
response = httpx.get("http://127.0.0.1:8000") | ||
print(response.status_code) | ||
|
||
p = Process(target=ping) | ||
p.start() | ||
|
||
with caplog.at_level(logging.INFO): | ||
app.run() | ||
|
||
p.kill() | ||
|
||
counter = Counter([r[1] for r in caplog.record_tuples]) | ||
|
||
assert counter[logging.INFO] == 5 | ||
assert logging.ERROR not in counter | ||
assert ( | ||
caplog.record_tuples[3][2] | ||
== "Request: GET http://127.0.0.1:8000/ stopped. Transport is closed." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters