11cb0ef41Sopenharmony_ci// Flags: --inspect=0
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
61cb0ef41Sopenharmony_cicommon.skipIfWorker();
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Assert that even when started with `--inspect=0` workers are assigned
91cb0ef41Sopenharmony_ci// consecutive (i.e. deterministically predictable) debug ports
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst cluster = require('cluster');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction serialFork() {
151cb0ef41Sopenharmony_ci  return new Promise((res) => {
161cb0ef41Sopenharmony_ci    const worker = cluster.fork();
171cb0ef41Sopenharmony_ci    worker.on('exit', common.mustCall((code, signal) => {
181cb0ef41Sopenharmony_ci      // code 0 is normal
191cb0ef41Sopenharmony_ci      // code 12 can happen if inspector could not bind because of a port clash
201cb0ef41Sopenharmony_ci      if (code !== 0 && code !== 12)
211cb0ef41Sopenharmony_ci        assert.fail(`code: ${code}, signal: ${signal}`);
221cb0ef41Sopenharmony_ci      const port = worker.process.spawnargs
231cb0ef41Sopenharmony_ci        .map((a) => (/=(?:.*:)?(\d{2,5})$/.exec(a) || [])[1])
241cb0ef41Sopenharmony_ci        .filter((p) => p)
251cb0ef41Sopenharmony_ci        .pop();
261cb0ef41Sopenharmony_ci      res(Number(port));
271cb0ef41Sopenharmony_ci    }));
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
321cb0ef41Sopenharmony_ci  Promise.all([serialFork(), serialFork(), serialFork()])
331cb0ef41Sopenharmony_ci    .then(common.mustCall((ports) => {
341cb0ef41Sopenharmony_ci      ports.splice(0, 0, process.debugPort);
351cb0ef41Sopenharmony_ci      // 4 = [primary, worker1, worker2, worker3].length()
361cb0ef41Sopenharmony_ci      assert.strictEqual(ports.length, 4);
371cb0ef41Sopenharmony_ci      assert(ports.every((port) => port > 0));
381cb0ef41Sopenharmony_ci      assert(ports.every((port) => port < 65536));
391cb0ef41Sopenharmony_ci      assert.strictEqual(ports[0] === 65535 ? 1024 : ports[0] + 1, ports[1]);
401cb0ef41Sopenharmony_ci      assert.strictEqual(ports[1] === 65535 ? 1024 : ports[1] + 1, ports[2]);
411cb0ef41Sopenharmony_ci      assert.strictEqual(ports[2] === 65535 ? 1024 : ports[2] + 1, ports[3]);
421cb0ef41Sopenharmony_ci    }))
431cb0ef41Sopenharmony_ci    .catch(
441cb0ef41Sopenharmony_ci      (err) => {
451cb0ef41Sopenharmony_ci        console.error(err);
461cb0ef41Sopenharmony_ci        process.exit(1);
471cb0ef41Sopenharmony_ci      });
481cb0ef41Sopenharmony_ci} else {
491cb0ef41Sopenharmony_ci  process.exit(0);
501cb0ef41Sopenharmony_ci}
51