1'use strict';
2const common = require('../common');
3const { isCPPSymbolsNotMapped } = require('./util');
4const tmpdir = require('../common/tmpdir');
5tmpdir.refresh();
6
7if (isCPPSymbolsNotMapped) {
8  common.skip('C++ symbols are not mapped for this OS.');
9}
10
11// This test will produce a broken profile log.
12// ensure prof-polyfill not stuck in infinite loop
13// and success process
14
15
16const assert = require('assert');
17const { spawn, spawnSync } = require('child_process');
18const path = require('path');
19const { writeFileSync } = require('fs');
20
21const LOG_FILE = path.join(tmpdir.path, 'tick-processor.log');
22const RETRY_TIMEOUT = 150;
23const BROKEN_PART = 'tick,';
24const WARN_REG_EXP = /\(node:\d+\) \[BROKEN_PROFILE_FILE] Warning: Profile file .* is broken/;
25const WARN_DETAIL_REG_EXP = /".*tick," at the file end is broken/;
26
27const code = `function f() {
28           this.ts = Date.now();
29           setImmediate(function() { new f(); });
30         };
31         f();`;
32
33const proc = spawn(process.execPath, [
34  '--no_logfile_per_isolate',
35  '--logfile=-',
36  '--prof',
37  '-pe', code,
38], {
39  stdio: ['ignore', 'pipe', 'inherit'],
40});
41
42let ticks = '';
43proc.stdout.on('data', (chunk) => ticks += chunk);
44
45
46function runPolyfill(content) {
47  proc.kill();
48  content += BROKEN_PART;
49  writeFileSync(LOG_FILE, content);
50  const child = spawnSync(
51    `${process.execPath}`,
52    [
53      '--prof-process', LOG_FILE,
54    ]);
55  assert.match(child.stderr.toString(), WARN_REG_EXP);
56  assert.match(child.stderr.toString(), WARN_DETAIL_REG_EXP);
57  assert.strictEqual(child.status, 0);
58}
59
60setTimeout(() => runPolyfill(ticks), RETRY_TIMEOUT);
61