chore(electron): route the worker heap probe

This commit is contained in:
4gray committed 2026-07-27 02:15:58 +02:00
1 parent d1e268f1dd
commit 2455165043
2 files changed
+60

No files matched your search

@@ -96,6 +96,10 @@ import {
startWorkerPerformanceCapture,
type WorkerPerformanceCapture,
} from './worker-performance-capture';
import {
handleDatabaseWorkerPostGcHeapRequest,
isDatabaseWorkerPostGcHeapRequest,
} from './database-worker-post-gc-heap';
const loggerLabel = '[DB Worker]';
const batchDelayMs = Number.parseInt(
@@ -989,6 +993,14 @@ async function executeRequest(
}
parentPort.on('message', async (message: DbWorkerIncomingMessage) => {
if (isDatabaseWorkerPostGcHeapRequest(message)) {
handleDatabaseWorkerPostGcHeapRequest(
message,
activePerformanceCaptures.size === 0
);
return;
}
if (message.type === 'cancel') {
const activeOperation = activeOperations.get(message.operationId);
if (activeOperation) {
@@ -311,3 +311,51 @@ describe('worker cancellation while performance capture arms', () => {
);
});
});
describe('database worker performance control messages', () => {
afterEach(() => {
jest.restoreAllMocks();
jest.resetModules();
});
it('handles a post-GC probe without dispatching a database request', async () => {
const armGate = createArmGate();
armGate.release();
const port = createParentPortHarness<unknown, DbWorkerMessage>();
const getWorkerDatabase = jest.fn().mockResolvedValue({});
const handlePostGcHeapRequest = jest.fn();
const responsePort = { marker: 'response-port' };
mockPerformanceCapture(armGate);
jest.doMock('worker_threads', () => ({
parentPort: port.parentPort,
}));
jest.doMock('./database.worker-connection', () => ({
closeWorkerDatabase: jest.fn(),
getWorkerDatabase,
}));
jest.doMock('./database-worker-post-gc-heap', () => ({
handleDatabaseWorkerPostGcHeapRequest: handlePostGcHeapRequest,
isDatabaseWorkerPostGcHeapRequest: jest.fn(
(message: unknown) =>
typeof message === 'object' &&
message !== null &&
(message as Record<string, unknown>)['type'] ===
'performance:collect-post-gc-heap'
),
}));
await import('./database.worker');
port.postMessage.mockClear();
const handleMessage = port.getMessageHandler();
const message = {
type: 'performance:collect-post-gc-heap',
responsePort,
};
await handleMessage(message);
expect(handlePostGcHeapRequest).toHaveBeenCalledWith(message, true);
expect(getWorkerDatabase).not.toHaveBeenCalled();
expect(port.postMessage).not.toHaveBeenCalled();
});
});