Skip to content

Commit ae0ca0a

Browse files
joyeecheungmartenrichter
authored andcommitted
test: improve test-bootstrap-modules.js
Divide builtins into two lists depending on whether they are loaded before pre-execution or at run time, and give clearer suggestions about how to deal with them based on the category they are in. This helps preventing regressions like the one reported in nodejs#45662. PR-URL: nodejs#50708 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent b881c24 commit ae0ca0a

File tree

1 file changed

+103
-25
lines changed

1 file changed

+103
-25
lines changed

test/parallel/test-bootstrap-modules.js

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
// Flags: --expose-internals
21
'use strict';
32

4-
// This list must be computed before we require any modules to
3+
// This list must be computed before we require any builtins to
54
// to eliminate the noise.
6-
const actualModules = new Set(process.moduleLoadList.slice());
5+
const list = process.moduleLoadList.slice();
76

87
const common = require('../common');
98
const assert = require('assert');
9+
const { inspect } = require('util');
1010

11-
const expectedModules = new Set([
11+
const preExecIndex =
12+
list.findIndex((i) => i.includes('pre_execution'));
13+
const actual = {
14+
beforePreExec: new Set(list.slice(0, preExecIndex)),
15+
atRunTime: new Set(list.slice(preExecIndex)),
16+
};
17+
18+
// Currently, we don't add additional builtins to worker snapshots.
19+
// So for worker snapshots we'll just concatenate the two. Once we
20+
// add more builtins to worker snapshots, we should also distinguish
21+
// the two stages for them.
22+
const expected = {};
23+
24+
expected.beforePreExec = new Set([
1225
'Internal Binding builtins',
1326
'Internal Binding encoding_binding',
1427
'Internal Binding modules',
@@ -85,22 +98,25 @@ const expectedModules = new Set([
8598
'NativeModule internal/modules/package_json_reader',
8699
'Internal Binding module_wrap',
87100
'NativeModule internal/modules/cjs/loader',
88-
'NativeModule internal/vm/module',
89-
'NativeModule internal/modules/esm/utils',
101+
]);
102+
103+
expected.atRunTime = new Set([
90104
'Internal Binding wasm_web_api',
91105
'Internal Binding worker',
92106
'NativeModule internal/modules/run_main',
93107
'NativeModule internal/net',
94108
'NativeModule internal/dns/utils',
95109
'NativeModule internal/process/pre_execution',
110+
'NativeModule internal/vm/module',
111+
'NativeModule internal/modules/esm/utils',
96112
]);
97113

98114
if (common.isMainThread) {
99115
[
100116
'NativeModule internal/idna',
101117
'NativeModule url',
102-
].forEach(expectedModules.add.bind(expectedModules));
103-
} else {
118+
].forEach(expected.beforePreExec.add.bind(expected.beforePreExec));
119+
} else { // Worker.
104120
[
105121
'NativeModule diagnostics_channel',
106122
'NativeModule internal/abort_controller',
@@ -128,35 +144,97 @@ if (common.isMainThread) {
128144
'NativeModule stream/promises',
129145
'NativeModule string_decoder',
130146
'NativeModule worker_threads',
131-
].forEach(expectedModules.add.bind(expectedModules));
147+
].forEach(expected.atRunTime.add.bind(expected.atRunTime));
148+
// For now we'll concatenate the two stages for workers. We prefer
149+
// atRunTime here because that's what currently happens for these.
132150
}
133151

134152
if (common.isWindows) {
135153
// On Windows fs needs SideEffectFreeRegExpPrototypeExec which uses vm.
136-
expectedModules.add('NativeModule vm');
154+
expected.atRunTime.add('NativeModule vm');
137155
}
138156

139157
if (common.hasIntl) {
140-
expectedModules.add('Internal Binding icu');
158+
expected.beforePreExec.add('Internal Binding icu');
141159
}
142160

143161
if (process.features.inspector) {
144-
expectedModules.add('Internal Binding inspector');
145-
expectedModules.add('NativeModule internal/inspector_async_hook');
146-
expectedModules.add('NativeModule internal/util/inspector');
162+
expected.beforePreExec.add('Internal Binding inspector');
163+
expected.beforePreExec.add('NativeModule internal/util/inspector');
164+
expected.atRunTime.add('NativeModule internal/inspector_async_hook');
147165
}
148166

149167
const difference = (setA, setB) => {
150168
return new Set([...setA].filter((x) => !setB.has(x)));
151169
};
152-
const missingModules = difference(expectedModules, actualModules);
153-
const extraModules = difference(actualModules, expectedModules);
154-
const printSet = (s) => { return `${[...s].sort().join(',\n ')}\n`; };
155-
156-
assert.deepStrictEqual(actualModules, expectedModules,
157-
(missingModules.size > 0 ?
158-
'These modules were not loaded:\n ' +
159-
printSet(missingModules) : '') +
160-
(extraModules.size > 0 ?
161-
'These modules were unexpectedly loaded:\n ' +
162-
printSet(extraModules) : ''));
170+
171+
// Accumulate all the errors and print them at the end instead of throwing
172+
// immediately which makes it harder to update the test.
173+
const errorLogs = [];
174+
function err(message) {
175+
if (typeof message === 'string') {
176+
errorLogs.push(message);
177+
} else {
178+
// Show the items in individual lines for easier copy-pasting.
179+
errorLogs.push(inspect(message, { compact: false }));
180+
}
181+
}
182+
183+
if (common.isMainThread) {
184+
const missing = difference(expected.beforePreExec, actual.beforePreExec);
185+
const extra = difference(actual.beforePreExec, expected.beforePreExec);
186+
if (missing.size !== 0) {
187+
err('These builtins are now no longer loaded before pre-execution.');
188+
err('If this is intentional, remove them from `expected.beforePreExec`.');
189+
err('\n--- These could be removed from expected.beforePreExec ---');
190+
err([...missing].sort());
191+
err('');
192+
}
193+
if (extra.size !== 0) {
194+
err('These builtins are now unexpectedly loaded before pre-execution.');
195+
err('If this is intentional, add them to `expected.beforePreExec`.');
196+
err('\n# Note: loading more builtins before pre-execution can lead to ' +
197+
'startup performance regression or invalid snapshots.');
198+
err('- Consider lazy loading builtins that are not used universally.');
199+
err('- Make sure that the builtins do not access environment dependent ' +
200+
'states e.g. command line arguments or environment variables ' +
201+
'during loading.');
202+
err('- When in doubt, ask @nodejs/startup.');
203+
err('\n--- These could be added to expected.beforePreExec ---');
204+
err([...extra].sort());
205+
err('');
206+
}
207+
}
208+
209+
if (!common.isMainThread) {
210+
// For workers, just merge beforePreExec into atRunTime for now.
211+
// When we start adding modules to the worker snapshot, this branch
212+
// can be removed and we can just remove the common.isMainThread
213+
// conditions.
214+
expected.beforePreExec.forEach(expected.atRunTime.add.bind(expected.atRunTime));
215+
actual.beforePreExec.forEach(actual.atRunTime.add.bind(actual.atRunTime));
216+
}
217+
218+
{
219+
const missing = difference(expected.atRunTime, actual.atRunTime);
220+
const extra = difference(actual.atRunTime, expected.atRunTime);
221+
if (missing.size !== 0) {
222+
err('These builtins are now no longer loaded at run time.');
223+
err('If this is intentional, remove them from `expected.atRunTime`.');
224+
err('\n--- These could be removed from expected.atRunTime ---');
225+
err([...missing].sort());
226+
err('');
227+
}
228+
if (extra.size !== 0) {
229+
err('These builtins are now unexpectedly loaded at run time.');
230+
err('If this is intentional, add them to `expected.atRunTime`.');
231+
err('\n# Note: loading more builtins at run time can lead to ' +
232+
'startup performance regression.');
233+
err('- Consider lazy loading builtins that are not used universally.');
234+
err('\n--- These could be added to expected.atRunTime ---');
235+
err([...extra].sort());
236+
err('');
237+
}
238+
}
239+
240+
assert.strictEqual(errorLogs.length, 0, errorLogs.join('\n'));

0 commit comments

Comments
 (0)