|
| 1 | +import { deepStrictEqual, ok, strictEqual } from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import WorkerPool from '../index.mjs'; |
| 5 | +import createParallelWorker from '../parallel.mjs'; |
| 6 | + |
| 7 | +describe('createParallelWorker', () => { |
| 8 | + // Use relative path from WorkerPool's location (src/threading/) |
| 9 | + const workerPath = './chunk-worker.mjs'; |
| 10 | + |
| 11 | + it('should create a ParallelWorker with map method', () => { |
| 12 | + const pool = new WorkerPool(workerPath, 2); |
| 13 | + |
| 14 | + const worker = createParallelWorker('metadata', pool, { threads: 2 }); |
| 15 | + |
| 16 | + ok(worker); |
| 17 | + strictEqual(typeof worker.map, 'function'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should use main thread for single-threaded execution', async () => { |
| 21 | + const pool = new WorkerPool(workerPath, 1); |
| 22 | + |
| 23 | + const worker = createParallelWorker('ast-js', pool, { threads: 1 }); |
| 24 | + const items = []; |
| 25 | + const results = await worker.map(items, items, {}); |
| 26 | + |
| 27 | + ok(Array.isArray(results)); |
| 28 | + strictEqual(results.length, 0); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should use main thread for small item counts', async () => { |
| 32 | + const pool = new WorkerPool(workerPath, 4); |
| 33 | + |
| 34 | + const worker = createParallelWorker('ast-js', pool, { threads: 4 }); |
| 35 | + const items = []; |
| 36 | + const results = await worker.map(items, items, {}); |
| 37 | + |
| 38 | + ok(Array.isArray(results)); |
| 39 | + strictEqual(results.length, 0); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should chunk items for parallel processing', async () => { |
| 43 | + const pool = new WorkerPool(workerPath, 2); |
| 44 | + |
| 45 | + const worker = createParallelWorker('ast-js', pool, { threads: 2 }); |
| 46 | + const items = []; |
| 47 | + |
| 48 | + const results = await worker.map(items, items, {}); |
| 49 | + |
| 50 | + strictEqual(results.length, 0); |
| 51 | + ok(Array.isArray(results)); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should pass extra options to worker', async () => { |
| 55 | + const pool = new WorkerPool(workerPath, 1); |
| 56 | + |
| 57 | + const worker = createParallelWorker('ast-js', pool, { threads: 1 }); |
| 58 | + const extra = { gitRef: 'main', customOption: 'value' }; |
| 59 | + const items = []; |
| 60 | + |
| 61 | + const results = await worker.map(items, items, extra); |
| 62 | + |
| 63 | + ok(Array.isArray(results)); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should serialize and deserialize data correctly', async () => { |
| 67 | + const pool = new WorkerPool(workerPath, 2); |
| 68 | + |
| 69 | + const worker = createParallelWorker('ast-js', pool, { threads: 2 }); |
| 70 | + const items = []; |
| 71 | + |
| 72 | + const results = await worker.map(items, items, {}); |
| 73 | + |
| 74 | + ok(Array.isArray(results)); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle empty items array', async () => { |
| 78 | + const pool = new WorkerPool(workerPath, 2); |
| 79 | + |
| 80 | + const worker = createParallelWorker('ast-js', pool, { threads: 2 }); |
| 81 | + const results = await worker.map([], [], {}); |
| 82 | + |
| 83 | + deepStrictEqual(results, []); |
| 84 | + }); |
| 85 | +}); |
0 commit comments