Core regression / Python core and release configuration (push) Successful in 17s
Core regression / JavaScript syntax (push) Successful in 2s
Core regression / Python core and release configuration (pull_request) Successful in 14s
Core regression / JavaScript syntax (pull_request) Successful in 3s
182 lines
8.3 KiB
Python
182 lines
8.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import subprocess
|
|
import pytest
|
|
|
|
from dx_productivity.trial_task_center.project_session import ProjectSessionWorker, ProjectSessionScope
|
|
from dx_productivity.trial_task_center.worker_monitor import ProgressWatch, WorkerMonitorError
|
|
|
|
|
|
def toy_worker(root):
|
|
script = root / 'toy_session.py'
|
|
script.write_text('''import pathlib,sys,time,json,os
|
|
root=pathlib.Path(sys.argv[1]); initial=sys.argv[4]; monitor=sys.argv[5]
|
|
while not (root/f'monitor-{monitor}.ready').exists():time.sleep(.005)
|
|
session=(root/'project-session.request').read_text(encoding='utf-8').split('\\t')[0]
|
|
job=initial; sequence=0
|
|
with (root/'starts.txt').open('a') as f:f.write(str(os.getpid())+'\\n')
|
|
while True:
|
|
sequence+=1
|
|
print(f'DX_PROGRESS\\t{monitor}\\t{initial}\\t{sequence}\\tsession_wait\\t1\\t1\\t1\\t{job}',flush=True)
|
|
(root/'project-session-result.tsv').write_text(f'{session}\\t{job}\\tsaved_ok\\tsession_verified',encoding='utf-8')
|
|
with (root/'steps.txt').open('a') as f:f.write(job+'\\n')
|
|
while not (root/'project-session-next.tsv').exists():time.sleep(.005)
|
|
# Match the real VBS continuation reader's bounded sharing-denial retry.
|
|
for attempt in range(20):
|
|
try:
|
|
token,job=(root/'project-session-next.tsv').read_text().split('\\t')
|
|
break
|
|
except (PermissionError,FileNotFoundError):time.sleep(.05)
|
|
else:raise RuntimeError('control read remained unavailable')
|
|
(root/'project-session-next.tsv').unlink()
|
|
if token!=session:sys.exit(8)
|
|
if job=='close':break
|
|
(root/'closed.txt').write_text('closed')
|
|
''', encoding='utf-8')
|
|
return (sys.executable, '-u', str(script), str(root), sys.executable, '', 'job1')
|
|
|
|
|
|
def test_same_project_steps_retain_one_supervised_worker_and_close(tmp_path):
|
|
command = toy_worker(tmp_path)
|
|
session = ProjectSessionWorker(tmp_path / '工程.prj', tmp_path)
|
|
try:
|
|
assert session.run_step(command, 3).returncode == 0
|
|
assert session.run_step((*command[:-1], 'job2'), 3).returncode == 0
|
|
finally:
|
|
session.close()
|
|
assert (tmp_path / 'closed.txt').exists()
|
|
assert (tmp_path / 'steps.txt').read_text().splitlines() == ['job1', 'job2']
|
|
assert len((tmp_path / 'starts.txt').read_text().splitlines()) == 1
|
|
assert not (tmp_path / 'project-session.request').exists()
|
|
|
|
|
|
def test_batch_runtime_retains_same_project_worker_without_owning_cad(tmp_path):
|
|
from dx_productivity.trial_task_center.worker_monitor import run_monitored_process
|
|
calls = []
|
|
class Runtime:
|
|
def run_worker(self, command, timeout, **options):
|
|
calls.append(command)
|
|
return run_monitored_process(command, timeout, **options)
|
|
runtime = Runtime()
|
|
command = toy_worker(tmp_path)
|
|
with ProjectSessionScope(None, enabled=True, private_desktop=True, runtime=runtime) as scope:
|
|
assert scope.run_step(tmp_path / 'board.prj', command, 3).returncode == 0
|
|
assert scope.run_step(tmp_path / 'board.prj', (*command[:-1], 'job2'), 3).returncode == 0
|
|
assert len(calls) == 1
|
|
assert (tmp_path / 'closed.txt').exists()
|
|
|
|
|
|
def test_session_progress_keeps_monitor_identity_and_resets_each_step():
|
|
watch = ProgressWatch('token', 'first', 10, 0)
|
|
first = b'DX_PROGRESS\ttoken\tfirst\t1\topen_project\t0\t1\t1\tfirst\n'
|
|
second = b'DX_PROGRESS\ttoken\tfirst\t2\tnative_replace\t0\t1\t0\tsecond\n'
|
|
assert watch.accept(first, 1)
|
|
assert watch.accept(second, 5)
|
|
value = watch.snapshot(now=6)
|
|
assert value['job_id'] == 'second'
|
|
assert value['stage_seconds'] == {'native_replace': 1}
|
|
assert value['elapsed_seconds'] == 1
|
|
assert not watch.accept(second.replace(b'\tfirst\t2', b'\tforeign\t3'), 6)
|
|
|
|
|
|
def test_real_vbs_session_authorization_fails_before_cad_for_missing_project(tmp_path):
|
|
script = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs'
|
|
project = tmp_path / 'missing.prj'
|
|
(tmp_path / 'command.tsv').write_text(f'job_id\tproject_path\njob\t{project}\n')
|
|
(tmp_path / 'batch.request').write_text('job')
|
|
(tmp_path / 'project-session.request').write_text(f'{"a"*32}\t{project}\tjob')
|
|
result = subprocess.run((r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(script),
|
|
str(tmp_path), sys.executable, '', 'job'),
|
|
capture_output=True, timeout=10, text=True)
|
|
assert result.returncode == 2
|
|
assert 'confirmed project file does not exist' in result.stderr
|
|
|
|
|
|
def test_switching_project_closes_previous_worker_before_starting_next(tmp_path):
|
|
roots = [tmp_path / 'first', tmp_path / 'second']
|
|
for root in roots:
|
|
root.mkdir()
|
|
with ProjectSessionScope(None, enabled=True) as scope:
|
|
assert scope.run_step(roots[0] / 'a.prj', toy_worker(roots[0]), 3).returncode == 0
|
|
scope.prepare(roots[1] / 'b.prj')
|
|
assert (roots[0] / 'closed.txt').exists()
|
|
assert scope.run_step(roots[1] / 'b.prj', toy_worker(roots[1]), 3).returncode == 0
|
|
assert (roots[1] / 'closed.txt').exists()
|
|
|
|
|
|
def test_wrong_session_checkpoint_cannot_release_next_step(tmp_path):
|
|
command = toy_worker(tmp_path)
|
|
script = Path(command[2])
|
|
script.write_text(script.read_text(encoding='utf-8').replace(
|
|
"f'{session}\\t{job}\\tsaved_ok\\tsession_verified'",
|
|
"f'wrong-token\\t{job}\\tsaved_ok\\tsession_verified'").replace(
|
|
"while not (root/'project-session-next.tsv').exists():time.sleep(.005)",
|
|
'sys.exit(0)'), encoding='utf-8')
|
|
session = ProjectSessionWorker(tmp_path / 'a.prj', tmp_path)
|
|
try:
|
|
with pytest.raises(WorkerMonitorError, match='before authorized closing'):
|
|
session.run_step(command, 3)
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def test_completed_failure_is_delivered_and_next_step_starts_fresh_worker(tmp_path):
|
|
command = toy_worker(tmp_path)
|
|
script = Path(command[2])
|
|
script.write_text(script.read_text(encoding='utf-8').replace(
|
|
'job=initial; sequence=0', "job=initial; sequence=0\nif job=='failed':sys.exit(7)"), encoding='utf-8')
|
|
project = tmp_path / 'a.prj'
|
|
with ProjectSessionScope(None, enabled=True) as scope:
|
|
failed = scope.run_step(project, (*command[:-1], 'failed'), 3)
|
|
assert failed.returncode == 7
|
|
previous = scope.worker
|
|
assert scope.run_step(project, command, 3).returncode == 0
|
|
assert scope.worker is not previous
|
|
assert (tmp_path / 'closed.txt').exists()
|
|
|
|
|
|
@pytest.mark.parametrize('release_reader', [True, False])
|
|
def test_real_vbs_session_result_write_handles_reader_contention(tmp_path, release_reader):
|
|
import os
|
|
import re
|
|
from threading import Timer
|
|
if os.name != 'nt':
|
|
pytest.skip('Windows ADODB.Stream sharing boundary')
|
|
import win32con
|
|
import win32file
|
|
source = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs'
|
|
writer = re.search(r'(?ms)^Sub QPWriteUtf8\(.*?^End Sub', source.read_text(encoding='utf-8')).group()
|
|
script = tmp_path / 'write-result.vbs'
|
|
script.write_text('Set QP_Fso = CreateObject("Scripting.FileSystemObject")\n'
|
|
'WScript.Echo "ready"\nOn Error Resume Next\nQPWriteUtf8 WScript.Arguments(0), "verified"\n'
|
|
'If Err.Number <> 0 Then\nWScript.StdErr.WriteLine Err.Description\nWScript.Quit 1\nEnd If\n'
|
|
'WScript.Quit 0\n' + writer,
|
|
encoding='ascii')
|
|
path = tmp_path / 'project-session-result.tsv'
|
|
path.write_text('before', encoding='utf-8')
|
|
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ, win32con.FILE_SHARE_READ,
|
|
None, win32con.OPEN_EXISTING, 0, None)
|
|
timer = None
|
|
process = subprocess.Popen((r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(script), str(path)),
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
|
|
try:
|
|
assert process.stdout.readline().strip() == b'ready'
|
|
if release_reader:
|
|
timer = Timer(.15, reader.Close)
|
|
timer.start()
|
|
_, stderr = process.communicate(timeout=5)
|
|
if release_reader:
|
|
assert process.returncode == 0, stderr
|
|
assert path.read_text(encoding='utf-8-sig') == 'verified'
|
|
else:
|
|
assert process.returncode != 0
|
|
assert path.read_text(encoding='utf-8') == 'before'
|
|
finally:
|
|
if timer:
|
|
timer.join()
|
|
reader.Close()
|
|
if process.poll() is None:
|
|
process.kill()
|
|
process.wait()
|