40 lines
1.9 KiB
TypeScript
40 lines
1.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { NodeImportWorkerExecutor } from '../../server/import-worker-client';
|
|
|
|
const request = {
|
|
source: { metadata: { name: 'x.csv', sizeBytes: 1, lastModified: 0 }, bytes: new Uint8Array([65]) }
|
|
};
|
|
|
|
function workerUrl(source: string): URL {
|
|
return new URL(`data:text/javascript,${encodeURIComponent(source)}`);
|
|
}
|
|
|
|
describe('Node import worker executor', () => {
|
|
it('returns isolated worker results', async () => {
|
|
const executor = new NodeImportWorkerExecutor({
|
|
workerUrl: workerUrl(`import { parentPort } from 'node:worker_threads'; parentPort.postMessage({ ok: true, dataset: { fileName: 'x.csv', fileSize: 1, lastModified: 0, format: 'CSV', rawText: 'A', sheets: [], profileId: 'generic', scanTool: 'Generic' } });`),
|
|
timeoutMs: 2_000
|
|
});
|
|
await expect(executor.execute(request)).resolves.toMatchObject({ fileName: 'x.csv', profileId: 'generic' });
|
|
await executor.close();
|
|
});
|
|
|
|
it('terminates parsers at the configured time limit', async () => {
|
|
const executor = new NodeImportWorkerExecutor({
|
|
workerUrl: workerUrl(`setInterval(() => {}, 1000);`), timeoutMs: 50, maximumConcurrent: 1
|
|
});
|
|
await expect(executor.execute(request)).rejects.toMatchObject({ diagnostics: [{ code: 'parser-time-limit' }] });
|
|
await executor.close();
|
|
});
|
|
|
|
it('rejects excess work instead of growing an unbounded queue', async () => {
|
|
const executor = new NodeImportWorkerExecutor({
|
|
workerUrl: workerUrl(`setInterval(() => {}, 1000);`), timeoutMs: 50, maximumConcurrent: 1, maximumQueued: 0
|
|
});
|
|
const active = executor.execute(request);
|
|
await expect(executor.execute(request)).rejects.toMatchObject({ diagnostics: [{ code: 'parser-capacity-limit' }] });
|
|
await expect(active).rejects.toMatchObject({ diagnostics: [{ code: 'parser-time-limit' }] });
|
|
await executor.close();
|
|
});
|
|
});
|