Skip to content

Commit 7a2134c

Browse files
joyeecheungtargos
authored andcommitted
test: run code cache test by default and test generator
- Add the code cache tests to the default test suite, and test the bookkeeping when the binary is not built with the code cache. - Test the code cache generator to make sure we do not accidentally break it - until we enable code cache in the CI. Refs: #21563 PR-URL: #23855 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5ff1e67 commit 7a2134c

File tree

3 files changed

+88
-17
lines changed

3 files changed

+88
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
// This test verifies that the binary is compiled with code cache and the
4+
// cache is used when built in modules are compiled.
5+
6+
const common = require('../common');
7+
8+
const tmpdir = require('../common/tmpdir');
9+
const { spawnSync } = require('child_process');
10+
const assert = require('assert');
11+
const path = require('path');
12+
const fs = require('fs');
13+
const readline = require('readline');
14+
15+
const generator = path.join(
16+
__dirname, '..', '..', 'tools', 'generate_code_cache.js'
17+
);
18+
tmpdir.refresh();
19+
const dest = path.join(tmpdir.path, 'cache.cc');
20+
21+
// Run tools/generate_code_cache.js
22+
const child = spawnSync(
23+
process.execPath,
24+
['--expose-internals', generator, dest]
25+
);
26+
assert.ifError(child.error);
27+
if (child.status !== 0) {
28+
console.log(child.stderr.toString());
29+
assert.strictEqual(child.status, 0);
30+
}
31+
32+
// Verifies that:
33+
// - node::DefineCodeCache()
34+
// - node::DefineCodeCacheHash()
35+
// are defined in the generated code.
36+
// See src/node_code_cache_stub.cc for explanations.
37+
38+
const rl = readline.createInterface({
39+
input: fs.createReadStream(dest),
40+
crlfDelay: Infinity
41+
});
42+
43+
let hasCacheDef = false;
44+
let hasHashDef = false;
45+
46+
rl.on('line', common.mustCallAtLeast((line) => {
47+
if (line.includes('DefineCodeCache(')) {
48+
hasCacheDef = true;
49+
}
50+
if (line.includes('DefineCodeCacheHash(')) {
51+
hasHashDef = true;
52+
}
53+
}, 2));
54+
55+
rl.on('close', common.mustCall(() => {
56+
assert.ok(hasCacheDef);
57+
assert.ok(hasHashDef);
58+
}));

test/code-cache/test-code-cache.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
// Flags: --expose-internals
4-
// This test verifies that the binary is compiled with code cache and the
5-
// cache is used when built in modules are compiled.
4+
// This test verifies that if the binary is compiled with code cache,
5+
// and the cache is used when built in modules are compiled.
6+
// Otherwise, verifies that no cache is used when compiling builtins.
67

78
require('../common');
89
const assert = require('assert');
@@ -18,23 +19,36 @@ const {
1819
compiledWithoutCache
1920
} = require('internal/bootstrap/cache');
2021

21-
assert.strictEqual(
22-
typeof process.config.variables.node_code_cache_path,
23-
'string'
24-
);
25-
26-
assert.deepStrictEqual(compiledWithoutCache, []);
27-
2822
const loadedModules = process.moduleLoadList
2923
.filter((m) => m.startsWith('NativeModule'))
3024
.map((m) => m.replace('NativeModule ', ''));
3125

32-
for (const key of loadedModules) {
33-
assert(compiledWithCache.includes(key),
34-
`"${key}" should've been compiled with code cache`);
35-
}
26+
// The binary is not configured with code cache, verifies that the builtins
27+
// are all compiled without cache and we are doing the bookkeeping right.
28+
if (process.config.variables.node_code_cache_path === undefined) {
29+
assert.deepStrictEqual(compiledWithCache, []);
30+
assert.notStrictEqual(compiledWithoutCache.length, 0);
31+
32+
for (const key of loadedModules) {
33+
assert(compiledWithoutCache.includes(key),
34+
`"${key}" should not have been compiled with code cache`);
35+
}
3636

37-
for (const key of cachableBuiltins) {
38-
assert(isUint8Array(codeCache[key]) && codeCache[key].length > 0,
39-
`Code cache for "${key}" should've been generated`);
37+
} else {
38+
// The binary is configured with code cache.
39+
assert.strictEqual(
40+
typeof process.config.variables.node_code_cache_path,
41+
'string'
42+
);
43+
assert.deepStrictEqual(compiledWithoutCache, []);
44+
45+
for (const key of loadedModules) {
46+
assert(compiledWithCache.includes(key),
47+
`"${key}" should've been compiled with code cache`);
48+
}
49+
50+
for (const key of cachableBuiltins) {
51+
assert(isUint8Array(codeCache[key]) && codeCache[key].length > 0,
52+
`Code cache for "${key}" should've been generated`);
53+
}
4054
}

tools/test.py

-1
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,6 @@ def PrintCrashed(code):
14981498
IGNORED_SUITES = [
14991499
'addons',
15001500
'addons-napi',
1501-
'code-cache',
15021501
'doctool',
15031502
'internet',
15041503
'pummel',

0 commit comments

Comments
 (0)