91 lines
4.8 KiB
Python
91 lines
4.8 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
from threading import Event
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from cbb_refresh_verify import session_record as sessions
|
|
from cbb_refresh_verify.operation_queue import BatchTaskContext,NativeQueue
|
|
|
|
|
|
def observed(tmp_path,monkeypatch,*,status='ACTIVE',initializing=False,registered=True,alive=True,worker_alive=False):
|
|
library=tmp_path/'lib.lmc';key='record'
|
|
states={key:dict(status=status,observer_initializing=initializing,owned_worker_alive=worker_alive)}
|
|
monkeypatch.setattr(sessions,'_retained_libraries',{key:{sessions._library_key(library)}})
|
|
monkeypatch.setattr(sessions,'_retained_results',states)
|
|
monkeypatch.setattr(sessions,'_observers',{key:(SimpleNamespace(is_alive=lambda:alive),None)} if registered else {})
|
|
return library,states[key]
|
|
|
|
|
|
@pytest.mark.parametrize('initializing',[False,True])
|
|
def test_same_library_waits_for_actual_observer_and_other_library_can_run(tmp_path,monkeypatch,initializing):
|
|
library,state=observed(tmp_path,monkeypatch,status='UNCONFIRMED' if initializing else 'ACTIVE',initializing=initializing)
|
|
queue=NativeQueue();waiting=Event();entered=Event();stop=Event()
|
|
# Queue predicates may not perform filesystem/process probes.
|
|
monkeypatch.setattr(sessions,'query_process_snapshot',lambda *a,**kw:pytest.fail('queue probed native clients'),raising=False)
|
|
context=BatchTaskContext(str(library),native_policy='external')
|
|
with ThreadPoolExecutor(2) as pool:
|
|
future=pool.submit(queue.run,lambda:entered.set() or 'resumed',context=context,stop=stop,waiting=waiting.set)
|
|
try:
|
|
assert waiting.wait(.5) and not entered.is_set()
|
|
other=pool.submit(queue.run,lambda:'unrelated',context=BatchTaskContext(str(tmp_path/'other.lmc')))
|
|
assert other.result(1)=='unrelated'
|
|
state.update(status='RELEASED',observer_initializing=False)
|
|
assert future.result(2)=='resumed'
|
|
finally:
|
|
stop.set();queue.wake()
|
|
|
|
|
|
@pytest.mark.parametrize('case',['orphaned','dead_thread','unconfirmed','released'])
|
|
def test_stale_or_nonlive_cache_does_not_prevent_fresh_scoped_admission(tmp_path,monkeypatch,case):
|
|
library,state=observed(tmp_path,monkeypatch,status='RELEASED' if case=='released' else 'UNCONFIRMED',
|
|
registered=case!='orphaned',alive=case!='dead_thread')
|
|
assert not sessions.library_available(library) # Historical cache is deliberately stale.
|
|
assert NativeQueue().run(lambda:'fresh admission',context=BatchTaskContext(str(library)))=='fresh admission'
|
|
|
|
|
|
def test_live_owned_worker_still_blocks_when_its_journal_cannot_be_read(tmp_path,monkeypatch):
|
|
library,state=observed(tmp_path,monkeypatch,status='UNCONFIRMED',worker_alive=True)
|
|
queue=NativeQueue();waiting=Event();stop=Event();entered=Event()
|
|
with ThreadPoolExecutor(1) as pool:
|
|
future=pool.submit(queue.run,lambda:entered.set(),context=BatchTaskContext(str(library)),
|
|
stop=stop,waiting=waiting.set)
|
|
try:
|
|
assert waiting.wait(.5) and not entered.is_set()
|
|
state['owned_worker_alive']=False
|
|
future.result(2)
|
|
finally:
|
|
stop.set();queue.wake()
|
|
|
|
|
|
def test_actual_observer_initialization_closes_queue_race_then_unconfirmed_cache_releases_it(tmp_path,monkeypatch):
|
|
library=tmp_path/'lib.lmc';path=tmp_path/'session.json'
|
|
sessions.write_session_record(path,dict(operation_id='initializing',library=str(library)))
|
|
entered,finish,waiting,stop=Event(),Event(),Event(),Event()
|
|
def first_scan(*args,**kwargs):
|
|
entered.set();assert finish.wait(3)
|
|
return dict(status='UNCONFIRMED',operation_id='initializing',library=str(library),
|
|
detail='No live client; fresh admission must recheck resource proof')
|
|
monkeypatch.setattr(sessions,'_reconcile',first_scan)
|
|
queue=NativeQueue();thread=None
|
|
try:
|
|
sessions.handoff_session(path,evidence_root=tmp_path,observe_only=True,stop=stop)
|
|
assert entered.wait(2)
|
|
thread=sessions._observers[str(path.absolute()).casefold()][0]
|
|
assert not sessions.library_available(library,live_only=True)
|
|
with ThreadPoolExecutor(1) as pool:
|
|
future=pool.submit(queue.run,lambda:'fresh scoped admission',context=BatchTaskContext(str(library)),
|
|
waiting=waiting.set,stop=stop)
|
|
try:
|
|
assert waiting.wait(1) and not future.done()
|
|
finish.set()
|
|
assert future.result(2)=='fresh scoped admission'
|
|
finally:
|
|
stop.set();queue.wake()
|
|
thread.join(2)
|
|
assert not sessions.library_available(library) # Unproved historical state remains visible.
|
|
assert sessions.library_available(library,live_only=True)
|
|
finally:
|
|
finish.set();stop.set()
|
|
if thread:thread.join(2)
|