1'use strict'; 2const common = require('../common'); 3common.skipIfInspectorDisabled(); 4 5const { spawnSync } = require('child_process'); 6const { createServer } = require('http'); 7const assert = require('assert'); 8const tmpdir = require('../common/tmpdir'); 9const fixtures = require('../common/fixtures'); 10const entry = fixtures.path('empty.js'); 11const { Worker } = require('worker_threads'); 12 13function testOnServerListen(fn) { 14 const server = createServer((socket) => { 15 socket.end('echo'); 16 }); 17 18 server.on('listening', () => { 19 fn(server); 20 server.close(); 21 }); 22 server.listen(0, '127.0.0.1'); 23} 24 25function testChildProcess(getArgs, exitCode, options) { 26 testOnServerListen((server) => { 27 const { port } = server.address(); 28 const child = spawnSync(process.execPath, getArgs(port), options); 29 const stderr = child.stderr.toString().trim(); 30 const stdout = child.stdout.toString().trim(); 31 console.log('[STDERR]'); 32 console.log(stderr); 33 console.log('[STDOUT]'); 34 console.log(stdout); 35 const match = stderr.match( 36 /Starting inspector on 127\.0\.0\.1:(\d+) failed: address already in use/ 37 ); 38 assert.notStrictEqual(match, null); 39 assert.strictEqual(match[1], port + ''); 40 assert.strictEqual(child.status, exitCode); 41 }); 42} 43 44tmpdir.refresh(); 45 46testChildProcess( 47 (port) => [`--inspect=${port}`, '--build-snapshot', entry], 0, 48 { cwd: tmpdir.path }); 49 50testChildProcess( 51 (port) => [`--inspect=${port}`, entry], 0); 52 53testOnServerListen((server) => { 54 const { port } = server.address(); 55 const worker = new Worker(entry, { 56 execArgv: [`--inspect=${port}`] 57 }); 58 59 worker.on('error', common.mustNotCall()); 60 61 worker.on('exit', common.mustCall((code) => { 62 assert.strictEqual(code, 0); 63 })); 64}); 65