Files
dxdesigner-part-replacement/tests/dsh/test_lifecycle_wait.py
T

70 lines
3.6 KiB
Python

"""The real lifecycle tools retain their result wait across long native runs."""
from pathlib import Path
import shutil
import subprocess
import pytest
@pytest.mark.parametrize('tool,operation,cancellation', [
('dx_task_execute', 'start', ''),
('dx_task_execute', 'resume', ''),
('dx_task_operations', 'resume', ''),
('dx_task_operations', 'run_schedule', ''),
('dx_task_operations', 'history', 'timeout'),
('dx_task_execute', 'resume', 'abort'),
])
def test_lifecycle_wait_preserves_long_execution_and_explicit_cancellation(tool, operation, cancellation):
node = shutil.which('node')
if not node:
pytest.skip('Node is required for the actual Harness lifecycle tools')
script = r'''
import assert from 'node:assert/strict';
import {registerLifecycle} from './integration/dsh/task-plugin/lifecycle.mjs';
const tools = new Map(), events = new Map(), writes = [], timers = new Map();
const tick = () => new Promise(resolve => setImmediate(resolve));
registerLifecycle({on: (name, handler) => events.set(name, handler), effect: () => {},
tools: {register: tool => tools.set(tool.name, tool)}});
process.env.DX_DSH_PARENT_PIPE = '1';
const original = {setTimeout, clearTimeout, write: process.stdout.write};
globalThis.setTimeout = (callback, ms) => {const id = Symbol(); timers.set(id, {callback, ms}); return id;};
globalThis.clearTimeout = id => timers.delete(id);
process.stdout.write = line => {writes.push(line); return true;};
try {
const controller = new AbortController();
let settled = false;
const waiting = tools.get(process.argv[1]).execute({operation: process.argv[2]},
{callId: 'native-long-run', agent: {id: 'same-agent'}, signal: controller.signal})
.then(value => {settled = true; return value;}, error => {settled = true; return error;});
const request = JSON.parse(writes.find(line => line.startsWith('DX_DSH_LIFECYCLE_REQUEST ')).split(' ').slice(1).join(' '));
if (process.argv[3] === 'abort') controller.abort();
else for (const timer of timers.values()) if (timer.ms <= 371375) timer.callback();
await tick();
if (process.argv[3]) {
assert.equal(settled, true, 'Read timeout and conversation cancellation must still stop waiting');
const error = await waiting;
assert.ok(error instanceof Error);
assert.match(error.message, /retain their own lifetime/);
const cancellations = writes.filter(line => line.startsWith('DX_DSH_LIFECYCLE_CANCEL '));
assert.equal(cancellations.length, 1);
assert.equal(JSON.parse(cancellations[0].slice('DX_DSH_LIFECYCLE_CANCEL '.length)).request_id, request.request_id);
} else {
assert.equal(settled, false, 'Native execution is still running after six minutes; do not cancel its result wait');
assert.equal(writes.filter(line => line.startsWith('DX_DSH_LIFECYCLE_CANCEL ')).length, 0);
const receipt = {request_id: request.request_id, status: 'completed', run_id: 'native-run',
tasks: [{task_id: 'actual-task', saved_items: 2, failed_items: 1, no_match_items: 3}]};
events.get('dx/task-lifecycle-result')(receipt);
assert.deepEqual(await waiting, receipt, 'Return the actual final counts');
}
assert.equal(timers.size, 0);
} finally {
globalThis.setTimeout = original.setTimeout;
globalThis.clearTimeout = original.clearTimeout;
process.stdout.write = original.write;
}
'''
result = subprocess.run([node, '--input-type=module', '-e', script, tool, operation, cancellation],
cwd=Path(__file__).resolve().parents[2],
capture_output=True, text=True, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr