summaryrefslogtreecommitdiff
path: root/test/parallel/test-code-cache.js
blob: 1c768d664c8a1866d0e50d310e016aaec789da1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Flags: --expose-internals
'use strict';

// This test verifies that if the binary is compiled with code cache,
// and the cache is used when built in modules are compiled.
// Otherwise, verifies that no cache is used when compiling builtins.

const { isMainThread } = require('../common');
const assert = require('assert');
const {
  internalBinding
} = require('internal/test/binding');
const {
  getCacheUsage,
  builtinCategories: { canBeRequired }
} = internalBinding('builtins');

for (const key of canBeRequired) {
  require(`node:${key}`);
}

// The computation has to be delayed until we have done loading modules
const {
  compiledWithoutCache,
  compiledWithCache,
  compiledInSnapshot
} = getCacheUsage();

function extractModules(list) {
  return list.filter((m) => m.startsWith('NativeModule'))
  .map((m) => m.replace('NativeModule ', ''));
}

const loadedModules = extractModules(process.moduleLoadList);

// Cross-compiled binaries do not have code cache, verifies that the builtins
// are all compiled without cache and we are doing the bookkeeping right.
if (!process.features.cached_builtins) {
  assert(!process.config.variables.node_use_node_code_cache ||
         process.execArgv.includes('--no-node-snapshot'));

  if (isMainThread) {
    assert.deepStrictEqual(compiledWithCache, new Set());
    for (const key of loadedModules) {
      assert(compiledWithoutCache.has(key),
             `"${key}" should've been compiled without code cache`);
    }
  } else {
    // TODO(joyeecheung): create a list of modules whose cache can be shared
    // from the main thread to the worker thread and check that their
    // cache are hit
    assert.notDeepStrictEqual(compiledWithCache, new Set());
  }
} else {  // Native compiled
  assert(process.config.variables.node_use_node_code_cache);

  const wrong = [];
  for (const key of loadedModules) {
    if (key.startsWith('internal/deps/v8/tools')) {
      continue;
    }
    if (!compiledWithCache.has(key) &&
      compiledInSnapshot.indexOf(key) === -1) {
      wrong.push(`"${key}" should've been compiled **with** code cache`);
    }
  }
  assert.strictEqual(wrong.length, 0, wrong.join('\n'));
}