From: Daniele Varrazzo Date: Mon, 8 Mar 2021 01:28:14 +0000 (+0100) Subject: Add test for pool config X-Git-Tag: 3.0.dev0~87^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91fe57149f01234564246bce09ccb190d8415bc1;p=thirdparty%2Fpsycopg.git Add test for pool config --- diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 1c3177814..219541754 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -126,6 +126,47 @@ def test_setup_no_timeout(dsn, proxy): conn.execute("select 1") +def test_configure(dsn): + inits = 0 + + def configure(conn): + nonlocal inits + inits += 1 + conn.execute("set default_transaction_read_only to on") + + with pool.ConnectionPool(minconn=1, configure=configure) as p: + with p.connection() as conn: + assert inits == 1 + res = conn.execute("show default_transaction_read_only") + assert res.fetchone()[0] == "on" + + with p.connection() as conn: + assert inits == 1 + res = conn.execute("show default_transaction_read_only") + assert res.fetchone()[0] == "on" + conn.close() + + with p.connection() as conn: + assert inits == 2 + res = conn.execute("show default_transaction_read_only") + assert res.fetchone()[0] == "on" + + +@pytest.mark.slow +def test_configure_broken(dsn, caplog): + caplog.set_level(logging.WARNING, logger="psycopg3.pool") + + def configure(conn): + conn.execute("WAT") + + with pool.ConnectionPool(minconn=1, configure=configure) as p: + with pytest.raises(pool.PoolTimeout): + p.wait_ready(timeout=0.5) + + assert caplog.records + assert "WAT" in caplog.records[0].message + + @pytest.mark.slow def test_queue(dsn, retries): def worker(n): diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index fa46ba543..7c4842cc1 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -143,6 +143,47 @@ async def test_setup_no_timeout(dsn, proxy): await conn.execute("select 1") +async def test_configure(dsn): + inits = 0 + + async def configure(conn): + nonlocal inits + inits += 1 + await conn.execute("set default_transaction_read_only to on") + + async with pool.AsyncConnectionPool(minconn=1, configure=configure) as p: + async with p.connection() as conn: + assert inits == 1 + res = await conn.execute("show default_transaction_read_only") + assert (await res.fetchone())[0] == "on" + + async with p.connection() as conn: + assert inits == 1 + res = await conn.execute("show default_transaction_read_only") + assert (await res.fetchone())[0] == "on" + await conn.close() + + async with p.connection() as conn: + assert inits == 2 + res = await conn.execute("show default_transaction_read_only") + assert (await res.fetchone())[0] == "on" + + +@pytest.mark.slow +async def test_configure_broken(dsn, caplog): + caplog.set_level(logging.WARNING, logger="psycopg3.pool") + + async def configure(conn): + await conn.execute("WAT") + + async with pool.AsyncConnectionPool(minconn=1, configure=configure) as p: + with pytest.raises(pool.PoolTimeout): + await p.wait_ready(timeout=0.5) + + assert caplog.records + assert "WAT" in caplog.records[0].message + + @pytest.mark.slow async def test_queue(dsn, retries): async def worker(n):