mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
test: split test-embedding.js and run tests in parallel
Split the tests so that it's easier to isolate and debug issues, and we can run the test cases in parallel to speed up. Also add more comments about what they are testing. PR-URL: https://github.com/nodejs/node/pull/61571 Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
1 parent
99cfb34cee
commit
a879b39778
16 files changed
+374
-169
No files matched your search
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
// Tests that createRequire() works correctly with local fs in embedded environments.
|
||||
// The createRequire() call is in embedtest.cc.
|
||||
|
||||
const common = require('../common');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const { spawnSyncAndExit } = require('../common/child_process');
|
||||
|
||||
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
|
||||
spawnSyncAndExit(
|
||||
common.resolveBuiltBinary('embedtest'),
|
||||
[`require(${fixturePath})`, 92],
|
||||
{
|
||||
status: 92,
|
||||
signal: null,
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
// Tests that createRequire() from embedded environments still cannot access internals.
|
||||
// The createRequire() call is in embedtest.cc.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndExit } = require('../common/child_process');
|
||||
|
||||
spawnSyncAndExit(
|
||||
common.resolveBuiltBinary('embedtest'),
|
||||
['require("lib/internal/test/binding")'],
|
||||
{
|
||||
status: 1,
|
||||
signal: null,
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
// Tests node::ProcessInitializationFlags::kDisableNodeOptionsEnv works correctly.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndExit } = require('../common/child_process');
|
||||
const os = require('os');
|
||||
|
||||
if (process.config.variables.node_without_node_options) {
|
||||
common.skip('NODE_REPL_EXTERNAL_MODULE cannot be tested with --without-node-options');
|
||||
}
|
||||
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
spawnSyncAndExit(
|
||||
embedtest,
|
||||
['require("os")'],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
'NODE_REPL_EXTERNAL_MODULE': 'fs',
|
||||
},
|
||||
},
|
||||
{
|
||||
status: 9,
|
||||
signal: null,
|
||||
stderr: `${embedtest}: NODE_REPL_EXTERNAL_MODULE can't be used with kDisableNodeOptionsEnv${os.EOL}`,
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
// Tests that process.exitCode works correctly in embedded environments.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndExit } = require('../common/child_process');
|
||||
|
||||
spawnSyncAndExit(
|
||||
common.resolveBuiltBinary('embedtest'),
|
||||
['process.exitCode = 8'],
|
||||
{
|
||||
status: 8,
|
||||
signal: null,
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
// Tests node::LoadEnvironment() with main_script_source_utf8 works.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndAssert } = require('../common/child_process');
|
||||
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['console.log(42)'],
|
||||
{
|
||||
trim: true,
|
||||
stdout: '42',
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
// Test node::LoadEnvironment() works with non-ASCII source code. The variable
|
||||
// comes from embedtest.cc.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndAssert } = require('../common/child_process');
|
||||
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['console.log(embedVars.nön_ascıı)'],
|
||||
{
|
||||
trim: true,
|
||||
stdout: '🏳️🌈',
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
// Tests node::EmbedderSnapshotData::FromFile() works correctly.
|
||||
// The snapshot is created in embedtest.cc.
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
|
||||
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(require("fs").readFileSync(${JSON.stringify(snapshotFixture)}, "utf8"))`,
|
||||
'arg1', 'arg2',
|
||||
];
|
||||
const snapshotBlobArgs = [
|
||||
'--embedder-snapshot-blob', tmpdir.resolve('embedder-snapshot.blob'),
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = ['arg3', 'arg4'];
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// Build the snapshot with the --embedder-snapshot-as-file flag.
|
||||
spawnSyncAndExitWithoutError(
|
||||
embedtest,
|
||||
['--', ...buildSnapshotExecArgs, ...snapshotBlobArgs, '--embedder-snapshot-as-file', '--embedder-snapshot-create'],
|
||||
{ cwd: tmpdir.path });
|
||||
|
||||
// Run the snapshot and check the serialized and refreshed argv values.
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['--', ...runSnapshotExecArgs, ...snapshotBlobArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout(output) {
|
||||
assert.deepStrictEqual(JSON.parse(output), {
|
||||
originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs],
|
||||
currentArgv: [embedtest, ...runSnapshotExecArgs],
|
||||
});
|
||||
return true;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
// Tests snapshot support in embedded environments.
|
||||
// See embedtest.cc for the API usage.
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
|
||||
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(require("fs").readFileSync(${JSON.stringify(snapshotFixture)}, "utf8"))`,
|
||||
'arg1', 'arg2',
|
||||
];
|
||||
const snapshotBlobArgs = [
|
||||
'--embedder-snapshot-blob', tmpdir.resolve('embedder-snapshot.blob'),
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = ['arg3', 'arg4'];
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// Build the snapshot with the default flags.
|
||||
spawnSyncAndExitWithoutError(
|
||||
embedtest,
|
||||
['--', ...buildSnapshotExecArgs, ...snapshotBlobArgs, '--embedder-snapshot-create'],
|
||||
{ cwd: tmpdir.path });
|
||||
|
||||
// Run the snapshot and check the serialized and refreshed argv values.
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['--', ...runSnapshotExecArgs, ...snapshotBlobArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout(output) {
|
||||
assert.deepStrictEqual(JSON.parse(output), {
|
||||
originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs],
|
||||
currentArgv: [embedtest, ...runSnapshotExecArgs],
|
||||
});
|
||||
return true;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
// Tests creating vm contexts after snapshot deserialization works
|
||||
// in embedded environments.
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
|
||||
const snapshotFixture = fixtures.path('snapshot', 'create-vm.js');
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(require("fs").readFileSync(${JSON.stringify(snapshotFixture)}, "utf8"))`,
|
||||
'arg1', 'arg2',
|
||||
];
|
||||
const snapshotBlobArgs = [
|
||||
'--embedder-snapshot-blob', tmpdir.resolve('embedder-snapshot.blob'),
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = ['arg3', 'arg4'];
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// Build the snapshot with the vm-creating code in serialized main funciton.
|
||||
spawnSyncAndExitWithoutError(
|
||||
embedtest,
|
||||
['--', ...buildSnapshotExecArgs, ...snapshotBlobArgs, '--embedder-snapshot-create'],
|
||||
{ cwd: tmpdir.path });
|
||||
|
||||
// Run the snapshot that creates a vm context from the snapshot.
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['--', ...runSnapshotExecArgs, ...snapshotBlobArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout: /value: 42/,
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
// Tests node::SnapshotFlags::kWithoutCodeCache. See
|
||||
// embedtest.cc for more details.
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
|
||||
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(require("fs").readFileSync(${JSON.stringify(snapshotFixture)}, "utf8"))`,
|
||||
'arg1', 'arg2',
|
||||
];
|
||||
const snapshotBlobArgs = [
|
||||
'--embedder-snapshot-blob', tmpdir.resolve('embedder-snapshot.blob'),
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = ['arg3', 'arg4'];
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// Build the snapshot with the --without-code-cache flag.
|
||||
spawnSyncAndExitWithoutError(
|
||||
embedtest,
|
||||
['--', ...buildSnapshotExecArgs, ...snapshotBlobArgs, '--without-code-cache', '--embedder-snapshot-create'],
|
||||
{ cwd: tmpdir.path });
|
||||
|
||||
// Run the snapshot and check the serialized and refreshed argv values.
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['--', ...runSnapshotExecArgs, ...snapshotBlobArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout(output) {
|
||||
assert.deepStrictEqual(JSON.parse(output), {
|
||||
originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs],
|
||||
currentArgv: [embedtest, ...runSnapshotExecArgs],
|
||||
});
|
||||
return true;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
// Tests creating workers after snapshot deserialization works
|
||||
// in embedded environments.
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
|
||||
const snapshotFixture = fixtures.path('snapshot', 'create-worker.js');
|
||||
const embedtest = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(require("fs").readFileSync(${JSON.stringify(snapshotFixture)}, "utf8"))`,
|
||||
'arg1', 'arg2',
|
||||
];
|
||||
const snapshotBlobArgs = [
|
||||
'--embedder-snapshot-blob', tmpdir.resolve('embedder-snapshot.blob'),
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = ['arg3', 'arg4'];
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// Build the snapshot with the worker-creating code in serialized main funciton.
|
||||
spawnSyncAndExitWithoutError(
|
||||
embedtest,
|
||||
['--', ...buildSnapshotExecArgs, ...snapshotBlobArgs, '--embedder-snapshot-create'],
|
||||
{ cwd: tmpdir.path });
|
||||
|
||||
// Run the snapshot that creates a worker from the snapshot.
|
||||
spawnSyncAndAssert(
|
||||
embedtest,
|
||||
['--', ...runSnapshotExecArgs, ...snapshotBlobArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout: /value: 42/,
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
// Tests that uncaught exceptions are handled correctly in embedded environments.
|
||||
|
||||
const common = require('../common');
|
||||
const { spawnSyncAndExit } = require('../common/child_process');
|
||||
|
||||
spawnSyncAndExit(
|
||||
common.resolveBuiltBinary('embedtest'),
|
||||
['throw new Error()'],
|
||||
{
|
||||
status: 1,
|
||||
signal: null,
|
||||
});
|
||||
@@ -1,164 +0,0 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const assert = require('assert');
|
||||
const {
|
||||
spawnSyncAndAssert,
|
||||
spawnSyncAndExit,
|
||||
spawnSyncAndExitWithoutError,
|
||||
} = require('../common/child_process');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
|
||||
tmpdir.refresh();
|
||||
common.allowGlobals(global.require);
|
||||
common.allowGlobals(global.embedVars);
|
||||
|
||||
const binary = common.resolveBuiltBinary('embedtest');
|
||||
|
||||
spawnSyncAndAssert(
|
||||
binary,
|
||||
['console.log(42)'],
|
||||
{
|
||||
trim: true,
|
||||
stdout: '42',
|
||||
});
|
||||
|
||||
spawnSyncAndAssert(
|
||||
binary,
|
||||
['console.log(embedVars.nön_ascıı)'],
|
||||
{
|
||||
trim: true,
|
||||
stdout: '🏳️🌈',
|
||||
});
|
||||
|
||||
spawnSyncAndExit(
|
||||
binary,
|
||||
['throw new Error()'],
|
||||
{
|
||||
status: 1,
|
||||
signal: null,
|
||||
});
|
||||
|
||||
spawnSyncAndExit(
|
||||
binary,
|
||||
['require("lib/internal/test/binding")'],
|
||||
{
|
||||
status: 1,
|
||||
signal: null,
|
||||
});
|
||||
|
||||
spawnSyncAndExit(
|
||||
binary,
|
||||
['process.exitCode = 8'],
|
||||
{
|
||||
status: 8,
|
||||
signal: null,
|
||||
});
|
||||
|
||||
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
|
||||
spawnSyncAndExit(
|
||||
binary,
|
||||
[`require(${fixturePath})`, 92],
|
||||
{
|
||||
status: 92,
|
||||
signal: null,
|
||||
});
|
||||
|
||||
function getReadFileCodeForPath(path) {
|
||||
return `(require("fs").readFileSync(${JSON.stringify(path)}, "utf8"))`;
|
||||
}
|
||||
|
||||
// Basic snapshot support
|
||||
for (const extraSnapshotArgs of [
|
||||
[], ['--embedder-snapshot-as-file'], ['--without-code-cache'],
|
||||
]) {
|
||||
// readSync + eval since snapshots don't support userland require() (yet)
|
||||
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
||||
const blobPath = tmpdir.resolve('embedder-snapshot.blob');
|
||||
const buildSnapshotExecArgs = [
|
||||
`eval(${getReadFileCodeForPath(snapshotFixture)})`, 'arg1', 'arg2',
|
||||
];
|
||||
const embedTestBuildArgs = [
|
||||
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
||||
...extraSnapshotArgs,
|
||||
];
|
||||
const buildSnapshotArgs = [
|
||||
...buildSnapshotExecArgs,
|
||||
...embedTestBuildArgs,
|
||||
];
|
||||
|
||||
const runSnapshotExecArgs = [
|
||||
'arg3', 'arg4',
|
||||
];
|
||||
const embedTestRunArgs = [
|
||||
'--embedder-snapshot-blob', blobPath,
|
||||
...extraSnapshotArgs,
|
||||
];
|
||||
const runSnapshotArgs = [
|
||||
...runSnapshotExecArgs,
|
||||
...embedTestRunArgs,
|
||||
];
|
||||
|
||||
fs.rmSync(blobPath, { force: true });
|
||||
spawnSyncAndExitWithoutError(
|
||||
binary,
|
||||
[ '--', ...buildSnapshotArgs ],
|
||||
{ cwd: tmpdir.path });
|
||||
spawnSyncAndAssert(
|
||||
binary,
|
||||
[ '--', ...runSnapshotArgs ],
|
||||
{ cwd: tmpdir.path },
|
||||
{
|
||||
stdout(output) {
|
||||
assert.deepStrictEqual(JSON.parse(output), {
|
||||
originalArgv: [binary, '__node_anonymous_main', ...buildSnapshotExecArgs],
|
||||
currentArgv: [binary, ...runSnapshotExecArgs],
|
||||
});
|
||||
return true;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Create workers and vm contexts after deserialization
|
||||
{
|
||||
const snapshotFixture = fixtures.path('snapshot', 'create-worker-and-vm.js');
|
||||
const blobPath = tmpdir.resolve('embedder-snapshot.blob');
|
||||
const buildSnapshotArgs = [
|
||||
`eval(${getReadFileCodeForPath(snapshotFixture)})`,
|
||||
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
||||
];
|
||||
const runEmbeddedArgs = [
|
||||
'--embedder-snapshot-blob', blobPath,
|
||||
];
|
||||
|
||||
fs.rmSync(blobPath, { force: true });
|
||||
|
||||
spawnSyncAndExitWithoutError(
|
||||
binary,
|
||||
[ '--', ...buildSnapshotArgs ],
|
||||
{ cwd: tmpdir.path });
|
||||
spawnSyncAndExitWithoutError(
|
||||
binary,
|
||||
[ '--', ...runEmbeddedArgs ],
|
||||
{ cwd: tmpdir.path });
|
||||
}
|
||||
|
||||
// Guarantee NODE_REPL_EXTERNAL_MODULE won't bypass kDisableNodeOptionsEnv
|
||||
if (!process.config.variables.node_without_node_options) {
|
||||
spawnSyncAndExit(
|
||||
binary,
|
||||
['require("os")'],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
'NODE_REPL_EXTERNAL_MODULE': 'fs',
|
||||
},
|
||||
},
|
||||
{
|
||||
status: 9,
|
||||
signal: null,
|
||||
stderr: `${binary}: NODE_REPL_EXTERNAL_MODULE can't be used with kDisableNodeOptionsEnv${os.EOL}`,
|
||||
});
|
||||
}
|
||||
@@ -3,4 +3,4 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
||||
import testpy
|
||||
|
||||
def GetConfiguration(context, root):
|
||||
return testpy.SimpleTestConfiguration(context, root, 'embedding')
|
||||
return testpy.ParallelTestConfiguration(context, root, 'embedding')
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
const {
|
||||
setDeserializeMainFunction,
|
||||
} = require('v8').startupSnapshot;
|
||||
const assert = require('assert');
|
||||
|
||||
setDeserializeMainFunction(() => {
|
||||
const vm = require('vm');
|
||||
const result = vm.runInNewContext('21+21');
|
||||
console.log(`value: ${result}`);
|
||||
assert.strictEqual(result, 42);
|
||||
});
|
||||
+6
-4
@@ -4,17 +4,19 @@ const {
|
||||
const assert = require('assert');
|
||||
|
||||
setDeserializeMainFunction(() => {
|
||||
const vm = require('vm');
|
||||
const { Worker } = require('worker_threads');
|
||||
assert.strictEqual(vm.runInNewContext('21+21'), 42);
|
||||
const worker = new Worker(
|
||||
'require("worker_threads").parentPort.postMessage({value: 21 + 21})',
|
||||
{ eval: true });
|
||||
|
||||
const messages = [];
|
||||
worker.on('message', message => messages.push(message));
|
||||
worker.on('message', (message) => {
|
||||
console.log('Worker message:', message);
|
||||
messages.push(message);
|
||||
});
|
||||
|
||||
process.on('beforeExit', () => {
|
||||
console.log('Process beforeExit, messages:', messages);
|
||||
assert.deepStrictEqual(messages, [{value:42}]);
|
||||
})
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user