Files
cbb-refresh-verify-795/tests/test_issue15_task_lifecycle.py
T

725 lines
30 KiB
Python

"""Cancellation must release scheduling, never invent native release."""
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from threading import Event, Thread
import time
import pytest
import win32con
import win32event
from cbb_refresh_verify import library_lock as locks
from test_management import setup as management_setup
@pytest.mark.parametrize('stage', ['read', 'archive'])
def test_stalled_checkpoint_does_not_own_native_channel_or_commit_after_stop(tmp_path, monkeypatch, stage):
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
marker = locks.blocker_path(library)
original = marker.read_bytes()
entered, resume, stop = Event(), Event(), Event()
read, write = Path.read_bytes, Path.write_bytes
def read_bytes(path):
if stage == 'read' and path == marker and not resume.is_set():
entered.set()
assert resume.wait(5)
return read(path)
def write_bytes(path, data):
if stage == 'archive' and path.name.startswith('blocker-before-'):
entered.set()
assert resume.wait(5)
return write(path, data)
monkeypatch.setattr(Path, 'read_bytes', read_bytes)
monkeypatch.setattr(Path, 'write_bytes', write_bytes)
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
with ThreadPoolExecutor(1) as pool:
task = pool.submit(locks.release_retained_library, library, tmp_path/'evidence', stop=stop)
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = False
try:
assert entered.wait(3)
acquired = win32event.WaitForSingleObject(mutex, 0) in (
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
assert acquired, 'checkpoint I/O retained the host native channel'
if stage == 'archive':
import json
receipt = next((tmp_path/'evidence').rglob('release.json'))
assert json.loads(receipt.read_text())['lease_state'] != 'RELEASED'
stop.set()
resume.set()
with pytest.raises(InterruptedError):
task.result(5)
assert read(marker) == original
assert locks.has_retained_library(library)
finally:
stop.set()
resume.set()
if acquired:
win32event.ReleaseMutex(mutex)
mutex.Close()
try:
task.result(5)
except InterruptedError:
pass
with locks._guard:
owner = locks._owners.pop(lease.key, None)
if owner:
owner['handle'].Close()
def test_task_context_is_frozen_pure_and_has_an_independent_monotonic_stop(tmp_path, monkeypatch):
from dataclasses import FrozenInstanceError
from cbb_refresh_verify.operation_queue import BatchTaskContext
monkeypatch.setattr(Path, 'resolve', lambda *a, **k: pytest.fail('context performed filesystem admission'))
first = BatchTaskContext(library=str(tmp_path/'A.lmc'), aliases=('Z:/A.lmc',), targets=('A',))
second = BatchTaskContext(library=str(tmp_path/'B.lmc'))
with pytest.raises(FrozenInstanceError):
first.library = second.library
first.cancel()
assert first.stop.is_set() and not second.stop.is_set()
assert not hasattr(first.stop, 'clear')
received = []
first.guard_callback(received.append)('CANCELLED')
assert received == ['CANCELLED']
first.close(timeout_seconds=0)
first.guard_callback(received.append)('late')
assert received == ['CANCELLED']
def test_bounded_queue_handoff_preserves_scope_and_fences_late_native_launch(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue, TaskHandoff, native_phase
queue = NativeQueue()
first = BatchTaskContext(library=str(tmp_path/'A.lmc'), targets=('A',))
second = BatchTaskContext(library=str(tmp_path/'B.lmc'), targets=('B',))
entered, resume, finished = Event(), Event(), Event()
effects = []
def stalled():
try:
entered.set()
assert resume.wait(3)
with native_phase():
effects.append('late native write')
finally:
finished.set()
try:
with pytest.raises(TaskHandoff) as error:
queue.run(stalled, context=first, timeout_seconds=.05)
assert entered.is_set() and error.value.library == first.library
assert first.snapshot()['pending'] == 1
assert queue.run(lambda: 'B ran', context=second) == 'B ran'
resume.set()
assert finished.wait(2)
assert effects == []
finally:
resume.set()
assert finished.wait(2)
def test_native_phase_is_never_yielded_at_metadata_deadline(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue, native_phase
queue = NativeQueue()
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
entered, resume, finished = Event(), Event(), Event()
results = []
def transaction():
with native_phase():
entered.set()
assert resume.wait(3)
return 'Remove and Add completed'
def run():
results.append(queue.run(transaction, context=context, timeout_seconds=.02))
finished.set()
thread = Thread(target=run)
thread.start()
try:
assert entered.wait(2)
assert not finished.wait(.1)
state = context.close(timeout_seconds=.02)
assert state['pending_native'] and queue.active
resume.set()
assert finished.wait(2)
assert results == ['Remove and Add completed']
finally:
resume.set()
thread.join(2)
def test_stalled_ready_predicate_does_not_hold_queue_condition():
from cbb_refresh_verify.operation_queue import NativeQueue, QueueCancelled
queue = NativeQueue()
entered, resume, stop, completed = Event(), Event(), Event(), Event()
effects = []
def slow_ready():
entered.set()
assert resume.wait(3)
return True
def first():
try:
queue.run(lambda: effects.append('A'), ready=slow_ready, stop=stop)
except QueueCancelled:
pass
thread = Thread(target=first)
thread.start()
second = Thread(target=lambda: (queue.run(lambda: effects.append('B')), completed.set()))
try:
assert entered.wait(2)
second.start()
assert completed.wait(.5), 'one ready predicate froze every other queue ticket'
stop.set()
resume.set()
thread.join(2)
assert effects == ['B']
finally:
stop.set()
resume.set()
thread.join(3)
second.join(3)
def test_verify_admission_handoff_unblocks_b_and_never_launches_after_read_returns(tmp_path, monkeypatch):
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue
from cbb_refresh_verify.verify_runner import VerifyRequest, run_verify
import win32process
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
marker = locks.blocker_path(library)
entered, resume, returned = Event(), Event(), Event()
read = Path.read_text
def withheld(path, *args, **kwargs):
if path == marker:
entered.set()
assert resume.wait(3)
returned.set()
return read(path, *args, **kwargs)
monkeypatch.setattr(Path, 'read_text', withheld)
monkeypatch.setattr(win32process, 'CreateProcess', lambda *a, **k: pytest.fail('late native launch'))
context = BatchTaskContext(library=str(library), targets=('A',))
events = []
try:
result = run_verify(VerifyRequest(library, 'A', tmp_path/'evidence'), context=context,
admission_timeout_seconds=.05, progress=events.append)
assert entered.is_set() and result.status == 'TIMEOUT'
assert not result.safe_to_continue
assert result.release_proof['resource_scope']['operation_ids'] == [context.operation_id]
def unrelated():
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = win32event.WaitForSingleObject(mutex, 0) == win32con.WAIT_OBJECT_0
try:
assert acquired
return 'B completed'
finally:
if acquired: win32event.ReleaseMutex(mutex)
mutex.Close()
assert NativeQueue().run(unrelated) == 'B completed'
count = len(events)
resume.set()
assert returned.wait(2)
limit = time.monotonic()+2
while context.snapshot()['pending'] and time.monotonic() < limit:
time.sleep(.01)
assert context.snapshot()['pending'] == 0
assert len(events) == count
finally:
resume.set()
context.close(timeout_seconds=2)
with locks._guard:
owner = locks._owners.pop(lease.key, None)
if owner: owner['handle'].Close()
def test_native_time_does_not_consume_next_metadata_phase_budget(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext, native_phase
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
stages = []
def action():
with native_phase():
time.sleep(.12)
stages.append('saved')
time.sleep(.02)
with native_phase():
stages.append('closed')
return stages
assert context.run(action, timeout_seconds=.06) == ['saved', 'closed']
def test_native_channel_enforces_cross_thread_serialization_and_reentrancy():
from cbb_refresh_verify.operation_queue import native_channel
with native_channel():
with native_channel():
with ThreadPoolExecutor(1) as pool:
def contend():
with native_channel():
pytest.fail('concurrent native writer admitted')
with pytest.raises(RuntimeError, match='NATIVE_CHANNEL_BUSY'):
pool.submit(contend).result(2)
with native_channel():
pass
def test_maintenance_checkpoint_admission_does_not_hold_native_channel(tmp_path, monkeypatch):
from contextlib import nullcontext
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
with locks.LibraryLock(library, safe_to_release=lambda: False) as retained:
pass
marker = locks.blocker_path(library)
entered, resume = Event(), Event()
original = Path.read_bytes
def withheld(path):
if path == marker and not resume.is_set():
entered.set()
assert resume.wait(3)
return original(path)
monkeypatch.setattr(Path, 'read_bytes', withheld)
def admit():
with locks.LibraryLock.for_maintenance(library, tmp_path/'archive', lambda: nullcontext({})):
pass
with ThreadPoolExecutor(1) as pool:
task = pool.submit(admit)
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = False
try:
assert entered.wait(2)
acquired = win32event.WaitForSingleObject(mutex, 0) == win32con.WAIT_OBJECT_0
assert acquired
finally:
if acquired: win32event.ReleaseMutex(mutex)
mutex.Close()
resume.set()
task.result(5)
with locks._guard:
owner = locks._owners.pop(retained.key, None)
if owner: owner['handle'].Close()
def test_native_handoff_keeps_real_mutex_until_bound_process_returns(tmp_path):
import subprocess
import sys
from cbb_refresh_verify.operation_queue import (
BatchTaskContext, NativeQueue, TaskHandoff, hold_native_handoff, native_channel)
from cbb_refresh_verify.session_record import write_session_record
process = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
record = tmp_path/'session.json'
write_session_record(record, dict(operation_id=context.operation_id, library=context.library,
worker_pid=process.pid))
queue = NativeQueue()
effects = []
def action():
with native_channel():
hold_native_handoff(process, record)
with native_channel():
effects.append('late mutation')
try:
with pytest.raises(TaskHandoff):
queue.run(action, context=context)
assert process.poll() is None
assert context.snapshot()['pending_native']
assert queue.run(lambda: 'pure read') == 'pure read'
with pytest.raises(RuntimeError, match='NATIVE_CHANNEL_BUSY'):
with native_channel():
pytest.fail('writer overlapped handed-off native call')
process.communicate('\n', timeout=3)
limit = time.monotonic()+3
while context.snapshot()['pending'] and time.monotonic() < limit:
time.sleep(.01)
assert context.snapshot()['pending'] == 0
with native_channel():
pass
assert effects == []
finally:
if process.poll() is None:
process.communicate('\n', timeout=3)
context.close(timeout_seconds=3)
def test_worker_write_barrier_survives_supervisor_process_exit(tmp_path):
import json
import subprocess
import sys
from cbb_refresh_verify.operation_queue import native_channel
ready, finish, done = (tmp_path/name for name in ('ready', 'finish', 'done'))
child = '''import sys,time,os
from pathlib import Path
from cbb_refresh_verify.desktop_worker import worker_native_channel
with worker_native_channel():
Path(sys.argv[1]).write_text(str(os.getpid()))
while not Path(sys.argv[2]).exists(): time.sleep(.02)
Path(sys.argv[3]).touch()
'''
parent = '''import subprocess,sys,time,json
from pathlib import Path
subprocess.Popen([sys.executable,'-c',sys.argv[1],*sys.argv[2:]])
deadline=time.monotonic()+4
while not Path(sys.argv[2]).exists() and time.monotonic()<deadline: time.sleep(.02)
raise SystemExit(0 if Path(sys.argv[2]).exists() else 2)
'''
supervisor = subprocess.Popen([sys.executable, '-c', parent, child, str(ready), str(finish), str(done)])
try:
assert supervisor.wait(timeout=6) == 0
assert ready.is_file() and not done.exists()
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
with native_channel():
pytest.fail('restart admitted a second writer')
finally:
finish.touch()
deadline = time.monotonic()+3
while not done.exists() and time.monotonic()<deadline: time.sleep(.02)
supervisor.wait(timeout=3)
assert done.is_file()
with native_channel():
pass
def test_exact_sidecar_handle_survives_parent_exit_until_worker_boundary(tmp_path):
import subprocess
import sys
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
ready, finish, done, handle = (tmp_path/name for name in ('ready', 'finish', 'done', 'handle'))
child = '''import sys,time
from pathlib import Path
from cbb_refresh_verify.desktop_worker import worker_native_channel
while not Path(sys.argv[4]).exists(): time.sleep(.02)
with worker_native_channel(lease_handle=int(Path(sys.argv[4]).read_text())):
Path(sys.argv[1]).touch()
while not Path(sys.argv[2]).exists(): time.sleep(.02)
Path(sys.argv[3]).touch()
'''
parent = '''import subprocess,sys,time,win32api,win32con
from pathlib import Path
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
with LibraryLock(sys.argv[1],checkpoint=False):
child=subprocess.Popen([sys.executable,'-c',sys.argv[2],*sys.argv[3:]])
process=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,child.pid)
try: handle=duplicate_worker_lease(sys.argv[1],process)
finally: process.Close()
Path(sys.argv[6]).write_text(str(handle))
deadline=time.monotonic()+4
while not Path(sys.argv[3]).exists() and time.monotonic()<deadline: time.sleep(.02)
assert Path(sys.argv[3]).exists()
'''
supervisor = subprocess.Popen([sys.executable, '-c', parent, str(library), child,
str(ready), str(finish), str(done), str(handle)])
try:
assert supervisor.wait(timeout=6) == 0
with pytest.raises(locks.LibraryBusy):
with locks.LibraryLock(library, checkpoint=False):
pytest.fail('uploader could replace a live worker library')
finally:
finish.touch()
deadline = time.monotonic()+3
while ready.exists() and not done.exists() and time.monotonic()<deadline: time.sleep(.02)
supervisor.wait(timeout=3)
assert done.is_file()
with locks.LibraryLock(library, checkpoint=False):
pass
@pytest.mark.parametrize('obstacle', ['', 'unknown_title', 'duplicate', 'changed'])
def test_failed_startup_dismisses_only_exact_stable_owned_tip(tmp_path, monkeypatch, obstacle):
import win32gui
from test_failed_startup import startup_fixture
from cbb_refresh_verify.failed_session import close_failed_startup
job, records, messages = startup_fixture(tmp_path, monkeypatch)
visible = {100, 200}
reads = []
classes = {100: 'Afx:main', 200: '#32770', 201: 'Button', 202: 'Button'}
def title(hwnd):
if hwnd == 100: return 'Expedition PCB'
if hwnd == 200:
reads.append(hwnd)
return 'Unknown' if obstacle == 'unknown_title' or obstacle == 'changed' and len(reads)>1 else 'Tip of the Day'
return 'OK'
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(h, arg) for h in sorted(visible)])
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg:
[fn(h, arg) for h in ((201, 202) if obstacle == 'duplicate' else (201,))] if hwnd == 200 else None)
monkeypatch.setattr(win32gui, 'GetClassName', classes.__getitem__)
monkeypatch.setattr(win32gui, 'GetWindowText', title)
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda hwnd: 1)
def post(hwnd, message, wparam, lparam):
messages.append((hwnd, message))
if hwnd == 201: visible.remove(200)
elif hwnd == 100: records.clear()
monkeypatch.setattr(win32gui, 'PostMessage', post)
result = close_failed_startup(job, 12, timeout_seconds=0)
if obstacle:
assert result['status'] == 'RETAINED' and not messages
else:
assert result['status'] == 'CLOSED'
assert messages == [(201, win32con.BM_CLICK), (100, win32con.WM_CLOSE)]
def test_late_release_recheck_is_bounded_and_keeps_business_failure(tmp_path, monkeypatch):
from cbb_refresh_verify.operation_lifecycle import recheck_failed_release
from cbb_refresh_verify.verify_runner import VerifyResult
from cbb_refresh_verify.session_record import write_session_record
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
evidence = tmp_path/'evidence'
evidence.mkdir()
result = VerifyResult('FAIL', stderr='native business failure', safe_to_continue=False,
evidence_path=evidence/'result.json')
result.evidence_path.write_text('{}')
record = evidence/'session.json'
write_session_record(record, dict(operation_id='failed', library=str(library), block='A', processes=[]))
entered, resume = Event(), Event()
original = Path.open
def withheld(path, *args, **kwargs):
if path == record:
entered.set()
assert resume.wait(3)
return original(path, *args, **kwargs)
monkeypatch.setattr(Path, 'open', withheld)
try:
started = time.monotonic()
returned = recheck_failed_release(library, 'A', result, timeout_seconds=.05)
assert time.monotonic()-started < .8
assert entered.is_set() and returned is result
assert returned.status == 'FAIL' and not returned.safe_to_continue
finally:
resume.set()
def test_batch_context_cancel_records_all_remaining_items_and_new_batch_can_run(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult, run_batch
requests = [VerifyRequest(tmp_path/'lib.lmc', name, tmp_path/'evidence') for name in ('A', 'B', 'C')]
first = BatchTaskContext(library=str(requests[0].lmc_file), targets=('A', 'B', 'C'))
events = []
def verify(request):
first.cancel()
return VerifyResult('CANCELLED')
results = run_batch(requests, verifier=verify, context=first,
progress=lambda name, result: events.append((name, result.status)))
assert [r.status for r in results] == ['CANCELLED', 'SKIPPED', 'SKIPPED']
assert events == [('A', 'CANCELLED'), ('B', 'SKIPPED'), ('C', 'SKIPPED')]
second = BatchTaskContext(library=str(requests[0].lmc_file), targets=('A',))
assert run_batch(requests[:1], verifier=lambda _: VerifyResult('PASS'), context=second)[0].status == 'PASS'
def test_batch_retention_matches_normalized_library_alias_not_business_failure(tmp_path):
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult, run_batch
library = tmp_path/'A/lib.lmc'
alias = tmp_path/'A/sub/../lib.lmc'
requests = [VerifyRequest(path, block, tmp_path/'evidence') for path, block in (
(library, 'A'), (alias, 'B'), (tmp_path/'B/lib.lmc', 'C'), (tmp_path/'B/lib.lmc', 'D'))]
calls = []
def verify(request):
calls.append(request.block_name)
return VerifyResult('FAIL', safe_to_continue=request.block_name != 'A')
results = run_batch(requests, verifier=verify)
assert calls == ['A', 'C', 'D']
assert [r.status for r in results] == ['FAIL', 'SKIPPED', 'FAIL', 'FAIL']
def test_queue_close_has_one_deadline_and_reports_pending_scope(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue, TaskHandoff
queue = NativeQueue()
context = BatchTaskContext(library=str(tmp_path/'A.lmc'), aliases=('Z:/A.lmc',))
entered, resume, finished = Event(), Event(), Event()
def action():
entered.set()
resume.wait(3)
def run():
try:
queue.run(action, context=context)
except TaskHandoff:
pass
finally:
finished.set()
thread = Thread(target=run)
thread.start()
try:
assert entered.wait(2)
started = time.monotonic()
states = queue.close(timeout_seconds=.05)
assert time.monotonic()-started < .5
assert states[0]['resource_scope']['aliases'] == ['Z:/A.lmc']
assert states[0]['pending'] == 1
assert finished.wait(1)
finally:
resume.set()
thread.join(2)
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup):
from dataclasses import replace
from test_management import execute
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue
request, adapter, source = management_setup
original = adapter.remove
def slow_remove(*args):
result = original(*args)
time.sleep(.4)
return result
adapter.remove = slow_remove # Native external boundary; transaction stays real.
context = BatchTaskContext(library=str(request.library), targets=('Block',))
result = NativeQueue().run(lambda: execute(replace(request, operation='update',
source=source, auto_verify=False), adapter), context=context, timeout_seconds=.2)
assert result.status == 'UPLOADED'
assert adapter.calls[0] == 'remove' and adapter.calls[1][0] == 'add'
def test_busy_native_channel_before_dispatch_does_not_retain_management_library(management_setup):
from test_management import execute
from cbb_refresh_verify.operation_queue import native_channel
request, adapter, _ = management_setup
entered, resume = Event(), Event()
def owner():
with native_channel():
entered.set()
resume.wait(3)
thread = Thread(target=owner)
thread.start()
try:
assert entered.wait(2)
result = execute(request, adapter)
assert result.safe_to_continue and not adapter.calls
with locks.LibraryLock(request.library):
pass
finally:
resume.set()
thread.join(3)
def test_only_explicit_repair_inspection_can_enter_while_worker_owns_writer_barrier(tmp_path):
import json
import os
from cbb_refresh_verify.desktop_worker import worker_native_channel, run_native_worker
from cbb_refresh_verify.operation_queue import native_channel
from cbb_refresh_verify.native_repair_ui import repair_worker_main
entered, resume = Event(), Event()
def owner():
with worker_native_channel():
entered.set()
resume.wait(3)
thread = Thread(target=owner)
thread.start()
job = dict(root=str(tmp_path), project=str(tmp_path/'A.prj'), database=str(tmp_path/'database'),
cancel_signal=str(tmp_path/'cancel'), supervisor_pid=os.getpid(), desktop='not-private')
path = tmp_path/'repair-request.json'
try:
assert entered.wait(2)
with native_channel(inspection_only=True):
pass
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
with native_channel():
pass
for mode in (False, 'true'):
path.write_text(json.dumps({**job, 'inspection_only': mode}))
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
run_native_worker(repair_worker_main, path)
path.write_text(json.dumps({**job, 'inspection_only': True}))
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
run_native_worker(lambda _: pytest.fail('arbitrary worker exempted'), path)
assert run_native_worker(repair_worker_main, path) == 1
result = json.loads((tmp_path/'repair-result.json').read_text())
assert result['native_started'] is False
assert 'REPAIR_PRIVATE_DESKTOP_REQUIRED' in result['detail']
finally:
resume.set()
thread.join(3)
def test_inspection_session_does_not_take_over_retained_writer_lease(tmp_path):
import os
import win32api
from cbb_refresh_verify.desktop_worker import DesktopWorker
library = tmp_path/'A/lib.lmc'
library.parent.mkdir()
library.touch()
with locks.LibraryLock(library, safe_to_release=lambda: False) as lease:
pass
worker = object.__new__(DesktopWorker)
worker.handle, worker.pid = win32api.GetCurrentProcess(), os.getpid()
try:
worker.bind_session(dict(operation_id='inspection', operation='inspect', inspection_only=True,
library=str(library), project=str(tmp_path/'A.prj'), session_record_path=str(tmp_path/'session.json')))
assert locks.has_retained_library(library)
from cbb_refresh_verify.session_record import read_session_record
record = read_session_record(tmp_path/'session.json', evidence_root=tmp_path)
assert record['worker_lease_handle'] is None
finally:
with locks._guard:
owner = locks._owners.pop(lease.key, None)
if owner: owner['handle'].Close()
def test_handoff_preserves_known_business_result_and_save_facts(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext, TaskHandoff, remember_result
from cbb_refresh_verify.verify_runner import VerifyResult
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
native_result = VerifyResult('FAIL', stderr='native failure after Save', safe_to_continue=False,
write_outcome=dict(save_state='COMMITTED', close_state='UNKNOWN'))
resume, done = Event(), Event()
def finalize():
remember_result(native_result)
try:
resume.wait(3)
finally:
done.set()
try:
with pytest.raises(TaskHandoff) as error:
context.run(finalize, timeout_seconds=.05)
assert error.value.result is native_result
assert error.value.result.write_outcome['save_state'] == 'COMMITTED'
assert error.value.result.status == 'FAIL'
from cbb_refresh_verify.verify_runner import _handoff_verify_result
returned = _handoff_verify_result(error.value)
assert returned.status == 'FAIL' and returned.stderr == native_result.stderr
assert returned.write_outcome == native_result.write_outcome and not returned.safe_to_continue
error.value.result = VerifyResult('PASS', write_outcome=native_result.write_outcome)
unfinished = _handoff_verify_result(error.value)
assert unfinished.status == 'FAIL' and unfinished.library_flag == 'UNKNOWN'
assert not unfinished.safe_to_continue
finally:
resume.set()
assert done.wait(2)
def test_registered_writer_owns_process_handle_after_callers_handle_closes(monkeypatch):
import subprocess
import sys
from cbb_refresh_verify import desktop_worker
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
process = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
entered, done = Event(), Event()
errors = []
def run():
try:
with desktop_worker.worker_native_channel():
desktop_worker.register_native_writer(process)
process._handle.Close()
entered.set()
except Exception as error:
errors.append(error)
finally:
done.set()
thread = Thread(target=run)
thread.start()
try:
assert entered.wait(2)
assert not done.wait(.1), 'a closed caller handle released an unreturned writer'
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
desktop_worker.assert_native_writer_idle()
finally:
process.stdin.write('\n')
process.stdin.flush()
process.stdin.close()
thread.join(3)
# The duplicate's observed exit is authoritative; the original Popen
# handle was deliberately invalidated, so do not poll it in __del__.
process.returncode = 0
assert done.is_set() and not errors