|
| 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 tmpdir = require('../common/tmpdir'); |
| 9 | +const fixtures = require('../common/fixtures'); |
| 10 | +const entry = fixtures.path('empty.js'); |
| 11 | +const { Worker } = require('worker_threads'); |
| 12 | + |
| 13 | +function 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 | + |
| 25 | +function 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 | + |
| 44 | +tmpdir.refresh(); |
| 45 | + |
| 46 | +testChildProcess( |
| 47 | + (port) => [`--inspect=${port}`, '--build-snapshot', entry], 0, |
| 48 | + { cwd: tmpdir.path }); |
| 49 | + |
| 50 | +testChildProcess( |
| 51 | + (port) => [`--inspect=${port}`, entry], 0); |
| 52 | + |
| 53 | +testOnServerListen((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 | +}); |
0 commit comments