File: NativeCompileCache-test.js

package info (click to toggle)
node-v8-compile-cache 2.3.0-3%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 268 kB
  • sloc: javascript: 880; sh: 11; makefile: 2
file content (125 lines) | stat: -rw-r--r-- 3,995 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';

const Module = require('module');
const fs = require('fs');
const path = require('path');
const tap = require('tap');
const temp = require('temp');

temp.track();

const FileSystemBlobStore_mock = require('./FileSystemBlobStore-mock');

process.env.DISABLE_V8_COMPILE_CACHE = 1;
const NativeCompileCache = require('..').__TEST__.NativeCompileCache;

let cachedFiles;
let fakeCacheStore;
let nativeCompileCache;

tap.beforeEach(() => {
  fakeCacheStore = new FileSystemBlobStore_mock();
  cachedFiles = fakeCacheStore._cachedFiles;
  nativeCompileCache = new NativeCompileCache();
  nativeCompileCache.setCacheStore(fakeCacheStore);
  nativeCompileCache.install();
});

tap.afterEach(() => {
  nativeCompileCache.uninstall();
});

tap.test('writes and reads from the cache storage when requiring files', t => {
  let fn1 = require('./fixtures/file-1');
  const fn2 = require('./fixtures/file-2');

  t.equal(cachedFiles.length, 2);

  t.equal(cachedFiles[0].key, require.resolve('./fixtures/file-1'));
  t.type(cachedFiles[0].buffer, Uint8Array);
  t.ok(cachedFiles[0].buffer.length > 0);
  t.equal(fn1(), 1);

  t.equal(cachedFiles[1].key, require.resolve('./fixtures/file-2'));
  t.type(cachedFiles[1].buffer, Uint8Array);
  t.ok(cachedFiles[1].buffer.length > 0);
  t.equal(fn2(), 2);

  delete Module._cache[require.resolve('./fixtures/file-1')];
  fn1 = require('./fixtures/file-1');
  t.equal(cachedFiles.length, 2);
  t.equal(fn1(), 1);

  t.end();
});

tap.test('when the cache changes it updates the new cache', t => {
  let fn4 = require('./fixtures/file-4');

  t.equal(cachedFiles.length, 1);
  t.equal(cachedFiles[0].key, require.resolve('./fixtures/file-4'));
  t.type(cachedFiles[0].buffer, Uint8Array);
  t.ok(cachedFiles[0].buffer.length > 0);
  t.equal(fn4(), 'file-4');

  const fakeCacheStore2 = new FileSystemBlobStore_mock();
  const cachedFiles2 = fakeCacheStore._cachedFiles;
  nativeCompileCache.setCacheStore(fakeCacheStore2);

  delete Module._cache[require.resolve('./fixtures/file-4')];
  fn4 = require('./fixtures/file-4');

  t.equal(cachedFiles.length, 1);
  t.equal(cachedFiles2.length, 1);
  t.equal(cachedFiles[0].key, require.resolve('./fixtures/file-4'));
  t.equal(cachedFiles2[0].key, require.resolve('./fixtures/file-4'));
  t.equal(cachedFiles[0].invalidationKey, cachedFiles2[0].invalidationKey);
  t.type(cachedFiles[0].buffer, Uint8Array);
  t.type(cachedFiles2[0].buffer, Uint8Array);
  t.ok(cachedFiles[0].buffer.length > 0);
  t.ok(cachedFiles2[0].buffer.length > 0);

  t.end();
});

tap.test('deletes previously cached code when the cache is an invalid file', t => {
  fakeCacheStore.has = () => true;
  fakeCacheStore.get = () => Buffer.from('an invalid cache');
  let wasCalledWith = null;
  fakeCacheStore.set = arg => { wasCalledWith = arg; };
  // older v8 still calls delete, though
  fakeCacheStore.delete = arg => { wasCalledWith = arg; };

  const fn3 = require('./fixtures/file-3');

  t.equal(wasCalledWith, require.resolve('./fixtures/file-3'));
  t.equal(fn3(), 3);

  t.end();
});

tap.test('when a previously required and cached file changes removes it from the store and re-inserts it with the new cache', t => {
  const tmpDir = temp.mkdirSync('native-compile-cache-test');
  const tmpFile = path.join(tmpDir, 'file-5.js');
  fs.writeFileSync(tmpFile, 'module.exports = () => `file-5`;');

  let fn5 = require(tmpFile);

  t.equal(cachedFiles.length, 1);
  t.equal(cachedFiles[0].key, require.resolve(tmpFile));
  t.type(cachedFiles[0].buffer, Uint8Array);
  t.ok(cachedFiles[0].buffer.length > 0);
  t.equal(fn5(), 'file-5');

  delete Module._cache[require.resolve(tmpFile)];
  fs.appendFileSync(tmpFile, '\n\n');
  fn5 = require(tmpFile);

  t.equal(cachedFiles.length, 2);
  t.equal(cachedFiles[1].key, require.resolve(tmpFile));
  t.notEqual(cachedFiles[1].invalidationKey, cachedFiles[0].invalidationKey);
  t.type(cachedFiles[1].buffer, Uint8Array);
  t.ok(cachedFiles[1].buffer.length > 0);

  t.end();
});