80 lines
3.5 KiB
Python
80 lines
3.5 KiB
Python
"""Native startup-cancel and watchdog cleanup probes; never opens a project."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
from threading import Event, Timer
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src'))
|
|
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
|
|
from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError, run_monitored_process
|
|
from private_desktop_observer import PrivateDesktopObserver
|
|
|
|
|
|
def main():
|
|
import win32api, win32event
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
output = args.output.resolve()
|
|
output.mkdir(parents=True, exist_ok=False)
|
|
report = {}
|
|
with PrivateDesktopObserver(output):
|
|
cancel_root = output / 'data/tasks/startup-cancel/quick-run'
|
|
abort = Event()
|
|
timer = Timer(0.8, abort.set)
|
|
timer.start()
|
|
try:
|
|
with IsolatedCadSession(cancel_root, abort_event=abort):
|
|
raise AssertionError('cancelled startup entered execution')
|
|
except WorkerMonitorError as error:
|
|
assert 'cancelled' in str(error)
|
|
report['startup_cancelled'] = True
|
|
finally:
|
|
timer.cancel()
|
|
timer.join()
|
|
timeout_root = output / 'data/tasks/timeout/quick-run'
|
|
timeout_root.mkdir(parents=True)
|
|
script = timeout_root / 'stalled-worker.py'
|
|
script.write_text('''import sys,time,pathlib,subprocess,json,os
|
|
root=pathlib.Path(sys.argv[1]);token=sys.argv[-1]
|
|
while not (root/f'monitor-{token}.ready').exists():time.sleep(.01)
|
|
child=subprocess.Popen([sys.executable,'-c','import time;time.sleep(60)'])
|
|
(root/'child.json').write_text(json.dumps({'parent':os.getpid(),'child':child.pid}))
|
|
time.sleep(60)
|
|
''', encoding='utf-8')
|
|
try:
|
|
with IsolatedCadSession(timeout_root) as session:
|
|
command = (sys.executable, '-u', str(script), str(timeout_root), sys.executable, '', 'timeout')
|
|
run_monitored_process(command, 1.5, process_factory=session.start_worker)
|
|
except subprocess.TimeoutExpired:
|
|
report['watchdog_timeout'] = True
|
|
assert report.get('watchdog_timeout') and (timeout_root / 'uncertain.flag').exists()
|
|
report['worker_exit_confirmed'] = {}
|
|
for key, pid in json.loads((timeout_root / 'child.json').read_text()).items():
|
|
try:
|
|
handle = win32api.OpenProcess(0x100000, False, pid)
|
|
except Exception as error:
|
|
assert error.args[0] == 87
|
|
exited = True
|
|
else:
|
|
try:
|
|
exited = win32event.WaitForSingleObject(handle, 5000) == 0
|
|
finally:
|
|
handle.Close()
|
|
assert exited
|
|
report['worker_exit_confirmed'][key] = exited
|
|
observation = json.loads((output / 'desktop-observation.json').read_text(encoding='utf-8'))
|
|
assert not observation['owned_cad_became_foreground'] and not observation['owned_cad_on_input_desktop']
|
|
assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values())
|
|
assert observation['input_desktop_before'] == observation['input_desktop_after']
|
|
assert not observation['observer_errors']
|
|
report['passed'] = True
|
|
(output / 'report.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
|
|
print(json.dumps(report), flush=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|