"""Resource snapshots stay conservative during slow, failed and repeated reads.""" import os os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen') from threading import Event from dx_productivity.trial_task_center.ui.resource_status import ResourceStatus def test_resource_read_failure_blocks_until_a_successful_recheck(qtbot): outcome = [()] def read(): if isinstance(outcome[0], Exception): raise outcome[0] return outcome[0] status = ResourceStatus(read) try: assert status.blocked and not status.known status.refresh() qtbot.waitUntil(lambda: not status.busy) assert status.known and not status.blocked outcome[0] = OSError('resource directory unavailable') status.refresh() qtbot.waitUntil(lambda: not status.busy) assert status.blocked and isinstance(status.error, OSError) outcome[0] = () status.refresh() qtbot.waitUntil(lambda: not status.busy) assert not status.blocked and status.error is None finally: status.close() def test_operation_completion_coalesces_recheck_and_ignores_the_stale_clear_snapshot(qtbot): started, release = Event(), Event() calls, states = [], [] def read(): calls.append(1) if len(calls) == 1: started.set() assert release.wait(5) return () return ({'state': 'blocked'},) status = ResourceStatus(read) status.changed.connect(lambda: states.append(status.blocked)) try: status.refresh() qtbot.waitUntil(started.is_set) for _ in range(10): status.refresh() release.set() qtbot.waitUntil(lambda: not status.busy) assert len(calls) == 2 assert status.records == ({'state': 'blocked'},) assert states == [True], 'a pre-operation snapshot must not briefly unlock CAD actions' finally: release.set() status.close() def test_polling_does_not_restart_a_slow_resource_read(qtbot): started, release = Event(), Event() calls = [] def read(): calls.append(1) started.set() assert release.wait(5) return () status = ResourceStatus(read) try: status.refresh() qtbot.waitUntil(started.is_set) for _ in range(10): status._poll() release.set() qtbot.waitUntil(lambda: not status.busy) assert status.known and not status.blocked assert len(calls) == 1 finally: release.set() status.close() def test_close_cancels_polling_and_drops_late_resource_results(qtbot): started, release = Event(), Event() changes = [] def read(): started.set() assert release.wait(5) return () status = ResourceStatus(read) status.changed.connect(lambda: changes.append(True)) try: status.refresh() qtbot.waitUntil(started.is_set) status.close() assert not status._timer.isActive() release.set() qtbot.waitUntil(lambda: not status.busy) assert status.blocked and changes == [] finally: release.set() status.close()