From: Denis Laxalde Date: Wed, 8 Dec 2021 15:53:05 +0000 (+0100) Subject: Test transactions with concurrence [skip ci] X-Git-Tag: pool-3.1~80^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=73dfb402a940daf39cd8145ab764cb0289bc1cc9;p=thirdparty%2Fpsycopg.git Test transactions with concurrence [skip ci] --- diff --git a/tests/test_transaction.py b/tests/test_transaction.py index d6522e117..d231c9161 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -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) diff --git a/tests/test_transaction_async.py b/tests/test_transaction_async.py index efcb1341c..d09103b90 100644 --- a/tests/test_transaction_async.py +++ b/tests/test_transaction_async.py @@ -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)