Skip to content

Commit e1cf634

Browse files
committed
test: spawn new processes in vm-cached-data
V8 may start caching scripts from the first run soon. Preventively execute scripts in another process to ensure no test failures due to an update in the future. See: #6258 PR-URL: #6280 Reviewed-By: Jochen Eisinger <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5f0fcd6 commit e1cf634

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

test/parallel/test-vm-cached-data.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,38 @@
22
require('../common');
33
const assert = require('assert');
44
const vm = require('vm');
5+
const spawnSync = require('child_process').spawnSync;
56
const Buffer = require('buffer').Buffer;
67

78
function getSource(tag) {
89
return `(function ${tag}() { return \'${tag}\'; })`;
910
}
1011

11-
function produce(source) {
12-
const script = new vm.Script(source, {
13-
produceCachedData: true
14-
});
15-
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
12+
function produce(source, count) {
13+
if (!count)
14+
count = 1;
15+
16+
const out = spawnSync(process.execPath, [ '-e', `
17+
var assert = require('assert');
18+
var vm = require('vm');
19+
20+
let data;
21+
for (var i = 0; i < ${count}; i++) {
22+
var script = new vm.Script(process.argv[1], {
23+
produceCachedData: true
24+
});
25+
26+
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
27+
28+
if (script.cachedDataProduced)
29+
data = script.cachedData.toString('base64');
30+
}
31+
console.log(data);
32+
`, source]);
33+
34+
assert.equal(out.status, 0, out.stderr + '');
1635

17-
return script.cachedData;
36+
return Buffer.from(out.stdout.toString(), 'base64');
1837
}
1938

2039
function testProduceConsume() {
@@ -34,9 +53,7 @@ testProduceConsume();
3453
function testProduceMultiple() {
3554
const source = getSource('original');
3655

37-
produce(source);
38-
produce(source);
39-
produce(source);
56+
produce(source, 3);
4057
}
4158
testProduceMultiple();
4259

0 commit comments

Comments
 (0)