Bound extension job and window-alias state
Two service-worker leaks. Running jobs were only bounded by their watchdog, so a command flood could hold many timers and results for up to JOB_TIMEOUT_MS; cap concurrent jobs at 32 and reject beyond that, since a clear error beats an unresponsive worker. Window aliases were never removed, so storage kept an entry for every window the user had ever renamed. Prune aliases against the live window set on list, rename, and close rather than only on close, because windows also disappear without going through windows.close. Also extract the repeated job summary in perf.status into a helper.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// @ts-nocheck
|
||||
import { test, mock } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { JobManager, JOB_TIMEOUT_MS, MAX_FINISHED_JOBS, pruneFinishedJobs } from '../src/classes/JobManager';
|
||||
import { JobManager, JOB_TIMEOUT_MS, MAX_FINISHED_JOBS, MAX_RUNNING_JOBS, pruneFinishedJobs } from '../src/classes/JobManager';
|
||||
import { makeChromeMock } from './chrome-mock';
|
||||
|
||||
// Drain pending microtasks (finalize() chains several awaits). setImmediate is
|
||||
@@ -129,6 +129,21 @@ test('JobManager: a runner that settles after the watchdog cannot resurrect the
|
||||
mock.timers.reset();
|
||||
});
|
||||
|
||||
test('JobManager: rejects new background jobs above the running-job cap', async () => {
|
||||
mock.timers.enable({ apis: ['setInterval', 'setTimeout'] });
|
||||
globalThis.chrome = makeChromeMock();
|
||||
const mgr = new JobManager();
|
||||
for (let i = 0; i < MAX_RUNNING_JOBS; i++) {
|
||||
await mgr.start(`running${i}`, {}, () => new Promise(() => {}));
|
||||
}
|
||||
await assert.rejects(
|
||||
() => mgr.start('overflow', {}, async () => 'nope'),
|
||||
/too many background jobs running/,
|
||||
);
|
||||
assert.equal(mgr.list().filter(job => job.status === 'running').length, MAX_RUNNING_JOBS);
|
||||
mock.timers.reset();
|
||||
});
|
||||
|
||||
test('JobManager: persisted set keeps running jobs even past the finished cap', async () => {
|
||||
mock.timers.enable({ apis: ['setInterval', 'setTimeout'] });
|
||||
globalThis.chrome = makeChromeMock();
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// @ts-nocheck
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { WindowsCommands } from '../src/commands/windows';
|
||||
import { makeChromeMock } from './chrome-mock';
|
||||
|
||||
function makeWindowsChromeMock(windows) {
|
||||
const chrome = makeChromeMock();
|
||||
chrome.windows = {
|
||||
getAll: async () => windows,
|
||||
remove: async () => {},
|
||||
create: async () => ({ id: 99 }),
|
||||
};
|
||||
return chrome;
|
||||
}
|
||||
|
||||
test('windows.list prunes aliases for closed windows', async () => {
|
||||
globalThis.chrome = makeWindowsChromeMock([
|
||||
{ id: 1, focused: true, state: 'normal', tabs: [{ id: 10 }] },
|
||||
{ id: 2, focused: false, state: 'minimized', tabs: [] },
|
||||
]);
|
||||
globalThis.chrome.storage.local._store.windowAliases = {
|
||||
1: 'main',
|
||||
2: 'side',
|
||||
999: 'closed',
|
||||
};
|
||||
|
||||
const commands = new WindowsCommands({ jobs: {} });
|
||||
const result = await commands.commands['windows.list']({});
|
||||
|
||||
assert.deepEqual(result.map(w => [w.id, w.alias]), [[1, 'main'], [2, 'side']]);
|
||||
assert.deepEqual(globalThis.chrome.storage.local._store.windowAliases, { 1: 'main', 2: 'side' });
|
||||
});
|
||||
|
||||
test('windows.rename prunes stale aliases before saving the new name', async () => {
|
||||
globalThis.chrome = makeWindowsChromeMock([
|
||||
{ id: 1, focused: true, state: 'normal', tabs: [] },
|
||||
{ id: 2, focused: false, state: 'normal', tabs: [] },
|
||||
]);
|
||||
globalThis.chrome.storage.local._store.windowAliases = {
|
||||
1: 'main',
|
||||
999: 'closed',
|
||||
};
|
||||
|
||||
const commands = new WindowsCommands({ jobs: {} });
|
||||
await commands.commands['windows.rename']({ windowId: 2, name: 'work' });
|
||||
|
||||
assert.deepEqual(globalThis.chrome.storage.local._store.windowAliases, { 1: 'main', 2: 'work' });
|
||||
});
|
||||
|
||||
test('windows.close removes the closed window alias immediately', async () => {
|
||||
let removed = null;
|
||||
globalThis.chrome = makeWindowsChromeMock([
|
||||
{ id: 1, focused: true, state: 'normal', tabs: [] },
|
||||
{ id: 2, focused: false, state: 'normal', tabs: [] },
|
||||
]);
|
||||
globalThis.chrome.windows.remove = async id => { removed = id; };
|
||||
globalThis.chrome.storage.local._store.windowAliases = {
|
||||
1: 'main',
|
||||
2: 'side',
|
||||
999: 'closed',
|
||||
};
|
||||
|
||||
const commands = new WindowsCommands({ jobs: {} });
|
||||
await commands.commands['windows.close']({ windowId: 2 });
|
||||
|
||||
assert.equal(removed, 2);
|
||||
assert.deepEqual(globalThis.chrome.storage.local._store.windowAliases, { 1: 'main' });
|
||||
});
|
||||
Reference in New Issue
Block a user