-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathparallel.test.mjs
More file actions
182 lines (147 loc) · 4.29 KB
/
parallel.test.mjs
File metadata and controls
182 lines (147 loc) · 4.29 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
import { describe, it } from 'node:test';
import createWorkerPool from '../index.mjs';
import createParallelWorker from '../parallel.mjs';
/**
* Helper to collect all results from an async generator.
*
* @template T
* @param {AsyncGenerator<T[], void, unknown>} generator
* @returns {Promise<T[]>}
*/
async function collectStream(generator) {
const results = [];
for await (const chunk of generator) {
results.push(...chunk);
}
return results;
}
/**
* Helper to collect chunks (not flattened)
*
* @template T
* @param {AsyncGenerator<T[], void, unknown>} generator
* @returns {Promise<T[][]>}
*/
async function collectChunks(generator) {
const chunks = [];
for await (const chunk of generator) {
chunks.push(chunk);
}
return chunks;
}
describe('createParallelWorker', () => {
it('should create a ParallelWorker with stream method', async () => {
const pool = createWorkerPool(2);
const worker = await createParallelWorker('metadata', pool, { threads: 2 });
ok(worker);
strictEqual(typeof worker.stream, 'function');
await pool.destroy();
});
it('should handle empty items array', async () => {
const pool = createWorkerPool(2);
const worker = await createParallelWorker('ast-js', pool, {
threads: 2,
chunkSize: 10,
});
const results = await collectStream(worker.stream([], [], {}));
deepStrictEqual(results, []);
await pool.destroy();
});
it('should distribute items to multiple worker threads', async () => {
const pool = createWorkerPool(4);
const worker = await createParallelWorker('metadata', pool, {
threads: 4,
chunkSize: 1,
});
const mockInput = [
{
file: { stem: 'test1', basename: 'test1.md' },
tree: { type: 'root', children: [] },
},
{
file: { stem: 'test2', basename: 'test2.md' },
tree: { type: 'root', children: [] },
},
{
file: { stem: 'test3', basename: 'test3.md' },
tree: { type: 'root', children: [] },
},
{
file: { stem: 'test4', basename: 'test4.md' },
tree: { type: 'root', children: [] },
},
];
const chunks = await collectChunks(
worker.stream(mockInput, mockInput, { typeMap: {} })
);
strictEqual(chunks.length, 4);
for (const chunk of chunks) {
ok(Array.isArray(chunk));
}
await pool.destroy();
});
it('should yield results as chunks complete', async () => {
const pool = createWorkerPool(2);
const worker = await createParallelWorker('metadata', pool, {
threads: 2,
chunkSize: 1,
});
const mockInput = [
{
file: { stem: 'test1', basename: 'test1.md' },
tree: { type: 'root', children: [] },
},
{
file: { stem: 'test2', basename: 'test2.md' },
tree: { type: 'root', children: [] },
},
];
const chunks = await collectChunks(
worker.stream(mockInput, mockInput, { typeMap: {} })
);
strictEqual(chunks.length, 2);
await pool.destroy();
});
it('should work with single thread and items', async () => {
const pool = createWorkerPool(2);
const worker = await createParallelWorker('metadata', pool, {
threads: 2,
chunkSize: 5,
});
const mockInput = [
{
file: { stem: 'test1', basename: 'test1.md' },
tree: { type: 'root', children: [] },
},
];
const chunks = await collectChunks(
worker.stream(mockInput, mockInput, { typeMap: {} })
);
strictEqual(chunks.length, 1);
ok(Array.isArray(chunks[0]));
await pool.destroy();
});
it('should use sliceInput for metadata generator', async () => {
const pool = createWorkerPool(2);
const worker = await createParallelWorker('metadata', pool, {
threads: 2,
chunkSize: 1,
});
const mockInput = [
{
file: { stem: 'test1', basename: 'test1.md' },
tree: { type: 'root', children: [] },
},
{
file: { stem: 'test2', basename: 'test2.md' },
tree: { type: 'root', children: [] },
},
];
const chunks = await collectChunks(
worker.stream(mockInput, mockInput, { typeMap: {} })
);
strictEqual(chunks.length, 2);
await pool.destroy();
});
});