add try/finally block for always clean up resources
This commit is contained in:
parent
2a64dabe82
commit
a2dbbb25a1
|
@ -241,42 +241,46 @@ def test_keep_alive_timeout_reuse():
|
||||||
"""If the server keep-alive timeout and client keep-alive timeout are
|
"""If the server keep-alive timeout and client keep-alive timeout are
|
||||||
both longer than the delay, the client _and_ server will successfully
|
both longer than the delay, the client _and_ server will successfully
|
||||||
reuse the existing connection."""
|
reuse the existing connection."""
|
||||||
loop = asyncio.new_event_loop()
|
try:
|
||||||
asyncio.set_event_loop(loop)
|
loop = asyncio.new_event_loop()
|
||||||
client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
|
asyncio.set_event_loop(loop)
|
||||||
headers = {"Connection": "keep-alive"}
|
client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
|
||||||
request, response = client.get("/1", headers=headers)
|
headers = {"Connection": "keep-alive"}
|
||||||
assert response.status == 200
|
request, response = client.get("/1", headers=headers)
|
||||||
assert response.text == "OK"
|
assert response.status == 200
|
||||||
loop.run_until_complete(aio_sleep(1))
|
assert response.text == "OK"
|
||||||
request, response = client.get("/1")
|
loop.run_until_complete(aio_sleep(1))
|
||||||
assert response.status == 200
|
request, response = client.get("/1")
|
||||||
assert response.text == "OK"
|
assert response.status == 200
|
||||||
client.kill_server()
|
assert response.text == "OK"
|
||||||
|
finally:
|
||||||
|
client.kill_server()
|
||||||
|
|
||||||
|
|
||||||
def test_keep_alive_client_timeout():
|
def test_keep_alive_client_timeout():
|
||||||
"""If the server keep-alive timeout is longer than the client
|
"""If the server keep-alive timeout is longer than the client
|
||||||
keep-alive timeout, client will try to create a new connection here."""
|
keep-alive timeout, client will try to create a new connection here."""
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
|
|
||||||
headers = {"Connection": "keep-alive"}
|
|
||||||
try:
|
try:
|
||||||
request, response = client.get(
|
loop = asyncio.new_event_loop()
|
||||||
"/1", headers=headers, request_keepalive=1
|
asyncio.set_event_loop(loop)
|
||||||
)
|
client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
|
||||||
assert response.status == 200
|
headers = {"Connection": "keep-alive"}
|
||||||
assert response.text == "OK"
|
try:
|
||||||
loop.run_until_complete(aio_sleep(2))
|
request, response = client.get(
|
||||||
exception = None
|
"/1", headers=headers, request_keepalive=1
|
||||||
request, response = client.get("/1", request_keepalive=1)
|
)
|
||||||
except ValueError as e:
|
assert response.status == 200
|
||||||
exception = e
|
assert response.text == "OK"
|
||||||
assert exception is not None
|
loop.run_until_complete(aio_sleep(2))
|
||||||
assert isinstance(exception, ValueError)
|
exception = None
|
||||||
assert "got a new connection" in exception.args[0]
|
request, response = client.get("/1", request_keepalive=1)
|
||||||
client.kill_server()
|
except ValueError as e:
|
||||||
|
exception = e
|
||||||
|
assert exception is not None
|
||||||
|
assert isinstance(exception, ValueError)
|
||||||
|
assert "got a new connection" in exception.args[0]
|
||||||
|
finally:
|
||||||
|
client.kill_server()
|
||||||
|
|
||||||
|
|
||||||
def test_keep_alive_server_timeout():
|
def test_keep_alive_server_timeout():
|
||||||
|
@ -284,25 +288,27 @@ def test_keep_alive_server_timeout():
|
||||||
keep-alive timeout, the client will either a 'Connection reset' error
|
keep-alive timeout, the client will either a 'Connection reset' error
|
||||||
_or_ a new connection. Depending on how the event-loop handles the
|
_or_ a new connection. Depending on how the event-loop handles the
|
||||||
broken server connection."""
|
broken server connection."""
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
|
|
||||||
headers = {"Connection": "keep-alive"}
|
|
||||||
try:
|
try:
|
||||||
request, response = client.get(
|
loop = asyncio.new_event_loop()
|
||||||
"/1", headers=headers, request_keepalive=60
|
asyncio.set_event_loop(loop)
|
||||||
|
client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
|
||||||
|
headers = {"Connection": "keep-alive"}
|
||||||
|
try:
|
||||||
|
request, response = client.get(
|
||||||
|
"/1", headers=headers, request_keepalive=60
|
||||||
|
)
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.text == "OK"
|
||||||
|
loop.run_until_complete(aio_sleep(3))
|
||||||
|
exception = None
|
||||||
|
request, response = client.get("/1", request_keepalive=60)
|
||||||
|
except ValueError as e:
|
||||||
|
exception = e
|
||||||
|
assert exception is not None
|
||||||
|
assert isinstance(exception, ValueError)
|
||||||
|
assert (
|
||||||
|
"Connection reset" in exception.args[0]
|
||||||
|
or "got a new connection" in exception.args[0]
|
||||||
)
|
)
|
||||||
assert response.status == 200
|
finally:
|
||||||
assert response.text == "OK"
|
client.kill_server()
|
||||||
loop.run_until_complete(aio_sleep(3))
|
|
||||||
exception = None
|
|
||||||
request, response = client.get("/1", request_keepalive=60)
|
|
||||||
except ValueError as e:
|
|
||||||
exception = e
|
|
||||||
assert exception is not None
|
|
||||||
assert isinstance(exception, ValueError)
|
|
||||||
assert (
|
|
||||||
"Connection reset" in exception.args[0]
|
|
||||||
or "got a new connection" in exception.args[0]
|
|
||||||
)
|
|
||||||
client.kill_server()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user