mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 14:29:48 +08:00
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
"""Snapshot readers release their SQLite connections before returning."""
|
|
|
|
from pathlib import Path
|
|
import sqlite3
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.memory.snapshot import adapters
|
|
from deeptutor.services.session.sqlite_store import SQLiteSessionStore
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"reader", ["read_chat_entities", "read_quiz_entities", "probe_chat_entities"]
|
|
)
|
|
def test_snapshot_reader_closes_connection(tmp_path: Path, monkeypatch, reader: str) -> None:
|
|
"""Keep native handles alive so garbage collection cannot mask a leak."""
|
|
database = tmp_path / "history.db"
|
|
SQLiteSessionStore(database)
|
|
monkeypatch.setattr(
|
|
adapters,
|
|
"get_account_path_service",
|
|
lambda: SimpleNamespace(get_chat_history_db=lambda: database),
|
|
)
|
|
monkeypatch.setattr(
|
|
"deeptutor.services.workspace.get_content_workspace_service",
|
|
lambda: SimpleNamespace(registered_bindings=lambda: []),
|
|
)
|
|
connect = sqlite3.connect
|
|
connections = []
|
|
|
|
def track_connection(*args, **kwargs):
|
|
connection = connect(*args, **kwargs)
|
|
connections.append(connection)
|
|
return connection
|
|
|
|
monkeypatch.setattr(sqlite3, "connect", track_connection)
|
|
try:
|
|
assert getattr(adapters, reader)() == []
|
|
assert len(connections) == 1
|
|
for connection in connections:
|
|
with pytest.raises(sqlite3.ProgrammingError, match="closed"):
|
|
connection.execute("SELECT 1")
|
|
database.rename(tmp_path / "moved.db")
|
|
finally:
|
|
for connection in connections:
|
|
connection.close()
|