1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const cp = require('child_process');
5const fs = require('fs');
6const path = require('path');
7const tmpdir = require('../common/tmpdir');
8
9if (!common.hasCrypto)
10  common.skip('missing crypto');
11
12const { hkdf } = require('crypto');
13const { deflate } = require('zlib');
14const { Blob } = require('buffer');
15
16if (process.env.isChild === '1') {
17  hkdf('sha512', 'key', 'salt', 'info', 64, () => {});
18  deflate('hello', () => {});
19  // Make async call
20  const blob = new Blob(['h'.repeat(4096 * 2)]);
21  blob.arrayBuffer();
22  return;
23}
24
25tmpdir.refresh();
26const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
27
28cp.spawnSync(process.execPath,
29             [
30               '--trace-events-enabled',
31               '--trace-event-categories',
32               'node.threadpoolwork.sync,node.threadpoolwork.async',
33               __filename,
34             ],
35             {
36               cwd: tmpdir.path,
37               env: {
38                 ...process.env,
39                 isChild: '1',
40               },
41             });
42
43assert(fs.existsSync(FILE_NAME));
44const data = fs.readFileSync(FILE_NAME);
45const traces = JSON.parse(data.toString()).traceEvents;
46
47assert(traces.length > 0);
48
49let blobCount = 0;
50let zlibCount = 0;
51let cryptoCount = 0;
52
53traces.forEach((item) => {
54  if ([
55    'node,node.threadpoolwork,node.threadpoolwork.sync',
56    'node,node.threadpoolwork,node.threadpoolwork.async',
57  ].includes(item.cat)) {
58    if (item.name === 'blob') {
59      blobCount++;
60    } else if (item.name === 'zlib') {
61      zlibCount++;
62    } else if (item.name === 'crypto') {
63      cryptoCount++;
64    }
65  }
66});
67
68// There are three types, each type has two async events and sync events at least
69assert.ok(blobCount >= 4);
70assert.ok(zlibCount >= 4);
71assert.ok(cryptoCount >= 4);
72