Skip to content

Commit

Permalink
test: make test-tick-processor.js non-flaky
Browse files Browse the repository at this point in the history
Wait for a sought-for symbol to appear instead of just hard-killing
subprocesses at 2s timeout.

Fix: nodejs#4427
  • Loading branch information
indutny committed Sep 14, 2016
1 parent 5ce117b commit a4b9b17
Showing 1 changed file with 68 additions and 24 deletions.
92 changes: 68 additions & 24 deletions test/parallel/test-tick-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const cp = require('child_process');
const path = require('path');

// TODO(mhdawson) Currently the test-tick-processor functionality in V8
// depends on addresses being smaller than a full 64 bits. Aix supports
Expand All @@ -15,52 +16,95 @@ if (common.isAix) {
}

common.refreshTmpDir();
process.chdir(common.tmpDir);

const tests = [];

const LOG_FILE = path.join(common.tmpDir, 'tick-processor.log');
const RETRY_TIMEOUT = 750;

// Unknown checked for to prevent flakiness, if pattern is not found,
// then a large number of unknown ticks should be present
runTest(/LazyCompile.*\[eval\]:1|.*% UNKNOWN/,
`function f() {
tests.push({
pattern: /LazyCompile.*\[eval\]:1|.*% UNKNOWN/,
code: `function f() {
for (var i = 0; i < 1000000; i++) {
i++;
}
setImmediate(function() { f(); });
};
setTimeout(function() { process.exit(0); }, 2000);
f();`);
f();`
});

if (common.isWindows ||
common.isSunOS ||
common.isAix ||
common.isLinuxPPCBE ||
common.isFreeBSD) {
common.skip('C++ symbols are not mapped for this os.');
return;
} else {
}
runTest(/RunInDebugContext/,
`function f() {
tests.push({
pattern: /RunInDebugContext/,
code: `function f() {
require(\'vm\').runInDebugContext(\'Debug\');
setImmediate(function() { f(); });
};
setTimeout(function() { process.exit(0); }, 2000);
f();`);
f();`
});

runTest(/Builtin_DateNow/,
`function f() {
tests.push({
pattern: /Builtin_DateNow/,
code: `function f() {
this.ts = Date.now();
setImmediate(function() { new f(); });
};
setTimeout(function() { process.exit(0); }, 2000);
f();`);
f();`
});

runTest();

function runTest() {
if (tests.length === 0)
return;

const test = tests.shift();

const proc = cp.spawn(process.execPath, [
'--no_logfile_per_isolate',
`--logfile=${LOG_FILE}`,
'--prof',
'-pe', test.code
], {
stdio: [ null, null, 'inherit' ]
});

// Try to match after timeout
setTimeout(() => {
match(test.pattern, proc, runTest);
}, RETRY_TIMEOUT);
}

function match(pattern, parent, cb) {
const proc = cp.spawn(process.execPath, [
'--prof-process',
'--call-graph-size=10',
LOG_FILE
], {
stdio: [ null, 'pipe', 'inherit' ]
});

function runTest(pattern, code) {
cp.execFileSync(process.execPath, ['-prof', '-pe', code]);
var matches = fs.readdirSync(common.tmpDir);
let out = '';
proc.stdout.on('data', (chunk) => {
out += chunk;
});
proc.stdout.on('end', () => {
// Retry after timeout
if (!pattern.test(out))
return setTimeout(() => match(pattern, parent, cb), RETRY_TIMEOUT);

assert.strictEqual(matches.length, 1, 'There should be a single log file.');
parent.kill('SIGTERM');

var log = matches[0];
var out = cp.execSync(process.execPath +
' --prof-process --call-graph-size=10 ' + log,
{encoding: 'utf8'});
assert(pattern.test(out), `${pattern} not matching ${out}`);
fs.unlinkSync(log);
fs.unlinkSync(LOG_FILE);
cb(null);
});
}

0 comments on commit a4b9b17

Please sign in to comment.