import pytest
+from ..utils import gc_collect
+
try:
import psycopg_pool as pool
except ImportError:
@pytest.mark.slow
-def test_reconnect_after_max_lifetime(dsn):
+def test_reconnect_after_max_lifetime(dsn, asyncio_run):
# See issue #219, pool created before the loop.
p = pool.AsyncConnectionPool(dsn, min_size=1, max_lifetime=0.2, open=False)
finally:
await p.close()
- asyncio.run(asyncio.wait_for(test(), timeout=2.0))
+ asyncio_run(asyncio.wait_for(test(), timeout=2.0))
@pytest.mark.slow
-def test_working_created_before_loop(dsn):
+def test_working_created_before_loop(dsn, asyncio_run):
p = pool.AsyncNullConnectionPool(dsn, open=False)
async def test():
finally:
await p.close()
- asyncio.run(asyncio.wait_for(test(), timeout=2.0))
+ asyncio_run(asyncio.wait_for(test(), timeout=2.0))
def test_cant_create_open_outside_loop(dsn):
with pytest.raises(RuntimeError):
pool.AsyncConnectionPool(dsn, open=True)
+
+
+@pytest.fixture
+def asyncio_run(recwarn):
+ """Fixture reuturning asyncio.run, but managing resources at exit.
+
+ In certain runs, fd objects are leaked and the error will only be caught
+ downstream, by some innocent test calling gc_collect().
+ """
+ recwarn.clear()
+ try:
+ yield asyncio.run
+ finally:
+ gc_collect()
+ if recwarn:
+ warn = recwarn.pop(ResourceWarning)
+ assert "unclosed event loop" in str(warn.message)
+ assert not recwarn