test(runtime): verify external desktop references block batch cleanup safely

This commit is contained in:
阿白叔 committed 2026-10-21 15:36:52 +08:00
1 parent 4b78ca0f27
commit 486ea05ad5
1 file changed
+49
+49
View File
@@ -0,0 +1,49 @@
"""An external Windows reference blocks a batch without replaying its results."""
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
from dx_productivity.trial_task_center.batch_runtime import (
BatchCadRuntime, BatchResourceCleanupError, cleanup_pending_resources, guard_pending_resources)
from dx_productivity.trial_task_center.cad_identity import current_desktop_name
from dx_productivity.trial_task_center.isolated_desktop import _Win32Api, build_startup_info
@pytest.mark.skipif(os.name != 'nt', reason='Windows desktop object lifetime')
def test_external_reference_preserves_completed_result_until_resource_only_cleanup(tmp_path):
before = current_desktop_name()
runtime = BatchCadRuntime(tmp_path, 'execution')
external = None
try:
with pytest.raises(BatchResourceCleanupError) as caught:
with runtime:
with runtime.native_scope(tmp_path / 'completed-operation') as scope:
# The test owns this independent actor, outside runtime's
# process/job list. The runtime must leave it alive.
external = _Win32Api().create_process((sys.executable, '-u', '-c',
'import win32service,time,sys; '
'd=win32service.OpenDesktop(sys.argv[1],0,False,0x10000000); '
'd.SetThreadDesktop();print("ready",flush=True);time.sleep(30)', scope.name),
build_startup_info(scope.name).lpDesktop, None, None,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
assert external.stdout.readline() == b'ready\r\n'
runtime.result = 'already saved and independently verified'
assert caught.value.completed_result == runtime.result
assert external.poll() is None
assert json.loads(runtime.path.read_text())['state'] == 'blocked'
with pytest.raises(BatchResourceCleanupError):
guard_pending_resources(tmp_path)
assert not list(Path(tmp_path).rglob('uncertain.flag'))
assert current_desktop_name() == before
finally:
if external is not None:
external.close() # Only the test closes its own independent actor.
assert cleanup_pending_resources(tmp_path) == ()
assert json.loads(runtime.path.read_text())['state'] == 'released'
assert runtime.result == 'already saved and independently verified'
assert runtime.generation == 1
guard_pending_resources(tmp_path)