add try/finally block for always clean up resources

This commit is contained in:
Yun Xu 2019-05-17 00:25:46 -07:00
parent 2a64dabe82
commit a2dbbb25a1

View File

@ -241,6 +241,7 @@ 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."""
try:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop) client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
@ -252,12 +253,14 @@ def test_keep_alive_timeout_reuse():
request, response = client.get("/1") request, response = client.get("/1")
assert response.status == 200 assert response.status == 200
assert response.text == "OK" assert response.text == "OK"
finally:
client.kill_server() 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."""
try:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop) client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
@ -276,6 +279,7 @@ def test_keep_alive_client_timeout():
assert exception is not None assert exception is not None
assert isinstance(exception, ValueError) assert isinstance(exception, ValueError)
assert "got a new connection" in exception.args[0] assert "got a new connection" in exception.args[0]
finally:
client.kill_server() client.kill_server()
@ -284,6 +288,7 @@ 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."""
try:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop) client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
@ -305,4 +310,5 @@ def test_keep_alive_server_timeout():
"Connection reset" in exception.args[0] "Connection reset" in exception.args[0]
or "got a new connection" in exception.args[0] or "got a new connection" in exception.args[0]
) )
finally:
client.kill_server() client.kill_server()