Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit f3078aa

Browse files
committed
test: add concurrent task tests
1 parent c9e3464 commit f3078aa

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

tests/test_databases.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -961,16 +961,59 @@ async def test_database_url_interface(database_url):
961961
@pytest.mark.parametrize("database_url", DATABASE_URLS)
962962
@async_adapter
963963
async def test_concurrent_access_on_single_connection(database_url):
964-
database_url = DatabaseURL(database_url)
965-
if database_url.dialect != "postgresql":
966-
pytest.skip("Test requires `pg_sleep()`")
967-
968964
async with Database(database_url, force_rollback=True) as database:
969965

970966
async def db_lookup():
971-
await database.fetch_one("SELECT pg_sleep(1)")
967+
await database.fetch_one("SELECT 1 AS value")
968+
969+
await asyncio.gather(
970+
db_lookup(),
971+
db_lookup(),
972+
)
973+
974+
975+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
976+
@async_adapter
977+
async def test_concurrent_transactions_on_single_connection(database_url: str):
978+
async with Database(database_url) as database:
979+
980+
@database.transaction()
981+
async def db_lookup():
982+
await database.fetch_one(query="SELECT 1 AS value")
983+
984+
await asyncio.gather(
985+
db_lookup(),
986+
db_lookup(),
987+
)
988+
989+
990+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
991+
@async_adapter
992+
async def test_concurrent_tasks_on_single_connection(database_url: str):
993+
async with Database(database_url) as database:
994+
995+
async def db_lookup():
996+
await database.fetch_one(query="SELECT 1 AS value")
997+
998+
await asyncio.gather(
999+
asyncio.create_task(db_lookup()),
1000+
asyncio.create_task(db_lookup()),
1001+
)
1002+
1003+
1004+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
1005+
@async_adapter
1006+
async def test_concurrent_task_transactions_on_single_connection(database_url: str):
1007+
async with Database(database_url) as database:
1008+
1009+
@database.transaction()
1010+
async def db_lookup():
1011+
await database.fetch_one(query="SELECT 1 AS value")
9721012

973-
await asyncio.gather(db_lookup(), db_lookup())
1013+
await asyncio.gather(
1014+
asyncio.create_task(db_lookup()),
1015+
asyncio.create_task(db_lookup()),
1016+
)
9741017

9751018

9761019
@pytest.mark.parametrize("database_url", DATABASE_URLS)

0 commit comments

Comments
 (0)