Skip to content

Commit

Permalink
test: add concurrent task tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zevisert committed Apr 11, 2023
1 parent 3c0790d commit 90e5350
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,16 +961,59 @@ async def test_database_url_interface(database_url):
@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_concurrent_access_on_single_connection(database_url):
database_url = DatabaseURL(database_url)
if database_url.dialect != "postgresql":
pytest.skip("Test requires `pg_sleep()`")

async with Database(database_url, force_rollback=True) as database:

async def db_lookup():
await database.fetch_one("SELECT pg_sleep(1)")
await database.fetch_one("SELECT 1 AS value")

await asyncio.gather(
db_lookup(),
db_lookup(),
)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_concurrent_transactions_on_single_connection(database_url: str):
async with Database(database_url) as database:

@database.transaction()
async def db_lookup():
await database.fetch_one(query="SELECT 1 AS value")

await asyncio.gather(
db_lookup(),
db_lookup(),
)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_concurrent_tasks_on_single_connection(database_url: str):
async with Database(database_url) as database:

async def db_lookup():
await database.fetch_one(query="SELECT 1 AS value")

await asyncio.gather(
asyncio.create_task(db_lookup()),
asyncio.create_task(db_lookup()),
)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_concurrent_task_transactions_on_single_connection(database_url: str):
async with Database(database_url) as database:

@database.transaction()
async def db_lookup():
await database.fetch_one(query="SELECT 1 AS value")

await asyncio.gather(db_lookup(), db_lookup())
await asyncio.gather(
asyncio.create_task(db_lookup()),
asyncio.create_task(db_lookup()),
)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
Expand Down

0 comments on commit 90e5350

Please sign in to comment.