chore: Close database engines to avoid aiosqlite pytest hangs

Co-authored-by: George Weale <gweale@google.com>
PiperOrigin-RevId: 852428755
This commit is contained in:
George Weale
2026-01-05 13:18:08 -08:00
committed by Copybara-Service
parent 8789ad8f16
commit 4ddb2cb2a8
4 changed files with 105 additions and 218 deletions
+28 -165
View File
@@ -45,33 +45,30 @@ def get_session_service(
return InMemorySessionService()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
@pytest.fixture(
params=[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
]
)
async def test_get_empty_session(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def session_service(request, tmp_path):
"""Provides a session service and closes database backends on teardown."""
service = get_session_service(request.param, tmp_path)
yield service
if isinstance(service, DatabaseSessionService):
await service.close()
@pytest.mark.asyncio
async def test_get_empty_session(session_service):
assert not await session_service.get_session(
app_name='my_app', user_id='test_user', session_id='123'
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_create_get_session(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_create_get_session(session_service):
app_name = 'my_app'
user_id = 'test_user'
state = {'key': 'value'}
@@ -111,16 +108,7 @@ async def test_create_get_session(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_create_and_list_sessions(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_create_and_list_sessions(session_service):
app_name = 'my_app'
user_id = 'test_user'
@@ -144,16 +132,7 @@ async def test_create_and_list_sessions(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_list_sessions_all_users(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_list_sessions_all_users(session_service):
app_name = 'my_app'
user_id_1 = 'user1'
user_id_2 = 'user2'
@@ -209,16 +188,7 @@ async def test_list_sessions_all_users(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_app_state_is_shared_by_all_users_of_app(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_app_state_is_shared_by_all_users_of_app(session_service):
app_name = 'my_app'
# User 1 creates a session, establishing app:k1
session1 = await session_service.create_session(
@@ -247,18 +217,7 @@ async def test_app_state_is_shared_by_all_users_of_app(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_user_state_is_shared_only_by_user_sessions(
service_type, tmp_path
):
session_service = get_session_service(service_type, tmp_path)
async def test_user_state_is_shared_only_by_user_sessions(session_service):
app_name = 'my_app'
# User 1 creates a session, establishing user:k1 for user 1
session1 = await session_service.create_session(
@@ -286,16 +245,7 @@ async def test_user_state_is_shared_only_by_user_sessions(
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_session_state_is_not_shared(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_session_state_is_not_shared(session_service):
app_name = 'my_app'
# User 1 creates a session session1, establishing sk1 only for session1
session1 = await session_service.create_session(
@@ -324,18 +274,7 @@ async def test_session_state_is_not_shared(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_temp_state_is_not_persisted_in_state_or_events(
service_type, tmp_path
):
session_service = get_session_service(service_type, tmp_path)
async def test_temp_state_is_not_persisted_in_state_or_events(session_service):
app_name = 'my_app'
user_id = 'u1'
session = await session_service.create_session(
@@ -361,16 +300,7 @@ async def test_temp_state_is_not_persisted_in_state_or_events(
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_get_session_respects_user_id(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_get_session_respects_user_id(session_service):
app_name = 'my_app'
# u1 creates session 's1' and adds an event
session1 = await session_service.create_session(
@@ -392,18 +322,7 @@ async def test_get_session_respects_user_id(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_create_session_with_existing_id_raises_error(
service_type, tmp_path
):
session_service = get_session_service(service_type, tmp_path)
async def test_create_session_with_existing_id_raises_error(session_service):
app_name = 'my_app'
user_id = 'test_user'
session_id = 'existing_session'
@@ -425,16 +344,7 @@ async def test_create_session_with_existing_id_raises_error(
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_append_event_bytes(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_append_event_bytes(session_service):
app_name = 'my_app'
user_id = 'user'
@@ -471,16 +381,7 @@ async def test_append_event_bytes(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_append_event_complete(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_append_event_complete(session_service):
app_name = 'my_app'
user_id = 'user'
@@ -532,18 +433,7 @@ async def test_append_event_complete(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_session_last_update_time_updates_on_event(
service_type, tmp_path
):
session_service = get_session_service(service_type, tmp_path)
async def test_session_last_update_time_updates_on_event(session_service):
app_name = 'my_app'
user_id = 'user'
@@ -573,16 +463,7 @@ async def test_session_last_update_time_updates_on_event(
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_get_session_with_config(service_type):
session_service = get_session_service(service_type)
async def test_get_session_with_config(session_service):
app_name = 'my_app'
user_id = 'user'
@@ -605,16 +486,7 @@ async def test_get_session_with_config(service_type):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_get_session_with_config(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_get_session_with_config(session_service):
app_name = 'my_app'
user_id = 'user'
@@ -674,16 +546,7 @@ async def test_get_session_with_config(service_type, tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize(
'service_type',
[
SessionServiceType.IN_MEMORY,
SessionServiceType.DATABASE,
SessionServiceType.SQLITE,
],
)
async def test_partial_events_are_not_persisted(service_type, tmp_path):
session_service = get_session_service(service_type, tmp_path)
async def test_partial_events_are_not_persisted(session_service):
app_name = 'my_app'
user_id = 'user'
session = await session_service.create_session(