"""Lease calls may wait on WNet; the Qt event loop must never wait for them.""" from pathlib import Path from threading import Event, get_ident from types import SimpleNamespace from PySide6.QtCore import QTimer from test_ui_filters import load_window def test_late_lease_callback_does_not_refresh_destroyed_view(qtbot, tmp_path, monkeypatch): import shiboken6 window = load_window(qtbot, tmp_path, monkeypatch) entered, release = Event(), Event() def action(): entered.set() assert release.wait(3) try: window._lease_action(action) qtbot.waitUntil(entered.is_set) window.cbb_table.deleteLater() qtbot.waitUntil(lambda: not shiboken6.isValid(window.cbb_table)) release.set() qtbot.waitUntil(lambda: window._lease_pending == 0) finally: release.set() window.pool.waitForDone(3000) def slow_lease(main_thread): entered, release, closed = Event(), Event(), Event() calls = [] def retain(): assert get_ident() != main_thread, 'retain blocked the Qt thread' calls.append('retain') entered.set() assert release.wait(3) def close(): assert get_ident() != main_thread, 'close blocked the Qt thread' calls.append('close') closed.set() lease = SimpleNamespace(mentor_path=Path('test.lmc'), retain_for_recovery=retain, close=close) return lease, entered, release, closed, calls def test_unsafe_background_retains_before_a_later_cross_library_close(qtbot, tmp_path, monkeypatch): from cbb_refresh_verify.verify_runner import VerifyResult window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False)) lease, entered, release, closed, calls = slow_lease(get_ident()) window.library_lease = lease try: window._run(['Beta'], maintenance=True) qtbot.waitUntil(entered.is_set) ticks = [] QTimer.singleShot(0, lambda: ticks.append(True)) window.lmc_edit.setText('other.lmc') qtbot.waitUntil(lambda: bool(ticks)) assert window.search_edit.isEnabled() and not closed.is_set() release.set() qtbot.waitUntil(closed.is_set) assert calls == ['retain', 'close'] finally: release.set() qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0) def test_management_dialog_finish_waits_for_mapping_cleanup_without_freezing_filters(qtbot, tmp_path, monkeypatch): from cbb_refresh_verify.management import ManagementResult window = load_window(qtbot, tmp_path, monkeypatch) library = Path(window.mentor_lmc_path) window._management_operation_library = library window._management_unsafe = True window._management_unsafe_library = library window.busy = True entered, release = Event(), Event() main_thread = get_ident() def restore(*args, **kwargs): assert get_ident() != main_thread, 'mapping release blocked the Qt thread' entered.set() assert release.wait(3) monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore) proof = {'status': 'RELEASE_CONFIRMED', 'library': str(library)} result = ManagementResult('remove', library, 'Beta', 'Logical-Physical', None, False, 'complete', 'BLOCKED', True, release_proof=proof) try: window._management_completed([result]) window._management_closed() qtbot.waitUntil(entered.is_set) assert window.busy and window.search_edit.isEnabled() and window.status_filter.isEnabled() assert window._management_library_unsafe(library) release.set() qtbot.waitUntil(lambda: not window.busy) assert not window._management_library_unsafe(library) finally: release.set() qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0) def test_release_dialog_finish_cannot_clear_busy_before_mapping_cleanup(qtbot, tmp_path, monkeypatch): window = load_window(qtbot, tmp_path, monkeypatch) library = Path(window.mentor_lmc_path) entered, release = Event(), Event() main_thread = get_ident() def restore(*args, **kwargs): assert get_ident() != main_thread, 'mapping release blocked the Qt thread' entered.set() assert release.wait(3) monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore) window.busy = True try: window._complete_library_release(library, {'status': 'RELEASE_CONFIRMED', 'library': str(library), 'evidence_path': 'proof.json'}) window._release_dialog_finished(0) qtbot.waitUntil(entered.is_set) assert window.busy and window.search_edit.isEnabled() release.set() qtbot.waitUntil(lambda: not window.busy) finally: release.set() qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0) def test_window_close_waits_asynchronously_for_retain_and_all_pool_work(qtbot, tmp_path, monkeypatch): window = load_window(qtbot, tmp_path, monkeypatch) lease, entered, release, closed, calls = slow_lease(get_ident()) window.library_lease = SimpleNamespace(mentor_path=Path('other.lmc'), close=lease.close) window._native_backup_session = SimpleNamespace(path_lease=lease, library=Path('test.lmc')) window.show() try: window.close() qtbot.waitUntil(entered.is_set) ticks = [] QTimer.singleShot(0, lambda: ticks.append(True)) qtbot.waitUntil(lambda: bool(ticks)) assert window.isVisible() and not closed.is_set() release.set() qtbot.waitUntil(lambda: not window.isVisible()) assert calls == ['retain', 'close'] assert window.pool.activeThreadCount() == 0 finally: release.set() qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0) window._native_backup_session = None def test_duplicate_dialog_finish_schedules_only_one_catalog_reload(qtbot, tmp_path, monkeypatch): from cbb_refresh_verify.management import ManagementResult window = load_window(qtbot, tmp_path, monkeypatch) library = Path(window.mentor_lmc_path) window._management_operation_library = library window.busy = True entered, release = Event(), Event() reloads = [] monkeypatch.setattr(window, 'load_library', lambda: reloads.append(True)) def restore(*args, **kwargs): entered.set() assert release.wait(3) monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore) result = ManagementResult('remove', library, 'Beta', 'Logical-Physical', None, False, 'complete', 'REMOVED', True, release_proof={'status': 'RELEASE_CONFIRMED', 'library': str(library)}) try: window._management_completed([result]) window._management_closed(); window._management_closed() qtbot.waitUntil(entered.is_set) release.set() qtbot.waitUntil(lambda: not window.busy) qtbot.waitUntil(lambda: bool(reloads)) assert reloads == [True] finally: release.set() qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0) def test_window_close_tolerates_fake_and_deleted_dialog_pools(qtbot, tmp_path, monkeypatch): from PySide6.QtCore import QThreadPool from shiboken6 import delete window = load_window(qtbot, tmp_path, monkeypatch) deleted_pool = QThreadPool() delete(deleted_pool) window.release_dialog = SimpleNamespace() window.management_dialog = SimpleNamespace(pool=deleted_pool) window.show() window.close() qtbot.waitUntil(lambda: not window.isVisible())