Files
node/test/parallel/test-worker-prof.js
T
Richard Lau 82a256ac67 test: fix tests so they work in worker threads
Use the `cwd` option for child_process instead of `process.chdir()` to
allow tests to work with worker threads.

PR-URL: https://github.com/nodejs/node/pull/26453
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2019-03-07 11:09:30 -05:00

41 lines
1.4 KiB
JavaScript

'use strict';
require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
const { join } = require('path');
const { spawnSync } = require('child_process');
const { Worker } = require('worker_threads');
// Test that --prof also tracks Worker threads.
// Refs: https://github.com/nodejs/node/issues/24016
if (process.argv[2] === 'child') {
const spin = `
const start = Date.now();
while (Date.now() - start < 1000);
`;
new Worker(spin, { eval: true });
eval(spin);
return;
}
tmpdir.refresh();
spawnSync(process.execPath, ['--prof', __filename, 'child'],
{ cwd: tmpdir.path });
const files = fs.readdirSync(tmpdir.path);
const logfiles = files.filter((name) => /\.log$/.test(name));
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
for (const logfile of logfiles) {
const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
// Test that at least 15 ticks have been recorded for both parent and child
// threads. When not tracking Worker threads, only 1 or 2 ticks would
// have been recorded.
// When running locally on x64 Linux, this number is usually at least 700
// for both threads, so 15 seems like a very safe threshold.
assert(ticks >= 15, `${ticks} >= 15`);
}