forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-test.ts
235 lines (195 loc) · 6.61 KB
/
simple-test.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import Piscina from '..';
import { test } from 'tap';
import { version } from '../package.json';
import { pathToFileURL } from 'url';
import { resolve } from 'path';
import { EventEmitter } from 'events';
test('Piscina is exposed on export', async ({ equal }) => {
equal(Piscina.version, version);
});
test('Piscina is exposed on itself', async ({ equal }) => {
equal(Piscina.Piscina, Piscina);
});
test('Piscina.isWorkerThread has the correct value', async ({ equal }) => {
equal(Piscina.isWorkerThread, false);
});
test('Piscina.isWorkerThread has the correct value (worker)', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-isworkerthread.ts')
});
const result = await worker.run(null);
equal(result, 'done');
});
test('Piscina.isWorkerThread has the correct value (worker) with named import', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-isworkerthread-named-import.ts')
});
const result = await worker.run(null);
equal(result, 'done');
});
test('Piscina.isWorkerThread has the correct value (worker) with named import', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-isworkerthread-named-import.ts')
});
const result = await worker.run(null);
equal(result, 'done');
});
test('Piscina instance is an EventEmitter', async ({ ok }) => {
const piscina = new Piscina();
ok(piscina instanceof EventEmitter);
});
test('Piscina constructor options are correctly set', async ({ equal }) => {
const piscina = new Piscina({
minThreads: 10,
maxThreads: 20,
maxQueue: 30
});
equal(piscina.options.minThreads, 10);
equal(piscina.options.maxThreads, 20);
equal(piscina.options.maxQueue, 30);
});
test('trivial eval() handler works', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
const result = await worker.run('42');
equal(result, 42);
});
test('async eval() handler works', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
const result = await worker.run('Promise.resolve(42)');
equal(result, 42);
});
test('filename can be provided while posting', async ({ equal }) => {
const worker = new Piscina();
const result = await worker.run(
'Promise.resolve(42)',
{ filename: resolve(__dirname, 'fixtures/eval.js') });
equal(result, 42);
});
test('filename can be null when initially provided', async ({ equal }) => {
const worker = new Piscina({ filename: null });
const result = await worker.run(
'Promise.resolve(42)',
{ filename: resolve(__dirname, 'fixtures/eval.js') });
equal(result, 42);
});
test('filename must be provided while posting', async ({ rejects }) => {
const worker = new Piscina();
rejects(worker.run('doesn’t matter'),
/filename must be provided to run\(\) or in options object/);
});
test('passing env to workers works', async ({ same }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
env: { A: 'foo' }
});
const env = await pool.run('({...process.env})');
same(env, { A: 'foo' });
});
test('passing argv to workers works', async ({ same }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
argv: ['a', 'b', 'c']
});
const env = await pool.run('process.argv.slice(2)');
same(env, ['a', 'b', 'c']);
});
test('passing execArgv to workers works', async ({ same }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
execArgv: ['--no-warnings']
});
const env = await pool.run('process.execArgv');
same(env, ['--no-warnings']);
});
test('passing valid workerData works', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-workerdata.ts'),
workerData: 'ABC'
});
equal(Piscina.workerData, undefined);
await pool.run(null);
});
test('passing valid workerData works with named import', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-workerdata-named-import.ts'),
workerData: 'ABC'
});
equal(Piscina.workerData, undefined);
await pool.run(null);
});
test('passing valid workerData works with named import', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/simple-workerdata-named-import.ts'),
workerData: 'ABC'
});
equal(Piscina.workerData, undefined);
await pool.run(null);
});
test('passing invalid workerData does not work', async ({ throws }) => {
throws(() => new Piscina(({
filename: resolve(__dirname, 'fixtures/simple-workerdata.ts'),
workerData: {
hello () {}
}
}) as any), /could not be cloned./);
});
test('filename can be a file:// URL', async ({ equal }) => {
const worker = new Piscina({
filename: pathToFileURL(resolve(__dirname, 'fixtures/eval.js')).href
});
const result = await worker.run('42');
equal(result, 42);
});
test('filename can be a file:// URL to an ESM module', {}, async ({ equal }) => {
const worker = new Piscina({
filename: pathToFileURL(resolve(__dirname, 'fixtures/esm-export.mjs')).href
});
const result = await worker.run('42');
equal(result, 42);
});
test('duration and utilization calculations work', async ({ equal, ok }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
// Initial utilization is always 0
equal(worker.utilization, 0);
await Promise.all([
worker.run('42'),
worker.run('41'),
worker.run('40')
]);
// utilization is going to be some non-deterministic value
// between 0 and 1. It should not be zero at this point
// because tasks have run, but it should also never be 1
ok(worker.utilization > 0);
ok(worker.utilization < 1);
// Duration must be non-zero.
ok(worker.duration > 0);
});
test('run works also', async () => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
await worker.run(42);
});
test('named tasks work', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/multiple.js')
});
equal(await worker.run({}, { name: 'a' }), 'a');
equal(await worker.run({}, { name: 'b' }), 'b');
equal(await worker.run({}), 'a');
});
test('named tasks work', async ({ equal }) => {
const worker = new Piscina({
filename: resolve(__dirname, 'fixtures/multiple.js'),
name: 'b'
});
equal(await worker.run({}, { name: 'a' }), 'a');
equal(await worker.run({}, { name: 'b' }), 'b');
equal(await worker.run({}), 'b');
});