1// Flags: --inspect=0 2'use strict'; 3const common = require('../common'); 4 5common.skipIfInspectorDisabled(); 6common.skipIfWorker(); 7 8// Assert that even when started with `--inspect=0` workers are assigned 9// consecutive (i.e. deterministically predictable) debug ports 10 11const assert = require('assert'); 12const cluster = require('cluster'); 13 14function serialFork() { 15 return new Promise((res) => { 16 const worker = cluster.fork(); 17 worker.on('exit', common.mustCall((code, signal) => { 18 // code 0 is normal 19 // code 12 can happen if inspector could not bind because of a port clash 20 if (code !== 0 && code !== 12) 21 assert.fail(`code: ${code}, signal: ${signal}`); 22 const port = worker.process.spawnargs 23 .map((a) => (/=(?:.*:)?(\d{2,5})$/.exec(a) || [])[1]) 24 .filter((p) => p) 25 .pop(); 26 res(Number(port)); 27 })); 28 }); 29} 30 31if (cluster.isMaster) { 32 Promise.all([serialFork(), serialFork(), serialFork()]) 33 .then(common.mustCall((ports) => { 34 ports.splice(0, 0, process.debugPort); 35 // 4 = [master, worker1, worker2, worker3].length() 36 assert.strictEqual(ports.length, 4); 37 assert(ports.every((port) => port > 0)); 38 assert(ports.every((port) => port < 65536)); 39 assert.strictEqual(ports[0] === 65535 ? 1024 : ports[0] + 1, ports[1]); 40 assert.strictEqual(ports[1] === 65535 ? 1024 : ports[1] + 1, ports[2]); 41 assert.strictEqual(ports[2] === 65535 ? 1024 : ports[2] + 1, ports[3]); 42 })) 43 .catch( 44 (err) => { 45 console.error(err); 46 process.exit(1); 47 }); 48} else { 49 process.exit(0); 50} 51