from threading import Event, Thread from cbb_refresh_verify.operation_queue import NativeQueue, QueueCancelled def test_paused_ticket_yields_to_a_ready_native_task(): queue=NativeQueue() ready, waiting, complete=Event(),Event(),Event() order=[] thread=Thread(target=lambda:queue.run(lambda:(order.append('resumed'),complete.set()), ready=ready.is_set,waiting=waiting.set)) thread.start() try: assert waiting.wait(2) queue.run(lambda:order.append('ready')) assert order==['ready'] ready.set();queue.wake() assert complete.wait(2) assert order==['ready','resumed'] finally: ready.set();queue.wake();thread.join(2) assert not thread.is_alive() def test_cancelled_paused_ticket_does_not_hold_later_work(): queue=NativeQueue() waiting,stop=Event(),Event() outcome=[] def wait(): try: queue.run(lambda:outcome.append('unexpected'),ready=lambda:False, waiting=waiting.set,stop=stop) except QueueCancelled:outcome.append('cancelled') thread=Thread(target=wait);thread.start() try: assert waiting.wait(2) stop.set();queue.wake();thread.join(2) assert not thread.is_alive() queue.run(lambda:outcome.append('next')) assert outcome==['cancelled','next'] finally: stop.set();queue.wake();thread.join(2) def test_saved_database_wait_yields_only_to_other_library_roots(tmp_path): from concurrent.futures import ThreadPoolExecutor from threading import Event from cbb_refresh_verify.operation_queue import BatchTaskContext, allow_other_libraries queue = NativeQueue() waiting, released = Event(), Event() same_directory_started = Event() current = BatchTaskContext(str(tmp_path/'first/a.lmc')) same_directory = BatchTaskContext(str(tmp_path/'first/b.lmc')) independent = BatchTaskContext(str(tmp_path/'second/c.lmc')) def saved(): with allow_other_libraries(): waiting.set() assert released.wait(4) return 'confirmed' with ThreadPoolExecutor(max_workers=3) as threads: first = threads.submit(queue.run, saved, context=current) assert waiting.wait(2) second = threads.submit(queue.run, same_directory_started.set, context=same_directory) try: third = threads.submit(queue.run, lambda: 'independent', context=independent) assert third.result(timeout=2) == 'independent' assert not same_directory_started.is_set() finally: released.set() assert first.result(timeout=2) == 'confirmed' second.result(timeout=2) assert same_directory_started.is_set()