diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index 1089e85..d79d52d 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -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)) @@ -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() diff --git a/tests/test_databases.py b/tests/test_databases.py index cee4044..c059c08 100644 --- a/tests/test_databases.py +++ b/tests/test_databases.py @@ -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""" @@ -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())