From b9146ce32a79c9602ef23c2ddb15ed27fe403d3b Mon Sep 17 00:00:00 2001 From: Yury Pliner Date: Sat, 5 Dec 2020 15:24:58 +0500 Subject: [PATCH] Add one more test. Also both exceptions could be raised --- tests/test_sa_engine.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_sa_engine.py b/tests/test_sa_engine.py index b9ec6a66..98de2a93 100644 --- a/tests/test_sa_engine.py +++ b/tests/test_sa_engine.py @@ -150,7 +150,7 @@ async def test_terminate_with_acquired_connections(make_engine): assert conn.closed -async def test_release_disconnected_connection( +async def test_release_after_connection_disconnected_before_select( tcp_proxy, unused_port, pg_params, make_engine ): server_port = pg_params["port"] @@ -160,7 +160,7 @@ async def test_release_disconnected_connection( engine = await make_engine(port=proxy_port) with pytest.raises( - psycopg2.InterfaceError, match='connection already closed' + (psycopg2.InterfaceError, psycopg2.OperationalError) ): with pytest.warns(ResourceWarning, match='Invalid transaction status'): async with engine.acquire() as conn, conn.begin(): @@ -169,3 +169,24 @@ async def test_release_disconnected_connection( await conn.execute('SELECT 1;') assert engine.size == 0 + + +async def test_release_after_connection_disconnected_before_begin( + tcp_proxy, unused_port, pg_params, make_engine +): + server_port = pg_params["port"] + proxy_port = unused_port() + + tcp_proxy = await tcp_proxy(proxy_port, server_port) + engine = await make_engine(port=proxy_port) + + with pytest.raises( + (psycopg2.InterfaceError, psycopg2.OperationalError) + ): + with pytest.warns(ResourceWarning, match='Invalid transaction status'): + async with engine.acquire() as conn: + await conn.execute('SELECT 1;') + await tcp_proxy.disconnect() + async with conn.begin(): + pytest.fail("Should not be here") + assert engine.size == 0