File size: 4,930 Bytes
a4c784d | 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 164 | 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 keeps the retained job after transport interruption', async () => {
await withBrowserEnv({}, async ({ importModule, window }) => {
const { createSolverController } = await importModule('static/app/shell/solver-controller.mjs');
const calls = [];
let onStreamError;
let createCount = 0;
let planProviderCalls = 0;
const controller = createSolverController({
sf: window.SF,
backend: {
createJob: async () => {
createCount += 1;
return 'job-retained';
},
streamJobEvents(id, onMessage, onError) {
calls.push(['streamJobEvents', id]);
onStreamError = onError;
onMessage({
eventType: 'progress',
jobId: id,
lifecycleState: 'SOLVING',
currentScore: '0hard/-1soft',
bestScore: '0hard/-1soft',
});
return () => calls.push(['closeStream']);
},
getSnapshot: async () => null,
analyzeSnapshot: async () => null,
pauseJob: async () => {},
resumeJob: async () => {},
cancelJob: async (id) => calls.push(['cancelJob', id]),
deleteJob: async () => {},
},
statusBar: null,
onPlan: () => {},
onAnalysis: () => {},
onMeta: () => {},
onLifecycle: () => {},
onError: () => {},
});
await controller.start(async () => ({}));
onStreamError(new Error('temporary disconnect'));
await flush();
assert.equal(controller.getJobId(), 'job-retained');
assert.equal(controller.getLifecycleState(), 'SOLVING');
await controller.start(async () => {
planProviderCalls += 1;
return {};
});
void controller.cancel();
await flush();
assert.equal(createCount, 1);
assert.equal(planProviderCalls, 0);
assert.deepEqual(calls, [
['streamJobEvents', 'job-retained'],
['closeStream'],
['streamJobEvents', 'job-retained'],
['cancelJob', 'job-retained'],
]);
});
});
test('solver controller pauses and resumes the retained runtime without starting over', 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;
let latestPlan = null;
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: 'PAUSED',
currentScore: '0hard/-4soft',
bestScore: '0hard/-4soft',
solution: { id, revision, source: 'paused-snapshot' },
}),
analyzeSnapshot: async () => null,
pauseJob: async (id) => calls.push(['pauseJob', id]),
resumeJob: async (id) => calls.push(['resumeJob', id]),
cancelJob: async () => {},
deleteJob: async () => {},
},
statusBar: null,
onPlan: (plan) => {
latestPlan = plan;
},
onAnalysis: () => {},
onMeta: () => {},
onLifecycle: () => {},
onError: () => {},
});
await controller.start(async () => ({ source: 'initial-plan' }));
const pause = controller.pause();
await flush();
onMessage({
eventType: 'paused',
jobId: 'job-1',
lifecycleState: 'PAUSED',
snapshotRevision: 7,
currentScore: '0hard/-4soft',
bestScore: '0hard/-4soft',
});
await pause;
assert.equal(controller.getLifecycleState(), 'PAUSED');
assert.deepEqual(latestPlan, { id: 'job-1', revision: 7, source: 'paused-snapshot' });
await controller.start(async () => {
planProviderCalls += 1;
return {};
});
const resume = controller.resume();
await flush();
onMessage({
eventType: 'resumed',
jobId: 'job-1',
lifecycleState: 'SOLVING',
snapshotRevision: 7,
});
await resume;
assert.equal(createCount, 1);
assert.equal(planProviderCalls, 0);
assert.equal(controller.getJobId(), 'job-1');
assert.equal(controller.getLifecycleState(), 'SOLVING');
assert.deepEqual(calls, [
['streamJobEvents', 'job-1'],
['pauseJob', 'job-1'],
['resumeJob', 'job-1'],
]);
});
});
|