]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add test for pool config
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 8 Mar 2021 01:28:14 +0000 (02:28 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index 1c3177814d916b139356b6914f50460d71eedde8..219541754019f42f019b0e8af3d8dcf867b81409 100644 (file)
@@ -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):
index fa46ba54351db82edf2319cd5e703e071981d541..7c4842cc18fb9e752d8a728f666393c71be6cc3f 100644 (file)
@@ -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):