1
- import { EventEmitter } from 'events' ;
2
- import Piscina from '..' ;
3
1
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 '..' ;
5
5
import { resolve } from 'path' ;
6
6
7
7
const TIMEOUT_MAX = 2 ** 31 - 1 ;
8
8
9
- test ( 'tasks can be aborted through AbortController while running' , async ( t : TestContext ) => {
9
+ test ( 'tasks can be aborted through AbortController while running' , ( ) => {
10
10
const pool = new Piscina ( {
11
11
filename : resolve ( __dirname , 'fixtures/notify-then-sleep.ts' )
12
12
} ) ;
13
13
14
14
const buf = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
15
15
const abortController = new AbortController ( ) ;
16
- t . assert . rejects ( pool . run ( buf , { signal : abortController . signal } ) ,
16
+ assert . rejects ( pool . run ( buf , { signal : abortController . signal } ) ,
17
17
/ T h e t a s k h a s b e e n a b o r t e d / ) ;
18
18
19
19
Atomics . wait ( buf , 0 , 0 ) ;
20
- t . assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
20
+ assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
21
21
22
22
abortController . abort ( ) ;
23
23
} ) ;
24
24
25
- test ( 'tasks can be aborted through EventEmitter while running' , async ( t : TestContext ) => {
25
+ test ( 'tasks can be aborted through EventEmitter while running' , ( ) => {
26
26
const pool = new Piscina ( {
27
27
filename : resolve ( __dirname , 'fixtures/notify-then-sleep.ts' )
28
28
} ) ;
29
29
30
30
const buf = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
31
31
const ee = new EventEmitter ( ) ;
32
- t . assert . rejects ( pool . run ( buf , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
32
+ assert . rejects ( pool . run ( buf , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
33
33
34
34
Atomics . wait ( buf , 0 , 0 ) ;
35
- t . assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
35
+ assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
36
36
37
37
ee . emit ( 'abort' ) ;
38
38
} ) ;
39
39
40
- test ( 'tasks can be aborted through EventEmitter before running' , async ( t : TestContext ) => {
40
+ test ( 'tasks can be aborted through EventEmitter before running' , async ( ) => {
41
41
const pool = new Piscina ( {
42
42
filename : resolve ( __dirname , 'fixtures/wait-for-notify.js' ) ,
43
43
maxThreads : 1
@@ -50,8 +50,8 @@ test('tasks can be aborted through EventEmitter before running', async (t: TestC
50
50
const ee = new EventEmitter ( ) ;
51
51
const task1 = pool . run ( bufs [ 0 ] ) ;
52
52
const abortable = pool . run ( bufs [ 1 ] , { signal : ee } ) ;
53
- t . assert . strictEqual ( pool . queueSize , 0 ) ; // Means it's running
54
- t . assert . rejects ( abortable , / T h e t a s k h a s b e e n a b o r t e d / ) ;
53
+ assert . strictEqual ( pool . queueSize , 0 ) ; // Means it's running
54
+ assert . rejects ( abortable , / T h e t a s k h a s b e e n a b o r t e d / ) ;
55
55
56
56
ee . emit ( 'abort' ) ;
57
57
@@ -61,7 +61,7 @@ test('tasks can be aborted through EventEmitter before running', async (t: TestC
61
61
await task1 ;
62
62
} ) ;
63
63
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 ( ) => {
65
65
const pool = new Piscina ( {
66
66
filename : resolve ( __dirname , 'fixtures/wait-for-notify.ts' ) ,
67
67
maxThreads : 1 ,
@@ -74,8 +74,8 @@ test('abortable tasks will not share workers (abortable posted second)', async (
74
74
] ;
75
75
const task1 = pool . run ( bufs [ 0 ] ) ;
76
76
const ee = new EventEmitter ( ) ;
77
- t . assert . rejects ( pool . run ( bufs [ 1 ] , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
78
- t . assert . strictEqual ( pool . queueSize , 0 ) ;
77
+ assert . rejects ( pool . run ( bufs [ 1 ] , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
78
+ assert . strictEqual ( pool . queueSize , 0 ) ;
79
79
80
80
ee . emit ( 'abort' ) ;
81
81
@@ -86,26 +86,26 @@ test('abortable tasks will not share workers (abortable posted second)', async (
86
86
} ) ;
87
87
88
88
// 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 ( ) => {
90
90
const pool = new Piscina ( {
91
91
filename : resolve ( __dirname , 'fixtures/eval.js' ) ,
92
92
maxThreads : 1 ,
93
93
concurrentTasksPerWorker : 2
94
94
} ) ;
95
95
96
96
const ee = new EventEmitter ( ) ;
97
- t . assert . rejects ( pool . run ( 'while(true);' , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
97
+ assert . rejects ( pool . run ( 'while(true);' , { signal : ee } ) , / T h e t a s k h a s b e e n a b o r t e d / ) ;
98
98
const task2 = pool . run ( '42' ) ;
99
- t . assert . strictEqual ( pool . queueSize , 1 ) ;
99
+ assert . strictEqual ( pool . queueSize , 1 ) ;
100
100
101
101
ee . emit ( 'abort' ) ;
102
102
103
103
// Wake up the thread handling the second task.
104
- t . assert . strictEqual ( await task2 , 42 ) ;
104
+ assert . strictEqual ( await task2 , 42 ) ;
105
105
} ) ;
106
106
107
107
// 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 ( ) => {
109
109
const pool = new Piscina ( {
110
110
filename : resolve ( __dirname , 'fixtures/sleep.js' ) ,
111
111
maxThreads : 1 ,
@@ -123,12 +123,12 @@ test('abortable tasks will not share workers (on worker available)', async (t: T
123
123
pool . run ( { time : 100 , a : 3 } , { signal : new EventEmitter ( ) } )
124
124
] ) ;
125
125
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 ) ;
129
129
} ) ;
130
130
131
- test ( 'abortable tasks will not share workers (destroy workers)' , async ( t : TestContext ) => {
131
+ test ( 'abortable tasks will not share workers (destroy workers)' , ( ) => {
132
132
const pool = new Piscina ( {
133
133
filename : resolve ( __dirname , 'fixtures/sleep.js' ) ,
134
134
maxThreads : 1 ,
@@ -145,30 +145,30 @@ test('abortable tasks will not share workers (destroy workers)', async (t: TestC
145
145
pool . destroy ( ) ;
146
146
} ) ;
147
147
148
- t . assert . rejects ( pool . run ( { time : TIMEOUT_MAX , a : 2 } ) , / T e r m i n a t i n g w o r k e r t h r e a d / ) ;
149
- t . assert . rejects ( pool . run ( { time : TIMEOUT_MAX , a : 3 } , { signal : new EventEmitter ( ) } ) ,
148
+ assert . rejects ( pool . run ( { time : TIMEOUT_MAX , a : 2 } ) , / T e r m i n a t i n g w o r k e r t h r e a d / ) ;
149
+ assert . rejects ( pool . run ( { time : TIMEOUT_MAX , a : 3 } , { signal : new EventEmitter ( ) } ) ,
150
150
/ T e r m i n a t i n g w o r k e r t h r e a d / ) ;
151
151
} ) ;
152
152
153
- test ( 'aborted AbortSignal rejects task immediately' , async ( t : TestContext ) => {
153
+ test ( 'aborted AbortSignal rejects task immediately' , ( ) => {
154
154
const pool = new Piscina ( {
155
155
filename : resolve ( __dirname , 'fixtures/move.ts' )
156
156
} ) ;
157
157
158
158
const controller = new AbortController ( ) ;
159
159
// Abort the controller early
160
160
controller . abort ( ) ;
161
- t . assert . strictEqual ( controller . signal . aborted , true ) ;
161
+ assert . strictEqual ( controller . signal . aborted , true ) ;
162
162
163
163
// The data won't be moved because the task will abort immediately.
164
164
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 ] } ) ,
166
166
/ T h e t a s k h a s b e e n a b o r t e d / ) ;
167
167
168
- t . assert . strictEqual ( data . length , 4 ) ;
168
+ assert . strictEqual ( data . length , 4 ) ;
169
169
} ) ;
170
170
171
- test ( 'task with AbortSignal cleans up properly' , async ( t : TestContext ) => {
171
+ test ( 'task with AbortSignal cleans up properly' , async ( ) => {
172
172
const pool = new Piscina ( {
173
173
filename : resolve ( __dirname , 'fixtures/eval.js' )
174
174
} ) ;
@@ -179,39 +179,39 @@ test('task with AbortSignal cleans up properly', async (t: TestContext) => {
179
179
180
180
const { getEventListeners } = EventEmitter as any ;
181
181
if ( typeof getEventListeners === 'function' ) {
182
- t . assert . strictEqual ( getEventListeners ( ee , 'abort' ) . length , 0 ) ;
182
+ assert . strictEqual ( getEventListeners ( ee , 'abort' ) . length , 0 ) ;
183
183
}
184
184
185
185
const controller = new AbortController ( ) ;
186
186
187
187
await pool . run ( '1+1' , { signal : controller . signal } ) ;
188
188
} ) ;
189
189
190
- test ( 'aborted AbortSignal rejects task immediately (with reason)' , async ( t : TestContext ) => {
190
+ test ( 'aborted AbortSignal rejects task immediately (with reason)' , async ( ) => {
191
191
const pool = new Piscina ( {
192
192
filename : resolve ( __dirname , 'fixtures/move.ts' )
193
193
} ) ;
194
194
const customReason = new Error ( 'custom reason' ) ;
195
195
196
196
const controller = new AbortController ( ) ;
197
197
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 ) ;
200
200
201
201
// The data won't be moved because the task will abort immediately.
202
202
const data = new Uint8Array ( new SharedArrayBuffer ( 4 ) ) ;
203
203
204
204
try {
205
205
await pool . run ( data , { transferList : [ data . buffer ] , signal : controller . signal } ) ;
206
206
} 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 ) ;
209
209
}
210
210
211
- t . assert . strictEqual ( data . length , 4 ) ;
211
+ assert . strictEqual ( data . length , 4 ) ;
212
212
} ) ;
213
213
214
- test ( 'tasks can be aborted through AbortController while running' , async ( t : TestContext ) => {
214
+ test ( 'tasks can be aborted through AbortController while running' , async ( ) => {
215
215
const pool = new Piscina ( {
216
216
filename : resolve ( __dirname , 'fixtures/notify-then-sleep.ts' )
217
217
} ) ;
@@ -224,42 +224,42 @@ test('tasks can be aborted through AbortController while running', async (t: Tes
224
224
const promise = pool . run ( buf , { signal : abortController . signal } ) ;
225
225
226
226
Atomics . wait ( buf , 0 , 0 ) ;
227
- t . assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
227
+ assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
228
228
229
229
abortController . abort ( reason ) ;
230
230
231
231
await promise ;
232
232
} 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 ) ;
235
235
}
236
236
} ) ;
237
237
238
- test ( 'aborted AbortSignal rejects task immediately (with reason)' , async ( t : TestContext ) => {
238
+ test ( 'aborted AbortSignal rejects task immediately (with reason)' , async ( ) => {
239
239
const pool = new Piscina ( {
240
240
filename : resolve ( __dirname , 'fixtures/move.ts' )
241
241
} ) ;
242
242
const customReason = new Error ( 'custom reason' ) ;
243
243
244
244
const controller = new AbortController ( ) ;
245
245
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 ) ;
248
248
249
249
// The data won't be moved because the task will abort immediately.
250
250
const data = new Uint8Array ( new SharedArrayBuffer ( 4 ) ) ;
251
251
252
252
try {
253
253
await pool . run ( data , { transferList : [ data . buffer ] , signal : controller . signal } ) ;
254
254
} 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 ) ;
257
257
}
258
258
259
- t . assert . strictEqual ( data . length , 4 ) ;
259
+ assert . strictEqual ( data . length , 4 ) ;
260
260
} ) ;
261
261
262
- test ( 'tasks can be aborted through AbortController while running' , async ( t : TestContext ) => {
262
+ test ( 'tasks can be aborted through AbortController while running' , async ( ) => {
263
263
const pool = new Piscina ( {
264
264
filename : resolve ( __dirname , 'fixtures/notify-then-sleep.ts' )
265
265
} ) ;
@@ -272,13 +272,13 @@ test('tasks can be aborted through AbortController while running', async (t: Tes
272
272
const promise = pool . run ( buf , { signal : abortController . signal } ) ;
273
273
274
274
Atomics . wait ( buf , 0 , 0 ) ;
275
- t . assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
275
+ assert . strictEqual ( Atomics . load ( buf , 0 ) , 1 ) ;
276
276
277
277
abortController . abort ( reason ) ;
278
278
279
279
await promise ;
280
280
} 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 ) ;
283
283
}
284
284
} ) ;
0 commit comments