Skip to content

Commit add01bf

Browse files
test: simplify (piscinajs#819)
1 parent 9553678 commit add01bf

38 files changed

+659
-665
lines changed

scripts/run-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ const args = [
4646
...testFiles
4747
];
4848

49+
4950
let result;
5051
if (isCoverage) {
5152
result = spawnSync('c8', ['--reporter=lcov', 'node', ...args], { stdio: 'inherit' });
5253
} else {
53-
result = spawnSync('c8', ['node', ...args], { stdio: 'inherit' });
54+
result = spawnSync('node', [...args], { stdio: 'inherit' });
5455
}
5556

5657
process.exit(result.status);

test/abort-task.test.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
import { EventEmitter } from 'events';
2-
import Piscina from '..';
31
import { test } from 'node:test';
4-
import type { TestContext } from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import { EventEmitter } from 'node:events';
4+
import Piscina from '..';
55
import { resolve } from 'path';
66

77
const TIMEOUT_MAX = 2 ** 31 - 1;
88

9-
test('tasks can be aborted through AbortController while running', async (t: TestContext) => {
9+
test('tasks can be aborted through AbortController while running', () => {
1010
const pool = new Piscina({
1111
filename: resolve(__dirname, 'fixtures/notify-then-sleep.ts')
1212
});
1313

1414
const buf = new Int32Array(new SharedArrayBuffer(4));
1515
const abortController = new AbortController();
16-
t.assert.rejects(pool.run(buf, { signal: abortController.signal }),
16+
assert.rejects(pool.run(buf, { signal: abortController.signal }),
1717
/The task has been aborted/);
1818

1919
Atomics.wait(buf, 0, 0);
20-
t.assert.strictEqual(Atomics.load(buf, 0), 1);
20+
assert.strictEqual(Atomics.load(buf, 0), 1);
2121

2222
abortController.abort();
2323
});
2424

25-
test('tasks can be aborted through EventEmitter while running', async (t: TestContext) => {
25+
test('tasks can be aborted through EventEmitter while running', () => {
2626
const pool = new Piscina({
2727
filename: resolve(__dirname, 'fixtures/notify-then-sleep.ts')
2828
});
2929

3030
const buf = new Int32Array(new SharedArrayBuffer(4));
3131
const ee = new EventEmitter();
32-
t.assert.rejects(pool.run(buf, { signal: ee }), /The task has been aborted/);
32+
assert.rejects(pool.run(buf, { signal: ee }), /The task has been aborted/);
3333

3434
Atomics.wait(buf, 0, 0);
35-
t.assert.strictEqual(Atomics.load(buf, 0), 1);
35+
assert.strictEqual(Atomics.load(buf, 0), 1);
3636

3737
ee.emit('abort');
3838
});
3939

40-
test('tasks can be aborted through EventEmitter before running', async (t: TestContext) => {
40+
test('tasks can be aborted through EventEmitter before running', async () => {
4141
const pool = new Piscina({
4242
filename: resolve(__dirname, 'fixtures/wait-for-notify.js'),
4343
maxThreads: 1
@@ -50,8 +50,8 @@ test('tasks can be aborted through EventEmitter before running', async (t: TestC
5050
const ee = new EventEmitter();
5151
const task1 = pool.run(bufs[0]);
5252
const abortable = pool.run(bufs[1], { signal: ee });
53-
t.assert.strictEqual(pool.queueSize, 0); // Means it's running
54-
t.assert.rejects(abortable, /The task has been aborted/);
53+
assert.strictEqual(pool.queueSize, 0); // Means it's running
54+
assert.rejects(abortable, /The task has been aborted/);
5555

5656
ee.emit('abort');
5757

@@ -61,7 +61,7 @@ test('tasks can be aborted through EventEmitter before running', async (t: TestC
6161
await task1;
6262
});
6363

64-
test('abortable tasks will not share workers (abortable posted second)', async (t: TestContext) => {
64+
test('abortable tasks will not share workers (abortable posted second)', async () => {
6565
const pool = new Piscina({
6666
filename: resolve(__dirname, 'fixtures/wait-for-notify.ts'),
6767
maxThreads: 1,
@@ -74,8 +74,8 @@ test('abortable tasks will not share workers (abortable posted second)', async (
7474
];
7575
const task1 = pool.run(bufs[0]);
7676
const ee = new EventEmitter();
77-
t.assert.rejects(pool.run(bufs[1], { signal: ee }), /The task has been aborted/);
78-
t.assert.strictEqual(pool.queueSize, 0);
77+
assert.rejects(pool.run(bufs[1], { signal: ee }), /The task has been aborted/);
78+
assert.strictEqual(pool.queueSize, 0);
7979

8080
ee.emit('abort');
8181

@@ -86,26 +86,26 @@ test('abortable tasks will not share workers (abortable posted second)', async (
8686
});
8787

8888
// TODO: move to testing balancer
89-
test('abortable tasks will not share workers (abortable posted first)', async (t: TestContext) => {
89+
test('abortable tasks will not share workers (abortable posted first)', async () => {
9090
const pool = new Piscina({
9191
filename: resolve(__dirname, 'fixtures/eval.js'),
9292
maxThreads: 1,
9393
concurrentTasksPerWorker: 2
9494
});
9595

9696
const ee = new EventEmitter();
97-
t.assert.rejects(pool.run('while(true);', { signal: ee }), /The task has been aborted/);
97+
assert.rejects(pool.run('while(true);', { signal: ee }), /The task has been aborted/);
9898
const task2 = pool.run('42');
99-
t.assert.strictEqual(pool.queueSize, 1);
99+
assert.strictEqual(pool.queueSize, 1);
100100

101101
ee.emit('abort');
102102

103103
// Wake up the thread handling the second task.
104-
t.assert.strictEqual(await task2, 42);
104+
assert.strictEqual(await task2, 42);
105105
});
106106

107107
// TODO: move to testing balancer
108-
test('abortable tasks will not share workers (on worker available)', async (t: TestContext) => {
108+
test('abortable tasks will not share workers (on worker available)', async () => {
109109
const pool = new Piscina({
110110
filename: resolve(__dirname, 'fixtures/sleep.js'),
111111
maxThreads: 1,
@@ -123,12 +123,12 @@ test('abortable tasks will not share workers (on worker available)', async (t: T
123123
pool.run({ time: 100, a: 3 }, { signal: new EventEmitter() })
124124
]);
125125

126-
t.assert.strictEqual(ret[0], 0);
127-
t.assert.strictEqual(ret[1], 1);
128-
t.assert.strictEqual(ret[2], 2);
126+
assert.strictEqual(ret[0], 0);
127+
assert.strictEqual(ret[1], 1);
128+
assert.strictEqual(ret[2], 2);
129129
});
130130

131-
test('abortable tasks will not share workers (destroy workers)', async (t: TestContext) => {
131+
test('abortable tasks will not share workers (destroy workers)', () => {
132132
const pool = new Piscina({
133133
filename: resolve(__dirname, 'fixtures/sleep.js'),
134134
maxThreads: 1,
@@ -145,30 +145,30 @@ test('abortable tasks will not share workers (destroy workers)', async (t: TestC
145145
pool.destroy();
146146
});
147147

148-
t.assert.rejects(pool.run({ time: TIMEOUT_MAX, a: 2 }), /Terminating worker thread/);
149-
t.assert.rejects(pool.run({ time: TIMEOUT_MAX, a: 3 }, { signal: new EventEmitter() }),
148+
assert.rejects(pool.run({ time: TIMEOUT_MAX, a: 2 }), /Terminating worker thread/);
149+
assert.rejects(pool.run({ time: TIMEOUT_MAX, a: 3 }, { signal: new EventEmitter() }),
150150
/Terminating worker thread/);
151151
});
152152

153-
test('aborted AbortSignal rejects task immediately', async (t: TestContext) => {
153+
test('aborted AbortSignal rejects task immediately', () => {
154154
const pool = new Piscina({
155155
filename: resolve(__dirname, 'fixtures/move.ts')
156156
});
157157

158158
const controller = new AbortController();
159159
// Abort the controller early
160160
controller.abort();
161-
t.assert.strictEqual(controller.signal.aborted, true);
161+
assert.strictEqual(controller.signal.aborted, true);
162162

163163
// The data won't be moved because the task will abort immediately.
164164
const data = new Uint8Array(new SharedArrayBuffer(4));
165-
t.assert.rejects(pool.run(data, { signal: controller.signal, transferList: [data.buffer] }),
165+
assert.rejects(pool.run(data, { signal: controller.signal, transferList: [data.buffer] }),
166166
/The task has been aborted/);
167167

168-
t.assert.strictEqual(data.length, 4);
168+
assert.strictEqual(data.length, 4);
169169
});
170170

171-
test('task with AbortSignal cleans up properly', async (t: TestContext) => {
171+
test('task with AbortSignal cleans up properly', async () => {
172172
const pool = new Piscina({
173173
filename: resolve(__dirname, 'fixtures/eval.js')
174174
});
@@ -179,39 +179,39 @@ test('task with AbortSignal cleans up properly', async (t: TestContext) => {
179179

180180
const { getEventListeners } = EventEmitter as any;
181181
if (typeof getEventListeners === 'function') {
182-
t.assert.strictEqual(getEventListeners(ee, 'abort').length, 0);
182+
assert.strictEqual(getEventListeners(ee, 'abort').length, 0);
183183
}
184184

185185
const controller = new AbortController();
186186

187187
await pool.run('1+1', { signal: controller.signal });
188188
});
189189

190-
test('aborted AbortSignal rejects task immediately (with reason)', async (t: TestContext) => {
190+
test('aborted AbortSignal rejects task immediately (with reason)', async () => {
191191
const pool = new Piscina({
192192
filename: resolve(__dirname, 'fixtures/move.ts')
193193
});
194194
const customReason = new Error('custom reason');
195195

196196
const controller = new AbortController();
197197
controller.abort(customReason);
198-
t.assert.strictEqual(controller.signal.aborted, true);
199-
t.assert.strictEqual(controller.signal.reason, customReason);
198+
assert.strictEqual(controller.signal.aborted, true);
199+
assert.strictEqual(controller.signal.reason, customReason);
200200

201201
// The data won't be moved because the task will abort immediately.
202202
const data = new Uint8Array(new SharedArrayBuffer(4));
203203

204204
try {
205205
await pool.run(data, { transferList: [data.buffer], signal: controller.signal });
206206
} catch (error) {
207-
t.assert.strictEqual(error.message, 'The task has been aborted');
208-
t.assert.strictEqual(error.cause, customReason);
207+
assert.strictEqual(error.message, 'The task has been aborted');
208+
assert.strictEqual(error.cause, customReason);
209209
}
210210

211-
t.assert.strictEqual(data.length, 4);
211+
assert.strictEqual(data.length, 4);
212212
});
213213

214-
test('tasks can be aborted through AbortController while running', async (t: TestContext) => {
214+
test('tasks can be aborted through AbortController while running', async () => {
215215
const pool = new Piscina({
216216
filename: resolve(__dirname, 'fixtures/notify-then-sleep.ts')
217217
});
@@ -224,42 +224,42 @@ test('tasks can be aborted through AbortController while running', async (t: Tes
224224
const promise = pool.run(buf, { signal: abortController.signal });
225225

226226
Atomics.wait(buf, 0, 0);
227-
t.assert.strictEqual(Atomics.load(buf, 0), 1);
227+
assert.strictEqual(Atomics.load(buf, 0), 1);
228228

229229
abortController.abort(reason);
230230

231231
await promise;
232232
} catch (error) {
233-
t.assert.strictEqual(error.message, 'The task has been aborted');
234-
t.assert.strictEqual(error.cause, reason);
233+
assert.strictEqual(error.message, 'The task has been aborted');
234+
assert.strictEqual(error.cause, reason);
235235
}
236236
});
237237

238-
test('aborted AbortSignal rejects task immediately (with reason)', async (t: TestContext) => {
238+
test('aborted AbortSignal rejects task immediately (with reason)', async () => {
239239
const pool = new Piscina({
240240
filename: resolve(__dirname, 'fixtures/move.ts')
241241
});
242242
const customReason = new Error('custom reason');
243243

244244
const controller = new AbortController();
245245
controller.abort(customReason);
246-
t.assert.strictEqual(controller.signal.aborted, true);
247-
t.assert.strictEqual(controller.signal.reason, customReason);
246+
assert.strictEqual(controller.signal.aborted, true);
247+
assert.strictEqual(controller.signal.reason, customReason);
248248

249249
// The data won't be moved because the task will abort immediately.
250250
const data = new Uint8Array(new SharedArrayBuffer(4));
251251

252252
try {
253253
await pool.run(data, { transferList: [data.buffer], signal: controller.signal });
254254
} catch (error) {
255-
t.assert.strictEqual(error.message, 'The task has been aborted');
256-
t.assert.strictEqual(error.cause, customReason);
255+
assert.strictEqual(error.message, 'The task has been aborted');
256+
assert.strictEqual(error.cause, customReason);
257257
}
258258

259-
t.assert.strictEqual(data.length, 4);
259+
assert.strictEqual(data.length, 4);
260260
});
261261

262-
test('tasks can be aborted through AbortController while running', async (t: TestContext) => {
262+
test('tasks can be aborted through AbortController while running', async () => {
263263
const pool = new Piscina({
264264
filename: resolve(__dirname, 'fixtures/notify-then-sleep.ts')
265265
});
@@ -272,13 +272,13 @@ test('tasks can be aborted through AbortController while running', async (t: Tes
272272
const promise = pool.run(buf, { signal: abortController.signal });
273273

274274
Atomics.wait(buf, 0, 0);
275-
t.assert.strictEqual(Atomics.load(buf, 0), 1);
275+
assert.strictEqual(Atomics.load(buf, 0), 1);
276276

277277
abortController.abort(reason);
278278

279279
await promise;
280280
} catch (error) {
281-
t.assert.strictEqual(error.message, 'The task has been aborted');
282-
t.assert.strictEqual(error.cause, reason);
281+
assert.strictEqual(error.message, 'The task has been aborted');
282+
assert.strictEqual(error.cause, reason);
283283
}
284284
});

test/async-context.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createHook, executionAsyncId } from 'async_hooks';
2-
import Piscina from '..';
1+
import assert from 'node:assert/strict';
32
import { test } from 'node:test';
4-
import type { TestContext } from 'node:test';
5-
import { resolve } from 'path';
3+
import { resolve } from 'node:path';
4+
import { createHook, executionAsyncId } from 'node:async_hooks';
5+
import Piscina from '..';
6+
67

7-
test('postTask() calls the correct async hooks', async (t: TestContext) => {
8+
test('postTask() calls the correct async hooks', async () => {
89
let taskId;
910
let initCalls = 0;
1011
let beforeCalls = 0;
@@ -37,8 +38,8 @@ test('postTask() calls the correct async hooks', async (t: TestContext) => {
3738
await pool.run('42');
3839

3940
hook.disable();
40-
t.assert.strictEqual(initCalls, 1);
41-
t.assert.strictEqual(beforeCalls, 1);
42-
t.assert.strictEqual(afterCalls, 1);
43-
t.assert.strictEqual(resolveCalls, 1);
41+
assert.strictEqual(initCalls, 1);
42+
assert.strictEqual(beforeCalls, 1);
43+
assert.strictEqual(afterCalls, 1);
44+
assert.strictEqual(resolveCalls, 1);
4445
});

test/atomics-optimization.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { resolve } from 'node:path';
2-
1+
import assert from 'node:assert/strict';
32
import { test } from 'node:test';
4-
import type { TestContext } from 'node:test';
5-
3+
import { resolve } from 'node:path';
64
import Piscina from '..';
75

8-
test('coverage test for Atomics optimization (sync mode)', async (t: TestContext) => {
6+
test('coverage test for Atomics optimization (sync mode)', async () => {
97
const pool = new Piscina({
108
filename: resolve(__dirname, 'fixtures/notify-then-sleep-or.js'),
119
minThreads: 2,
@@ -33,7 +31,7 @@ test('coverage test for Atomics optimization (sync mode)', async (t: TestContext
3331
// The check above could also be !== 2 but it's hard to get things right
3432
// sometimes and this gives us a nice assertion. Basically, at this point
3533
// exactly 2 tasks should be in Atomics.wait() state.
36-
t.assert.strictEqual(popcount8(v), 2);
34+
assert.strictEqual(popcount8(v), 2);
3735
// Wake both tasks up as simultaneously as possible. The other 2 tasks should
3836
// then start executing.
3937
Atomics.store(i32array, 0, 0);
@@ -53,7 +51,7 @@ test('coverage test for Atomics optimization (sync mode)', async (t: TestContext
5351

5452
// Wake up the remaining 2 tasks in order to make sure that the test finishes.
5553
// Do the same consistency check beforehand as above.
56-
t.assert.strictEqual(popcount8(v), 2);
54+
assert.strictEqual(popcount8(v), 2);
5755
Atomics.store(i32array, 0, 0);
5856
Atomics.notify(i32array, 0, Infinity);
5957

@@ -85,7 +83,7 @@ test('avoids unbounded recursion', async () => {
8583
await Promise.all(tasks);
8684
});
8785

88-
test('enable async mode', async (t: TestContext) => {
86+
test('enable async mode', async () => {
8987
const pool = new Piscina({
9088
filename: resolve(__dirname, 'fixtures/eval-params.js'),
9189
minThreads: 1,
@@ -112,12 +110,10 @@ test('enable async mode', async (t: TestContext) => {
112110
shared: bufs
113111
});
114112

115-
t.plan(2);
116-
117113
const atResult1 = Atomics.wait(bufs[0], 0, 0);
118114
const atResult2 = Atomics.wait(bufs[1], 0, 0);
119115
const atResult3 = Atomics.wait(bufs[2], 0, 0);
120116

121-
t.assert.deepStrictEqual([atResult1, atResult2, atResult3], ['ok', 'ok', 'ok']);
122-
t.assert.strictEqual(await promise, true);
117+
assert.deepStrictEqual([atResult1, atResult2, atResult3], ['ok', 'ok', 'ok']);
118+
assert.strictEqual(await promise, true);
123119
});

0 commit comments

Comments
 (0)