Skip to content

Commit

Permalink
move not really concurrent tests to databases
Browse files Browse the repository at this point in the history
  • Loading branch information
devkral committed Sep 11, 2024
1 parent 8f4f124 commit ce1e8c5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 55 deletions.
55 changes: 0 additions & 55 deletions tests/test_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,6 @@ def _startswith(tested, params):
return False


@pytest.mark.asyncio
async def test_concurrent_access_on_single_connection(database_url):
database_url = DatabaseURL(str(database_url.url))
if not _startswith(database_url.dialect, ["mysql", "mariadb", "postgres", "mssql"]):
pytest.skip("Test requires sleep function")
async with Database(database_url, force_rollback=True, full_isolation=False) as database:

async def db_lookup():
if database_url.dialect.startswith("postgres"):
await database.fetch_one("SELECT pg_sleep(0.3)")
elif database_url.dialect.startswith("mysql") or database_url.dialect.startswith(
"mariadb"
):
await database.fetch_one("SELECT SLEEP(0.3)")
elif database_url.dialect.startswith("mssql"):
await database.execute("WAITFOR DELAY '00:00:00.300'")

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


def _future_helper(awaitable, future):
try:
future.set_result(asyncio.run(awaitable))
Expand Down Expand Up @@ -277,38 +257,3 @@ async def db_connect():

with pytest.raises(RuntimeError):
await to_thread(asyncio.run, db_connect())


@pytest.mark.asyncio
async def test_global_connection_is_initialized_lazily(database_url):
"""
Ensure that global connection is initialized at latest possible time
so it's _query_lock will belong to same event loop that async_adapter has
initialized.
See https://github.com/dymmond/databasez/issues/157 for more context.
"""

database_url = DatabaseURL(database_url.url)
if not _startswith(database_url.dialect, ["mysql", "mariadb", "postgres", "mssql"]):
pytest.skip("Test requires sleep function")

database = Database(database_url, force_rollback=True)

async def run_database_queries():
async with database:

async def db_lookup():
if database_url.dialect.startswith("postgres"):
await database.fetch_one("SELECT pg_sleep(0.3)")
elif database_url.dialect.startswith("mysql") or database_url.dialect.startswith(
"mariadb"
):
await database.fetch_one("SELECT SLEEP(0.3)")
elif database_url.dialect.startswith("mssql"):
await database.execute("WAITFOR DELAY '00:00:00.300'")

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

await run_database_queries()
await database.disconnect()
62 changes: 62 additions & 0 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
MIXED_DATABASE_CONFIG_URLS_IDS = [*DATABASE_URLS, *(f"{x}[config]" for x in DATABASE_URLS)]


def _startswith(tested, params):
for param in params:
if tested.startswith(param):
return True
return False


@pytest.fixture(params=DATABASE_URLS)
def database_url(request):
"""Yield test database despite its name"""
Expand Down Expand Up @@ -889,3 +896,58 @@ async def test_mapping_property_interface(database_url):
list_result = await database.fetch_all(query=query)
assert list_result[0]._mapping["text"] == "example1"
assert list_result[0]._mapping["completed"] is True


@pytest.mark.asyncio
async def test_global_connection_is_initialized_lazily(database_url):
"""
Ensure that global connection is initialized at latest possible time
so it's _query_lock will belong to same event loop that async_adapter has
initialized.
See https://github.com/dymmond/databasez/issues/157 for more context.
"""

database_url = DatabaseURL(database_url.url)
if not _startswith(database_url.dialect, ["mysql", "mariadb", "postgres", "mssql"]):
pytest.skip("Test requires sleep function")

database = Database(database_url, force_rollback=True)

async def run_database_queries():
async with database:

async def db_lookup():
if database_url.dialect.startswith("postgres"):
await database.fetch_one("SELECT pg_sleep(0.3)")
elif database_url.dialect.startswith("mysql") or database_url.dialect.startswith(
"mariadb"
):
await database.fetch_one("SELECT SLEEP(0.3)")
elif database_url.dialect.startswith("mssql"):
await database.execute("WAITFOR DELAY '00:00:00.300'")

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

await run_database_queries()
await database.disconnect()


@pytest.mark.asyncio
async def test_concurrent_access_on_single_connection(database_url):
database_url = DatabaseURL(str(database_url.url))
if not _startswith(database_url.dialect, ["mysql", "mariadb", "postgres", "mssql"]):
pytest.skip("Test requires sleep function")
async with Database(database_url, force_rollback=True, full_isolation=False) as database:

async def db_lookup():
if database_url.dialect.startswith("postgres"):
await database.fetch_one("SELECT pg_sleep(0.3)")
elif database_url.dialect.startswith("mysql") or database_url.dialect.startswith(
"mariadb"
):
await database.fetch_one("SELECT SLEEP(0.3)")
elif database_url.dialect.startswith("mssql"):
await database.execute("WAITFOR DELAY '00:00:00.300'")

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

0 comments on commit ce1e8c5

Please sign in to comment.