Skip to content

Commit 352c31c

Browse files
gireeshpunathiltargos
authored andcommitted
test: strengthen test-worker-prof
Force main and worker to stay for some deterministic time Add some more validation check around profile file generation Fixes: #26401 PR-URL: #26608 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 7e072c8 commit 352c31c

File tree

1 file changed

+62
-24
lines changed

1 file changed

+62
-24
lines changed

test/parallel/test-worker-prof.js

+62-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,78 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const tmpdir = require('../common/tmpdir');
44
const fs = require('fs');
55
const assert = require('assert');
6+
const util = require('util');
67
const { join } = require('path');
78
const { spawnSync } = require('child_process');
8-
const { Worker } = require('worker_threads');
99

1010
// Test that --prof also tracks Worker threads.
1111
// Refs: https://github.com/nodejs/node/issues/24016
1212

1313
if (process.argv[2] === 'child') {
14-
const spin = `
15-
const start = Date.now();
16-
while (Date.now() - start < 1000);
14+
let files = fs.readdirSync(tmpdir.path);
15+
const plog = files.filter((name) => /\.log$/.test(name))[0];
16+
if (plog === undefined) {
17+
console.error('`--prof` did not produce a profile log for parent thread!');
18+
process.exit(1);
19+
}
20+
const pingpong = `
21+
let counter = 0;
22+
const { Worker, parentPort } = require('worker_threads');
23+
parentPort.on('message', (m) => {
24+
if (counter++ === 1024)
25+
process.exit(0);
26+
parentPort.postMessage(
27+
m.toString().split('').reverse().toString().replace(/,/g, ''));
28+
});
1729
`;
18-
new Worker(spin, { eval: true });
19-
eval(spin);
20-
return;
21-
}
2230

23-
tmpdir.refresh();
24-
spawnSync(process.execPath, ['--prof', __filename, 'child'],
25-
{ cwd: tmpdir.path });
26-
const files = fs.readdirSync(tmpdir.path);
27-
const logfiles = files.filter((name) => /\.log$/.test(name));
28-
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
31+
const { Worker } = require('worker_threads');
32+
const data = 'x'.repeat(1024);
33+
const w = new Worker(pingpong, { eval: true });
34+
w.on('message', (m) => {
35+
w.postMessage(m.toString().split('').reverse().toString().replace(/,/g, ''));
36+
});
37+
38+
w.on('exit', common.mustCall(() => {
39+
files = fs.readdirSync(tmpdir.path);
40+
const wlog = files.filter((name) => /\.log$/.test(name) && name !== plog)[0];
41+
if (wlog === undefined) {
42+
console.error('`--prof` did not produce a profile log' +
43+
' for worker thread!');
44+
process.exit(1);
45+
}
46+
process.exit(0);
47+
}));
48+
w.postMessage(data);
49+
} else {
50+
tmpdir.refresh();
51+
const spawnResult = spawnSync(
52+
process.execPath, ['--prof', __filename, 'child'],
53+
{ cwd: tmpdir.path, encoding: 'utf8' });
54+
assert.strictEqual(spawnResult.stderr.toString(), '',
55+
`child exited with an error: \
56+
${util.inspect(spawnResult)}`);
57+
assert.strictEqual(spawnResult.signal, null,
58+
`child exited with signal: ${util.inspect(spawnResult)}`);
59+
assert.strictEqual(spawnResult.status, 0,
60+
`child exited with non-zero status: \
61+
${util.inspect(spawnResult)}`);
62+
const files = fs.readdirSync(tmpdir.path);
63+
const logfiles = files.filter((name) => /\.log$/.test(name));
64+
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
2965

30-
for (const logfile of logfiles) {
31-
const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');
32-
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
66+
for (const logfile of logfiles) {
67+
const lines = fs.readFileSync(
68+
join(tmpdir.path, logfile), 'utf8').split('\n');
69+
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
3370

34-
// Test that at least 15 ticks have been recorded for both parent and child
35-
// threads. When not tracking Worker threads, only 1 or 2 ticks would
36-
// have been recorded.
37-
// When running locally on x64 Linux, this number is usually at least 700
38-
// for both threads, so 15 seems like a very safe threshold.
39-
assert(ticks >= 15, `${ticks} >= 15`);
71+
// Test that at least 15 ticks have been recorded for both parent and child
72+
// threads. When not tracking Worker threads, only 1 or 2 ticks would
73+
// have been recorded.
74+
// When running locally on x64 Linux, this number is usually at least 200
75+
// for both threads, so 15 seems like a very safe threshold.
76+
assert(ticks >= 15, `${ticks} >= 15`);
77+
}
4078
}

0 commit comments

Comments
 (0)