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