forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-context.ts
43 lines (38 loc) · 941 Bytes
/
async-context.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
import { createHook, executionAsyncId } from 'async_hooks';
import Piscina from '..';
import { test } from 'tap';
import { resolve } from 'path';
test('postTask() calls the correct async hooks', async ({ equal }) => {
let taskId;
let initCalls = 0;
let beforeCalls = 0;
let afterCalls = 0;
let resolveCalls = 0;
const hook = createHook({
init (id, type) {
if (type === 'Piscina.Task') {
initCalls++;
taskId = id;
}
},
before (id) {
if (id === taskId) beforeCalls++;
},
after (id) {
if (id === taskId) afterCalls++;
},
promiseResolve () {
if (executionAsyncId() === taskId) resolveCalls++;
}
});
hook.enable();
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
await pool.run('42');
hook.disable();
equal(initCalls, 1);
equal(beforeCalls, 1);
equal(afterCalls, 1);
equal(resolveCalls, 1);
});