|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +common.skipIfInspectorDisabled(); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const cluster = require('cluster'); |
| 9 | + |
| 10 | +const debuggerPort = common.PORT; |
| 11 | +const childProcess = require('child_process'); |
| 12 | + |
| 13 | +let offset = 0; |
| 14 | + |
| 15 | +/* |
| 16 | + * This test suite checks that inspector port in cluster is incremented |
| 17 | + * for different execArgv combinations |
| 18 | + */ |
| 19 | + |
| 20 | +function testRunnerMain() { |
| 21 | + spawnMaster({ |
| 22 | + execArgv: ['--inspect'], |
| 23 | + workers: [{expectedPort: 9230}] |
| 24 | + }); |
| 25 | + |
| 26 | + let port = debuggerPort + offset++ * 10; |
| 27 | + |
| 28 | + spawnMaster({ |
| 29 | + execArgv: [`--inspect=${port}`], |
| 30 | + workers: [ |
| 31 | + {expectedPort: port + 1}, |
| 32 | + {expectedPort: port + 2}, |
| 33 | + {expectedPort: port + 3} |
| 34 | + ] |
| 35 | + }); |
| 36 | + |
| 37 | + port = debuggerPort + offset++ * 10; |
| 38 | + |
| 39 | + spawnMaster({ |
| 40 | + execArgv: ['--inspect', `--inspect-port=${port}`], |
| 41 | + workers: [{expectedPort: port + 1}] |
| 42 | + }); |
| 43 | + |
| 44 | + port = debuggerPort + offset++ * 10; |
| 45 | + |
| 46 | + spawnMaster({ |
| 47 | + execArgv: ['--inspect', `--debug-port=${port}`], |
| 48 | + workers: [{expectedPort: port + 1}] |
| 49 | + }); |
| 50 | + |
| 51 | + port = debuggerPort + offset++ * 10; |
| 52 | + |
| 53 | + spawnMaster({ |
| 54 | + execArgv: [`--inspect=0.0.0.0:${port}`], |
| 55 | + workers: [{expectedPort: port + 1, expectedHost: '0.0.0.0'}] |
| 56 | + }); |
| 57 | + |
| 58 | + port = debuggerPort + offset++ * 10; |
| 59 | + |
| 60 | + spawnMaster({ |
| 61 | + execArgv: [`--inspect=127.0.0.1:${port}`], |
| 62 | + workers: [{expectedPort: port + 1, expectedHost: '127.0.0.1'}] |
| 63 | + }); |
| 64 | + |
| 65 | + if (common.hasIPv6) { |
| 66 | + port = debuggerPort + offset++ * 10; |
| 67 | + |
| 68 | + spawnMaster({ |
| 69 | + execArgv: [`--inspect=[::]:${port}`], |
| 70 | + workers: [{expectedPort: port + 1, expectedHost: '::'}] |
| 71 | + }); |
| 72 | + |
| 73 | + port = debuggerPort + offset++ * 10; |
| 74 | + |
| 75 | + spawnMaster({ |
| 76 | + execArgv: [`--inspect=[::1]:${port}`], |
| 77 | + workers: [{expectedPort: port + 1, expectedHost: '::1'}] |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function masterProcessMain() { |
| 83 | + const workers = JSON.parse(process.env.workers); |
| 84 | + |
| 85 | + for (const worker of workers) { |
| 86 | + cluster.fork({ |
| 87 | + expectedPort: worker.expectedPort, |
| 88 | + expectedHost: worker.expectedHost |
| 89 | + }).on('exit', common.mustCall(checkExitCode)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function workerProcessMain() { |
| 94 | + const {expectedPort, expectedHost} = process.env; |
| 95 | + |
| 96 | + assert.strictEqual(process.debugPort, +expectedPort); |
| 97 | + |
| 98 | + if (expectedHost !== 'undefined') { |
| 99 | + assert.strictEqual( |
| 100 | + process.binding('config').debugOptions.host, |
| 101 | + expectedHost |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + process.exit(); |
| 106 | +} |
| 107 | + |
| 108 | +function spawnMaster({execArgv, workers}) { |
| 109 | + childProcess.fork(__filename, { |
| 110 | + env: { |
| 111 | + workers: JSON.stringify(workers), |
| 112 | + testProcess: true |
| 113 | + }, |
| 114 | + execArgv |
| 115 | + }).on('exit', common.mustCall(checkExitCode)); |
| 116 | +} |
| 117 | + |
| 118 | +function checkExitCode(code, signal) { |
| 119 | + assert.strictEqual(code, 0); |
| 120 | + assert.strictEqual(signal, null); |
| 121 | +} |
| 122 | + |
| 123 | +if (!process.env.testProcess) { |
| 124 | + testRunnerMain(); |
| 125 | +} else if (cluster.isMaster) { |
| 126 | + masterProcessMain(); |
| 127 | +} else { |
| 128 | + workerProcessMain(); |
| 129 | +} |
0 commit comments