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>
This commit is contained in:
parent
76e67e9884
commit
82a256ac67
@ -34,9 +34,6 @@ const path = require('path');
|
|||||||
const fixtures = require('../common/fixtures');
|
const fixtures = require('../common/fixtures');
|
||||||
const nodejs = `"${process.execPath}"`;
|
const nodejs = `"${process.execPath}"`;
|
||||||
|
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
if (process.argv.length > 2) {
|
if (process.argv.length > 2) {
|
||||||
console.log(process.argv.slice(2).join(' '));
|
console.log(process.argv.slice(2).join(' '));
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
@ -98,16 +95,14 @@ child.exec(`${nodejs} --print "os.platform()"`,
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
// Module path resolve bug regression test.
|
// Module path resolve bug regression test.
|
||||||
const cwd = process.cwd();
|
|
||||||
process.chdir(path.resolve(__dirname, '../../'));
|
|
||||||
child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`,
|
child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`,
|
||||||
|
{ cwd: path.resolve(__dirname, '../../') },
|
||||||
common.mustCall((err, stdout, stderr) => {
|
common.mustCall((err, stdout, stderr) => {
|
||||||
assert.strictEqual(err.code, 42);
|
assert.strictEqual(err.code, 42);
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
stdout, 'Loaded as a module, exiting with status code 42.\n');
|
stdout, 'Loaded as a module, exiting with status code 42.\n');
|
||||||
assert.strictEqual(stderr, '');
|
assert.strictEqual(stderr, '');
|
||||||
}));
|
}));
|
||||||
process.chdir(cwd);
|
|
||||||
|
|
||||||
// Missing argument should not crash.
|
// Missing argument should not crash.
|
||||||
child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => {
|
child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => {
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
if (process.config.variables.node_without_node_options)
|
if (process.config.variables.node_without_node_options)
|
||||||
common.skip('missing NODE_OPTIONS support');
|
common.skip('missing NODE_OPTIONS support');
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
// Test options specified by env variable.
|
// Test options specified by env variable.
|
||||||
|
|
||||||
@ -12,7 +10,6 @@ const exec = require('child_process').execFile;
|
|||||||
|
|
||||||
const tmpdir = require('../common/tmpdir');
|
const tmpdir = require('../common/tmpdir');
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
process.chdir(tmpdir.path);
|
|
||||||
|
|
||||||
disallow('--version');
|
disallow('--version');
|
||||||
disallow('-v');
|
disallow('-v');
|
||||||
@ -32,7 +29,7 @@ disallow('--');
|
|||||||
|
|
||||||
function disallow(opt) {
|
function disallow(opt) {
|
||||||
const env = Object.assign({}, process.env, { NODE_OPTIONS: opt });
|
const env = Object.assign({}, process.env, { NODE_OPTIONS: opt });
|
||||||
exec(process.execPath, { env }, common.mustCall(function(err) {
|
exec(process.execPath, { cwd: tmpdir.path, env }, common.mustCall((err) => {
|
||||||
const message = err.message.split(/\r?\n/)[1];
|
const message = err.message.split(/\r?\n/)[1];
|
||||||
const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
|
const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
if (process.config.variables.node_without_node_options)
|
if (process.config.variables.node_without_node_options)
|
||||||
common.skip('missing NODE_OPTIONS support');
|
common.skip('missing NODE_OPTIONS support');
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
// Test options specified by env variable.
|
// Test options specified by env variable.
|
||||||
|
|
||||||
@ -12,7 +10,6 @@ const exec = require('child_process').execFile;
|
|||||||
|
|
||||||
const tmpdir = require('../common/tmpdir');
|
const tmpdir = require('../common/tmpdir');
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
process.chdir(tmpdir.path);
|
|
||||||
|
|
||||||
const printA = require.resolve('../fixtures/printA.js');
|
const printA = require.resolve('../fixtures/printA.js');
|
||||||
expect(`-r ${printA}`, 'A\nB\n');
|
expect(`-r ${printA}`, 'A\nB\n');
|
||||||
@ -64,6 +61,7 @@ expect('--stack-trace-limit=100',
|
|||||||
function expect(opt, want, command = 'console.log("B")', wantsError = false) {
|
function expect(opt, want, command = 'console.log("B")', wantsError = false) {
|
||||||
const argv = ['-e', command];
|
const argv = ['-e', command];
|
||||||
const opts = {
|
const opts = {
|
||||||
|
cwd: tmpdir.path,
|
||||||
env: Object.assign({}, process.env, { NODE_OPTIONS: opt }),
|
env: Object.assign({}, process.env, { NODE_OPTIONS: opt }),
|
||||||
maxBuffer: 1e6,
|
maxBuffer: 1e6,
|
||||||
};
|
};
|
||||||
|
@ -3,31 +3,28 @@
|
|||||||
// This tests that process.argv is the same in the preloaded module
|
// This tests that process.argv is the same in the preloaded module
|
||||||
// and the user module.
|
// and the user module.
|
||||||
|
|
||||||
const common = require('../common');
|
require('../common');
|
||||||
|
|
||||||
const tmpdir = require('../common/tmpdir');
|
const tmpdir = require('../common/tmpdir');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const { join } = require('path');
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
if (!common.isMainThread) {
|
|
||||||
common.skip('Cannot chdir to the tmp directory in workers');
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
|
|
||||||
process.chdir(tmpdir.path);
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
'preload.js',
|
join(tmpdir.path, 'preload.js'),
|
||||||
'console.log(JSON.stringify(process.argv));',
|
'console.log(JSON.stringify(process.argv));',
|
||||||
'utf-8');
|
'utf-8');
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
'main.js',
|
join(tmpdir.path, 'main.js'),
|
||||||
'console.log(JSON.stringify(process.argv));',
|
'console.log(JSON.stringify(process.argv));',
|
||||||
'utf-8');
|
'utf-8');
|
||||||
|
|
||||||
const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js']);
|
const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js'],
|
||||||
|
{ cwd: tmpdir.path });
|
||||||
|
|
||||||
if (child.status !== 0) {
|
if (child.status !== 0) {
|
||||||
console.log(child.stderr.toString());
|
console.log(child.stderr.toString());
|
||||||
|
@ -5,8 +5,6 @@ const fixtures = require('../common/fixtures');
|
|||||||
// Refs: https://github.com/nodejs/node/pull/2253
|
// Refs: https://github.com/nodejs/node/pull/2253
|
||||||
if (common.isSunOS)
|
if (common.isSunOS)
|
||||||
common.skip('unreliable on SunOS');
|
common.skip('unreliable on SunOS');
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
@ -133,9 +131,9 @@ childProcess.exec(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Test that preloading with a relative path works
|
// Test that preloading with a relative path works
|
||||||
process.chdir(fixtures.fixturesDir);
|
|
||||||
childProcess.exec(
|
childProcess.exec(
|
||||||
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
|
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
|
||||||
|
{ cwd: fixtures.fixturesDir },
|
||||||
common.mustCall(function(err, stdout, stderr) {
|
common.mustCall(function(err, stdout, stderr) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.strictEqual(stdout, 'A\nB\n');
|
assert.strictEqual(stdout, 'A\nB\n');
|
||||||
@ -145,6 +143,7 @@ if (common.isWindows) {
|
|||||||
// https://github.com/nodejs/node/issues/21918
|
// https://github.com/nodejs/node/issues/21918
|
||||||
childProcess.exec(
|
childProcess.exec(
|
||||||
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
|
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
|
||||||
|
{ cwd: fixtures.fixturesDir },
|
||||||
common.mustCall(function(err, stdout, stderr) {
|
common.mustCall(function(err, stdout, stderr) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.strictEqual(stdout, 'A\nB\n');
|
assert.strictEqual(stdout, 'A\nB\n');
|
||||||
@ -153,10 +152,10 @@ if (common.isWindows) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/nodejs/node/issues/1691
|
// https://github.com/nodejs/node/issues/1691
|
||||||
process.chdir(fixtures.fixturesDir);
|
|
||||||
childProcess.exec(
|
childProcess.exec(
|
||||||
`"${nodeBinary}" --require ` +
|
`"${nodeBinary}" --require ` +
|
||||||
`"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
|
`"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
|
||||||
|
{ cwd: fixtures.fixturesDir },
|
||||||
function(err, stdout, stderr) {
|
function(err, stdout, stderr) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.ok(/worker terminated with code 43/.test(stdout));
|
assert.ok(/worker terminated with code 43/.test(stdout));
|
||||||
|
@ -5,20 +5,18 @@ const fs = require('fs');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
|
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('chdir not available in workers');
|
|
||||||
if (!common.enoughTestMem)
|
if (!common.enoughTestMem)
|
||||||
common.skip('skipped due to memory requirements');
|
common.skip('skipped due to memory requirements');
|
||||||
if (common.isAIX)
|
if (common.isAIX)
|
||||||
common.skip('does not work on AIX');
|
common.skip('does not work on AIX');
|
||||||
|
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
process.chdir(tmpdir.path);
|
|
||||||
|
|
||||||
// Generate log file.
|
// Generate log file.
|
||||||
spawnSync(process.execPath, [ '--prof', '-p', '42' ]);
|
spawnSync(process.execPath, [ '--prof', '-p', '42' ], { cwd: tmpdir.path });
|
||||||
|
|
||||||
const logfile = fs.readdirSync('.').filter((name) => name.endsWith('.log'))[0];
|
const files = fs.readdirSync(tmpdir.path);
|
||||||
|
const logfile = files.filter((name) => /\.log$/.test(name))[0];
|
||||||
assert(logfile);
|
assert(logfile);
|
||||||
|
|
||||||
// Make sure that the --preprocess argument is passed through correctly,
|
// Make sure that the --preprocess argument is passed through correctly,
|
||||||
@ -28,7 +26,7 @@ assert(logfile);
|
|||||||
const { stdout } = spawnSync(
|
const { stdout } = spawnSync(
|
||||||
process.execPath,
|
process.execPath,
|
||||||
[ '--prof-process', '--preprocess', logfile ],
|
[ '--prof-process', '--preprocess', logfile ],
|
||||||
{ encoding: 'utf8' });
|
{ cwd: tmpdir.path, encoding: 'utf8' });
|
||||||
|
|
||||||
// Make sure that the result is valid JSON.
|
// Make sure that the result is valid JSON.
|
||||||
JSON.parse(stdout);
|
JSON.parse(stdout);
|
||||||
|
@ -10,9 +10,6 @@ const tmpdir = require('../common/tmpdir');
|
|||||||
|
|
||||||
// This tests the emission of node.environment trace events
|
// This tests the emission of node.environment trace events
|
||||||
|
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
const names = new Set([
|
const names = new Set([
|
||||||
'Environment',
|
'Environment',
|
||||||
'RunAndClearNativeImmediates',
|
'RunAndClearNativeImmediates',
|
||||||
@ -32,10 +29,10 @@ if (process.argv[2] === 'child') {
|
|||||||
setTimeout(() => { 1 + 1; }, 1);
|
setTimeout(() => { 1 + 1; }, 1);
|
||||||
} else {
|
} else {
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
process.chdir(tmpdir.path);
|
|
||||||
|
|
||||||
const proc = cp.fork(__filename,
|
const proc = cp.fork(__filename,
|
||||||
[ 'child' ], {
|
[ 'child' ], {
|
||||||
|
cwd: tmpdir.path,
|
||||||
execArgv: [
|
execArgv: [
|
||||||
'--trace-event-categories',
|
'--trace-event-categories',
|
||||||
'node.environment'
|
'node.environment'
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
require('../common');
|
||||||
const tmpdir = require('../common/tmpdir');
|
const tmpdir = require('../common/tmpdir');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const { join } = require('path');
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
const { Worker } = require('worker_threads');
|
const { Worker } = require('worker_threads');
|
||||||
|
|
||||||
if (!common.isMainThread)
|
|
||||||
common.skip('process.chdir is not available in Workers');
|
|
||||||
|
|
||||||
// Test that --prof also tracks Worker threads.
|
// Test that --prof also tracks Worker threads.
|
||||||
// Refs: https://github.com/nodejs/node/issues/24016
|
// Refs: https://github.com/nodejs/node/issues/24016
|
||||||
|
|
||||||
@ -23,13 +21,14 @@ if (process.argv[2] === 'child') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
process.chdir(tmpdir.path);
|
spawnSync(process.execPath, ['--prof', __filename, 'child'],
|
||||||
spawnSync(process.execPath, ['--prof', __filename, 'child']);
|
{ cwd: tmpdir.path });
|
||||||
const logfiles = fs.readdirSync('.').filter((name) => /\.log$/.test(name));
|
const files = fs.readdirSync(tmpdir.path);
|
||||||
|
const logfiles = files.filter((name) => /\.log$/.test(name));
|
||||||
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
|
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
|
||||||
|
|
||||||
for (const logfile of logfiles) {
|
for (const logfile of logfiles) {
|
||||||
const lines = fs.readFileSync(logfile, 'utf8').split('\n');
|
const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');
|
||||||
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
|
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
|
||||||
|
|
||||||
// Test that at least 15 ticks have been recorded for both parent and child
|
// Test that at least 15 ticks have been recorded for both parent and child
|
||||||
|
Loading…
x
Reference in New Issue
Block a user