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