11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
61cb0ef41Sopenharmony_ciconst { createServer } = require('http');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ciconst entry = fixtures.path('empty.js');
111cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction testOnServerListen(fn) {
141cb0ef41Sopenharmony_ci  const server = createServer((socket) => {
151cb0ef41Sopenharmony_ci    socket.end('echo');
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  server.on('listening', () => {
191cb0ef41Sopenharmony_ci    fn(server);
201cb0ef41Sopenharmony_ci    server.close();
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci  server.listen(0, '127.0.0.1');
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction testChildProcess(getArgs, exitCode, options) {
261cb0ef41Sopenharmony_ci  testOnServerListen((server) => {
271cb0ef41Sopenharmony_ci    const { port } = server.address();
281cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, getArgs(port), options);
291cb0ef41Sopenharmony_ci    const stderr = child.stderr.toString().trim();
301cb0ef41Sopenharmony_ci    const stdout = child.stdout.toString().trim();
311cb0ef41Sopenharmony_ci    console.log('[STDERR]');
321cb0ef41Sopenharmony_ci    console.log(stderr);
331cb0ef41Sopenharmony_ci    console.log('[STDOUT]');
341cb0ef41Sopenharmony_ci    console.log(stdout);
351cb0ef41Sopenharmony_ci    const match = stderr.match(
361cb0ef41Sopenharmony_ci      /Starting inspector on 127\.0\.0\.1:(\d+) failed: address already in use/
371cb0ef41Sopenharmony_ci    );
381cb0ef41Sopenharmony_ci    assert.notStrictEqual(match, null);
391cb0ef41Sopenharmony_ci    assert.strictEqual(match[1], port + '');
401cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, exitCode);
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_citmpdir.refresh();
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_citestChildProcess(
471cb0ef41Sopenharmony_ci  (port) => [`--inspect=${port}`, '--build-snapshot', entry], 0,
481cb0ef41Sopenharmony_ci  { cwd: tmpdir.path });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_citestChildProcess(
511cb0ef41Sopenharmony_ci  (port) => [`--inspect=${port}`, entry], 0);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_citestOnServerListen((server) => {
541cb0ef41Sopenharmony_ci  const { port } = server.address();
551cb0ef41Sopenharmony_ci  const worker = new Worker(entry, {
561cb0ef41Sopenharmony_ci    execArgv: [`--inspect=${port}`]
571cb0ef41Sopenharmony_ci  });
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  worker.on('error', common.mustNotCall());
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  worker.on('exit', common.mustCall((code) => {
621cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci});
65