mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:29:27 +08:00
75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
import asyncio
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def test_lifespan_releases_environment_after_migration_failure(tmp_path):
|
|
env = dict(
|
|
os.environ,
|
|
DEEPTUTOR_HOME=str(tmp_path),
|
|
TEMP=str(tmp_path),
|
|
TMP=str(tmp_path),
|
|
TMPDIR=str(tmp_path),
|
|
)
|
|
subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
"import runpy,sys; runpy.run_path(sys.argv[1],run_name='__main__')",
|
|
__file__,
|
|
],
|
|
check=True,
|
|
env=env,
|
|
timeout=90,
|
|
)
|
|
|
|
|
|
async def exercise():
|
|
from deeptutor.api.main import app, lifespan
|
|
|
|
database = Path(os.environ["DEEPTUTOR_HOME"]) / "data/user/chat_history.db"
|
|
database.parent.mkdir(parents=True, exist_ok=True)
|
|
database.write_bytes(b"invalid database")
|
|
|
|
def probe():
|
|
return subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
"from deeptutor.modules.lifecycle import environment_lease\n"
|
|
"with environment_lease(exclusive=True): pass",
|
|
],
|
|
capture_output=True,
|
|
timeout=20,
|
|
)
|
|
|
|
assert probe().returncode == 0
|
|
try:
|
|
async with lifespan(app):
|
|
raise AssertionError("migration should fail")
|
|
except sqlite3.DatabaseError:
|
|
pass
|
|
assert not app.state.ready
|
|
assert probe().returncode == 0
|
|
database.unlink()
|
|
async with lifespan(app):
|
|
assert app.state.ready
|
|
assert probe().returncode != 0
|
|
assert not app.state.ready
|
|
assert probe().returncode == 0
|
|
|
|
try:
|
|
async with lifespan(app):
|
|
raise asyncio.CancelledError
|
|
except asyncio.CancelledError:
|
|
pass
|
|
assert not app.state.ready
|
|
assert probe().returncode == 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(exercise())
|