|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This tests that user land snapshots works when the instance restored from |
| 4 | +// the snapshot is launched as a REPL. |
| 5 | + |
| 6 | +const common = require('../common'); |
| 7 | +if (common.isWindows) { |
| 8 | + // No way to send CTRL_C_EVENT to processes from JS right now. |
| 9 | + common.skip('platform not supported'); |
| 10 | +} |
| 11 | +const assert = require('assert'); |
| 12 | +const { spawnSync, spawn } = require('child_process'); |
| 13 | +const tmpdir = require('../common/tmpdir'); |
| 14 | +const fixtures = require('../common/fixtures'); |
| 15 | +const path = require('path'); |
| 16 | + |
| 17 | +tmpdir.refresh(); |
| 18 | +const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); |
| 19 | +const file = fixtures.path('snapshot', 'mutate-fs.js'); |
| 20 | + |
| 21 | +{ |
| 22 | + // Create the snapshot. |
| 23 | + const child = spawnSync(process.execPath, [ |
| 24 | + '--snapshot-main', |
| 25 | + file, |
| 26 | + '--snapshot-blob', |
| 27 | + blobPath, |
| 28 | + ], { |
| 29 | + cwd: tmpdir.path |
| 30 | + }); |
| 31 | + if (child.status !== 0) { |
| 32 | + console.log(child.stderr.toString()); |
| 33 | + console.log(child.stdout.toString()); |
| 34 | + assert.strictEqual(child.status, 0); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +{ |
| 39 | + // Use the snapshot to run the REPL. |
| 40 | + const child = spawn(process.execPath, [ |
| 41 | + '--snapshot-blob', |
| 42 | + blobPath, |
| 43 | + '-i', |
| 44 | + ], { |
| 45 | + cwd: tmpdir.path, |
| 46 | + env: { |
| 47 | + ...process.env, |
| 48 | + REPL_TEST_PPID: process.pid |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + let stdout = ''; |
| 53 | + child.stdout.setEncoding('utf8'); |
| 54 | + child.stdout.on('data', (data) => { |
| 55 | + stdout += data; |
| 56 | + }); |
| 57 | + |
| 58 | + let stderr = ''; |
| 59 | + child.stderr.setEncoding('utf8'); |
| 60 | + child.stderr.on('data', (data) => { |
| 61 | + stderr += data; |
| 62 | + }); |
| 63 | + |
| 64 | + child.stdout.once('data', () => { |
| 65 | + process.on('SIGUSR2', () => { |
| 66 | + process.kill(child.pid, 'SIGINT'); |
| 67 | + child.stdout.once('data', () => { |
| 68 | + // Make sure state from before the interruption is still available. |
| 69 | + child.stdin.end('fs.foo\n'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + child.stdin.write('process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' + |
| 74 | + 'while(true){}\n'); |
| 75 | + }); |
| 76 | + |
| 77 | + child.on('close', common.mustCall(function(code) { |
| 78 | + if (code !== 0) { |
| 79 | + console.log(stderr); |
| 80 | + console.log(stdout); |
| 81 | + assert.strictEqual(code, 0); |
| 82 | + } |
| 83 | + // Check that fs.foo is mutated from the snapshot |
| 84 | + assert(/I am from the snapshot/.test(stdout)); |
| 85 | + })); |
| 86 | + |
| 87 | + child.stdin.end("require('fs').foo"); |
| 88 | +} |
0 commit comments