File size: 4,868 Bytes
7596726 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | const assert = require('node:assert/strict');
const test = require('node:test');
const { withBrowserEnv } = require('./support/load-browser-modules');
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
test('solver controller stop leaves only a terminal job for the next solve to replace', async () => {
await withBrowserEnv({}, async ({ importModule, window }) => {
const { createSolverController } = await importModule('static/app/shell/solver-controller.mjs');
const calls = [];
let onMessage;
let createCount = 0;
let latestPlan = null;
let restartProviderCalls = 0;
const controller = createSolverController({
sf: window.SF,
backend: {
createJob: async () => {
createCount += 1;
return `job-${createCount}`;
},
streamJobEvents(id, callback) {
calls.push(['streamJobEvents', id]);
onMessage = callback;
return () => calls.push(['closeStream', id]);
},
getSnapshot: async (id, revision) => ({
id,
jobId: id,
snapshotRevision: revision,
lifecycleState: 'CANCELLED',
currentScore: '0hard/-2soft',
bestScore: '0hard/-2soft',
solution: { id, revision, source: 'stopped-snapshot' },
}),
analyzeSnapshot: async () => null,
pauseJob: async () => {},
resumeJob: async (id) => calls.push(['resumeJob', id]),
cancelJob: async (id) => calls.push(['cancelJob', id]),
deleteJob: async (id) => calls.push(['deleteJob', id]),
},
statusBar: null,
onPlan: (plan) => {
latestPlan = plan;
},
onAnalysis: () => {},
onMeta: () => {},
onLifecycle: () => {},
onError: () => {},
});
await controller.start(async () => ({ source: 'initial-plan' }));
const stop = controller.cancel();
await flush();
onMessage({
eventType: 'cancelled',
jobId: 'job-1',
lifecycleState: 'CANCELLED',
snapshotRevision: 5,
currentScore: '0hard/-2soft',
bestScore: '0hard/-2soft',
});
await stop;
assert.equal(controller.getLifecycleState(), 'CANCELLED');
assert.deepEqual(latestPlan, { id: 'job-1', revision: 5, source: 'stopped-snapshot' });
await controller.start(async () => {
restartProviderCalls += 1;
return latestPlan;
});
assert.equal(createCount, 2);
assert.equal(restartProviderCalls, 1);
assert.equal(controller.getJobId(), 'job-2');
assert.deepEqual(calls, [
['streamJobEvents', 'job-1'],
['cancelJob', 'job-1'],
['closeStream', 'job-1'],
['deleteJob', 'job-1'],
['streamJobEvents', 'job-2'],
]);
});
});
test('solver controller aborts a retry when terminal cleanup fails', async () => {
await withBrowserEnv({}, async ({ importModule, window }) => {
const { createSolverController } = await importModule('static/app/shell/solver-controller.mjs');
const calls = [];
let onMessage;
let createCount = 0;
let planProviderCalls = 0;
const controller = createSolverController({
sf: window.SF,
backend: {
createJob: async () => {
createCount += 1;
return `job-${createCount}`;
},
streamJobEvents(id, callback) {
calls.push(['streamJobEvents', id]);
onMessage = callback;
return () => calls.push(['closeStream', id]);
},
getSnapshot: async (id, revision) => ({
id,
jobId: id,
snapshotRevision: revision,
lifecycleState: 'COMPLETED',
currentScore: '0hard/0soft',
bestScore: '0hard/0soft',
solution: { id, revision },
}),
analyzeSnapshot: async () => null,
pauseJob: async () => {},
resumeJob: async () => {},
cancelJob: async () => {},
deleteJob: async (id) => {
calls.push(['deleteJob', id]);
throw new Error('delete failed');
},
},
statusBar: null,
onPlan: () => {},
onAnalysis: () => {},
onMeta: () => {},
onLifecycle: () => {},
onError: () => {},
});
await controller.start(async () => ({}));
onMessage({
eventType: 'completed',
jobId: 'job-1',
lifecycleState: 'COMPLETED',
snapshotRevision: 1,
currentScore: '0hard/0soft',
bestScore: '0hard/0soft',
});
await flush();
await assert.rejects(
() => controller.start(async () => {
planProviderCalls += 1;
return {};
}),
/delete failed/,
);
assert.equal(createCount, 1);
assert.equal(planProviderCalls, 0);
assert.deepEqual(calls, [
['streamJobEvents', 'job-1'],
['closeStream', 'job-1'],
['deleteJob', 'job-1'],
]);
});
});
|