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

Commit fe00323

Browse files
committed
Add test recreating issue.
1 parent f916dbb commit fe00323

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

tests/test_databases.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def async_adapter(wrapped_func):
100100

101101
@functools.wraps(wrapped_func)
102102
def run_sync(*args, **kwargs):
103-
loop = asyncio.get_event_loop()
103+
loop = asyncio.new_event_loop()
104104
task = wrapped_func(*args, **kwargs)
105105
return loop.run_until_complete(task)
106106

@@ -752,6 +752,34 @@ async def db_lookup():
752752
await asyncio.gather(db_lookup(), db_lookup())
753753

754754

755+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
756+
def test_global_connection_is_initialized_lazily(database_url):
757+
"""
758+
Ensure that global connection is initialized at latest possible time
759+
so it's _query_lock will belong to same event loop that async_adapter has
760+
initialized.
761+
762+
See https://github.com/encode/databases/issues/157 for more context.
763+
"""
764+
765+
database_url = DatabaseURL(database_url)
766+
if database_url.dialect != "postgresql":
767+
pytest.skip("Test requires `pg_sleep()`")
768+
769+
database = Database(database_url, force_rollback=True)
770+
771+
@async_adapter
772+
async def run_database_queries():
773+
async with database:
774+
775+
async def db_lookup():
776+
await database.fetch_one("SELECT pg_sleep(1)")
777+
778+
await asyncio.gather(db_lookup(), db_lookup())
779+
780+
run_database_queries()
781+
782+
755783
@pytest.mark.parametrize("database_url", DATABASE_URLS)
756784
@async_adapter
757785
async def test_iterate_outside_transaction_with_values(database_url):

0 commit comments

Comments
 (0)