207 lines
10 KiB
Python
207 lines
10 KiB
Python
"""Review 319 regressions; native databases are replaced by isolated files."""
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from threading import Event
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from cbb_refresh_verify import session_record as sessions
|
|
from test_session_record import child, record
|
|
|
|
|
|
def retain_unconfirmed(root, library):
|
|
root.mkdir(parents=True)
|
|
path = root/'session.json'
|
|
sessions.write_session_record(path, dict(operation_id=root.name, library=str(library), processes=[]))
|
|
done = Event()
|
|
sessions.handoff_session(path, evidence_root=root, progress=lambda _: done.set())
|
|
assert done.wait(3)
|
|
with sessions._observer_guard:
|
|
observer = sessions._observers.get(str(path.absolute()).casefold())
|
|
if observer:
|
|
observer[0].join(3)
|
|
assert not observer[0].is_alive()
|
|
assert not sessions.library_available(library)
|
|
return path
|
|
|
|
|
|
def test_explicit_proof_resumes_queue_after_unconfirmed_observer_ended(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock as locks
|
|
from cbb_refresh_verify.operation_queue import NativeQueue
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {})
|
|
library = tmp_path/'library/lib.lmc'
|
|
library.parent.mkdir(); library.touch()
|
|
other = tmp_path/'other.lmc'
|
|
retain_unconfirmed(tmp_path/'old', library)
|
|
retain_unconfirmed(tmp_path/'independent', other)
|
|
@contextmanager
|
|
def proof(*args, **kwargs):
|
|
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
|
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
|
result = locks.release_retained_library(library, tmp_path/'proof')
|
|
assert result['status'] == 'RELEASE_CONFIRMED'
|
|
assert sessions.library_available(library), 'successful explicit proof left the queue blocked'
|
|
assert sessions.discover_sessions(tmp_path/'old') == [], 'released history reappeared as unconfirmed'
|
|
assert not sessions.library_available(other)
|
|
assert NativeQueue().run(lambda: 'resumed', ready=lambda: sessions.library_available(library)) == 'resumed'
|
|
|
|
|
|
def test_failed_proof_and_newer_handoff_cannot_clear_queue_retention(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock as locks
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {})
|
|
library = tmp_path/'library/lib.lmc'
|
|
library.parent.mkdir(); library.touch()
|
|
old = retain_unconfirmed(tmp_path/'old', library)
|
|
def denied(*args, **kwargs):
|
|
raise OSError('external owner remains')
|
|
monkeypatch.setattr(locks, '_prove_library_released', denied)
|
|
with pytest.raises(OSError, match='external owner'):
|
|
locks.release_retained_library(library, tmp_path/'proof')
|
|
assert not sessions.library_available(library)
|
|
@contextmanager
|
|
def proof(*args, **kwargs):
|
|
retain_unconfirmed(tmp_path/'new', library)
|
|
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
|
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
|
locks.release_retained_library(library, tmp_path/'proof')
|
|
assert not sessions.library_available(library)
|
|
assert str(old.absolute()).casefold() not in sessions._retained_libraries
|
|
|
|
|
|
def test_discovery_of_exited_session_never_reconciles_library(tmp_path, child, monkeypatch):
|
|
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
|
|
child.terminate(); child.wait(5)
|
|
def forbidden(*args, **kwargs):
|
|
pytest.fail('passive history scan attempted library release')
|
|
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', forbidden)
|
|
found = sessions.discover_sessions(tmp_path)
|
|
assert len(found) == 1 and found[0]['path'] == str(path)
|
|
assert found[0]['status'] == 'UNCONFIRMED'
|
|
assert found[0]['release_proof'] is None
|
|
|
|
|
|
@pytest.mark.parametrize('phase', ['PRECALL', 'POSTCALL'])
|
|
@pytest.mark.parametrize('broken_evidence', [False, True])
|
|
def test_parent_loss_exits_idle_worker_even_when_stop_evidence_fails(tmp_path, phase, broken_evidence):
|
|
run_watchdog_case(tmp_path, phase, broken_evidence=broken_evidence, exits=True)
|
|
|
|
|
|
@pytest.mark.parametrize('phase', ['NATIVE_CALL', 'CLOSING', 'UNCONFIRMED'])
|
|
def test_parent_loss_preserves_pending_or_unknown_worker(tmp_path, phase):
|
|
run_watchdog_case(tmp_path, phase, exits=False)
|
|
|
|
|
|
def test_stale_postcall_journal_cannot_override_in_memory_active_call(tmp_path):
|
|
run_watchdog_case(tmp_path, 'NATIVE_CALL', stale=True, exits=False)
|
|
|
|
|
|
def test_missing_journal_preserves_worker_until_state_can_be_confirmed(tmp_path):
|
|
run_watchdog_case(tmp_path, 'POSTCALL', missing=True, exits=False)
|
|
|
|
|
|
def run_watchdog_case(root, phase, *, exits, broken_evidence=False, stale=False, missing=False):
|
|
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
|
|
script, ready, returned = root/'worker.py', root/'ready', root/'returned'
|
|
cancel = root/'missing/cancel' if broken_evidence else root/'cancel'
|
|
script.write_text(
|
|
'import time\nfrom pathlib import Path\n'
|
|
'from cbb_refresh_verify import session_record as s, desktop_worker as d\n'
|
|
f'job=dict(operation_id="watchdog-test", session_record_path={str(root/"session.json")!r})\n'
|
|
f's.update_worker_state(job, {"POSTCALL" if stale else phase!r})\n'
|
|
'original=s.write_session_record\n'
|
|
'def write(path,value):\n'
|
|
f' if {broken_evidence!r} and Path(path).name=="supervisor-lost.json": raise OSError("disk full")\n'
|
|
f' if {stale!r} and Path(path).name=="worker-state.json": raise OSError("stale journal")\n'
|
|
' return original(path,value)\n'
|
|
's.write_session_record=write\n'
|
|
+ (f's.update_worker_state(job,{phase!r})\n' if stale else '') +
|
|
(f'Path({str(root/"worker-state.json")!r}).unlink()\n' if missing else '') +
|
|
f'd.start_supervisor_watchdog({supervisor.pid},cancel_path={str(cancel)!r},'
|
|
'session_record_path=job["session_record_path"],operation_id=job["operation_id"])\n'
|
|
f'Path({str(ready)!r}).touch()\n'
|
|
f'while not Path({str(returned)!r}).exists(): time.sleep(.02)\n'
|
|
's.write_session_record=original\n'
|
|
's.update_worker_state(job,"POSTCALL")\n'
|
|
'time.sleep(30)\n', encoding='utf-8')
|
|
log = (root/'worker.log').open('wb')
|
|
worker = subprocess.Popen([sys.executable, str(script)], stdout=log, stderr=log)
|
|
try:
|
|
deadline = time.monotonic()+6
|
|
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
|
|
time.sleep(.02)
|
|
assert ready.exists()
|
|
supervisor.terminate(); supervisor.wait(5)
|
|
if exits:
|
|
assert worker.wait(4) == 75, 'idle orphan must exit without another application checkpoint'
|
|
else:
|
|
with pytest.raises(subprocess.TimeoutExpired):
|
|
worker.wait(.4)
|
|
returned.touch()
|
|
assert worker.wait(4) == 75, 'watchdog must recheck once the pending call returns'
|
|
finally:
|
|
for process in (worker, supervisor):
|
|
if process.poll() is None:
|
|
process.terminate()
|
|
process.wait(5)
|
|
log.close()
|
|
|
|
|
|
def test_residual_dialog_refresh_shows_runtime_blocker_without_releasing(qtbot, tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock as locks
|
|
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {})
|
|
library = tmp_path/'library/lib.lmc'
|
|
library.parent.mkdir(); library.touch()
|
|
monkeypatch.setattr(locks, 'inspect_residual_sessions', lambda *_: {'library': str(library), 'sessions': []})
|
|
monkeypatch.setattr(locks, 'release_retained_library', lambda *a, **kw: pytest.fail('preview mutated library'))
|
|
dialog = ResidualReleaseDialog(library, tmp_path/'release-checks')
|
|
qtbot.addWidget(dialog)
|
|
qtbot.waitUntil(lambda: not dialog.busy)
|
|
retain_unconfirmed(tmp_path/'late-session', library)
|
|
dialog.refresh_button.click()
|
|
qtbot.waitUntil(lambda: not dialog.busy)
|
|
assert dialog.plan['runtime_sessions'][0]['status'] == 'UNCONFIRMED'
|
|
assert '待核对' in dialog.runtime_label.text()
|
|
assert not sessions.library_available(library)
|
|
|
|
|
|
def test_passive_handoff_reports_final_unconfirmed_state_without_proof(tmp_path, child, monkeypatch):
|
|
from threading import Event
|
|
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {})
|
|
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library',
|
|
lambda *a, **kw: pytest.fail('startup observer released the library'))
|
|
terminal = Event()
|
|
sessions.handoff_session(path, evidence_root=tmp_path, observe_only=True,
|
|
progress=lambda value: terminal.set() if value['status'] == 'UNCONFIRMED' else None)
|
|
child.terminate(); child.wait(5)
|
|
assert terminal.wait(3)
|
|
states = sessions.retained_session_states(tmp_path/'lib.lmc')
|
|
assert len(states) == 1 and states[0]['status'] == 'UNCONFIRMED'
|
|
assert states[0]['detail']
|
|
assert not sessions.library_available(tmp_path/'lib.lmc')
|
|
|
|
|
|
def test_explicit_release_marks_discovered_history_without_an_in_memory_observer(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock as locks, residual_sessions as residual
|
|
library = tmp_path/'library/lib.lmc'
|
|
library.parent.mkdir(); library.touch()
|
|
root = tmp_path/'history'; root.mkdir()
|
|
path = root/'session.json'
|
|
sessions.write_session_record(path, dict(operation_id='old-session', library=str(library), processes=[]))
|
|
found = sessions.discover_sessions(root)
|
|
assert len(found) == 1
|
|
monkeypatch.setattr(residual, 'query_process_snapshot', lambda: [])
|
|
@contextmanager
|
|
def proof(*args, **kwargs):
|
|
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
|
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
|
outcome = residual.release_residual_sessions(dict(library=str(library), sessions=[], runtime_sessions=found),
|
|
tmp_path/'release')
|
|
assert outcome['status'] == 'RELEASE_CONFIRMED'
|
|
assert sessions.discover_sessions(root) == []
|