source: trip-planner-front/node_modules/piscina/test/async-context.ts@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 945 bytes
Line 
1import { createHook, executionAsyncId } from 'async_hooks';
2import Piscina from '..';
3import { test } from 'tap';
4import { resolve } from 'path';
5
6test('postTask() calls the correct async hooks', async ({ equal }) => {
7 let taskId;
8 let initCalls = 0;
9 let beforeCalls = 0;
10 let afterCalls = 0;
11 let resolveCalls = 0;
12
13 const hook = createHook({
14 init (id, type) {
15 if (type === 'Piscina.Task') {
16 initCalls++;
17 taskId = id;
18 }
19 },
20 before (id) {
21 if (id === taskId) beforeCalls++;
22 },
23 after (id) {
24 if (id === taskId) afterCalls++;
25 },
26 promiseResolve () {
27 if (executionAsyncId() === taskId) resolveCalls++;
28 }
29 });
30 hook.enable();
31
32 const pool = new Piscina({
33 filename: resolve(__dirname, 'fixtures/eval.js')
34 });
35
36 await pool.runTask('42');
37
38 hook.disable();
39 equal(initCalls, 1);
40 equal(beforeCalls, 1);
41 equal(afterCalls, 1);
42 equal(resolveCalls, 1);
43});
Note: See TracBrowser for help on using the repository browser.