-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Engine.release method to release connection in any way #756
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import asyncio | ||
|
||
import psycopg2 | ||
import pytest | ||
from psycopg2.extensions import parse_dsn | ||
from sqlalchemy import Column, Integer, MetaData, String, Table | ||
|
@@ -80,10 +81,10 @@ def test_not_context_manager(engine): | |
async def test_release_transacted(engine): | ||
conn = await engine.acquire() | ||
tr = await conn.begin() | ||
with pytest.raises(sa.InvalidRequestError): | ||
engine.release(conn) | ||
with pytest.warns(ResourceWarning, match='Invalid transaction status'): | ||
await engine.release(conn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it is better to raise an exception here instead of a warning. Looks like a user's bug to release a connection in the middle of transaction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added this comment to the issue as well. |
||
del tr | ||
await conn.close() | ||
assert conn.closed | ||
|
||
|
||
def test_timeout(engine): | ||
|
@@ -147,3 +148,46 @@ async def test_terminate_with_acquired_connections(make_engine): | |
await engine.wait_closed() | ||
|
||
assert conn.closed | ||
|
||
|
||
async def test_release_after_connection_disconnected_before_select( | ||
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'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it ok to spam users with both warning and exception? Maybe it is better just to raise an exception? Connection reset is not a user's fault, so I think that this warning doesn't help them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for noticing this. I'm a newbie in the aiopg codebase and don't know the reasons why these warnings are reported in such a way. @asvetlov Could you share your opinion please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial thought was preventing a programming error and signal this situation early.
The 1) is easier, 2) is harder to implement but maybe more correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, got you, thanks. I've opened a new issue to fix it a bit later, because it looks like merging of the PR with incorrect warnings in quite rare case is much better than suffering from broken pool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense |
||
async with engine.acquire() as conn, conn.begin(): | ||
await conn.execute('SELECT 1;') | ||
await tcp_proxy.disconnect() | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In python 3.6 there is no such a method