]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Test transactions with concurrence [skip ci]
authorDenis Laxalde <denis.laxalde@dalibo.com>
Wed, 8 Dec 2021 15:53:05 +0000 (16:53 +0100)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Wed, 8 Dec 2021 15:53:45 +0000 (16:53 +0100)
tests/test_transaction.py
tests/test_transaction_async.py

index d6522e117dc4add866ce7a4e496e12c74185601f..d231c916131eb7fa443c34ccf9f512349426c1bf 100644 (file)
@@ -1,3 +1,4 @@
+import concurrent.futures
 import logging
 
 import pytest
@@ -648,3 +649,17 @@ def test_str(conn):
 
     assert "[IDLE]" in str(tx)
     assert "(terminated)" in str(tx)
+
+
+def test_concurrency(conn):
+    conn.autocommit = True
+
+    def fn(value):
+        with conn.transaction():
+            cur = conn.execute("select %s", (value,))
+        return cur
+
+    values = range(2)
+    with concurrent.futures.ThreadPoolExecutor() as e:
+        cursors = e.map(fn, values)
+    assert sum(cur.fetchone()[0] for cur in cursors) == sum(values)
index efcb1341ce6330e848c6e400b6737565cd1e9cdf..d09103b90d605bfebc53e85d602ea22cb45dfeec 100644 (file)
@@ -1,3 +1,4 @@
+import asyncio
 import logging
 
 import pytest
@@ -615,3 +616,16 @@ async def test_str(aconn):
 
     assert "[IDLE]" in str(tx)
     assert "(terminated)" in str(tx)
+
+
+async def test_concurrency(aconn):
+    await aconn.set_autocommit(True)
+
+    async def fn(value):
+        async with aconn.transaction():
+            cur = await aconn.execute("select %s", (value,))
+        return cur
+
+    values = range(2)
+    cursors = await asyncio.gather(*[fn(value) for value in values])
+    assert sum([(await cur.fetchone())[0] for cur in cursors]) == sum(values)