11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
61cb0ef41Sopenharmony_ciconst net = require('net');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
91cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustCall());
101cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
111cb0ef41Sopenharmony_ci    process.send({ type: 'ready', data: { port: server.address().port } });
121cb0ef41Sopenharmony_ci  }));
131cb0ef41Sopenharmony_ci} else {
141cb0ef41Sopenharmony_ci  const cp = spawn(process.execPath,
151cb0ef41Sopenharmony_ci                   [__filename, 'child'],
161cb0ef41Sopenharmony_ci                   {
171cb0ef41Sopenharmony_ci                     stdio: ['ipc', 'inherit', 'inherit']
181cb0ef41Sopenharmony_ci                   });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  cp.on('exit', common.mustCall((code, signal) => {
211cb0ef41Sopenharmony_ci    assert.strictEqual(code, null);
221cb0ef41Sopenharmony_ci    assert.strictEqual(signal, 'SIGKILL');
231cb0ef41Sopenharmony_ci  }));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  cp.on('message', common.mustCall((msg) => {
261cb0ef41Sopenharmony_ci    const { type, data } = msg;
271cb0ef41Sopenharmony_ci    assert.strictEqual(type, 'ready');
281cb0ef41Sopenharmony_ci    const port = data.port;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    const conn = net.createConnection({
311cb0ef41Sopenharmony_ci      port,
321cb0ef41Sopenharmony_ci      onread: {
331cb0ef41Sopenharmony_ci        buffer: Buffer.alloc(65536),
341cb0ef41Sopenharmony_ci        callback: () => {},
351cb0ef41Sopenharmony_ci      }
361cb0ef41Sopenharmony_ci    });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    conn.on('error', (err) => {
391cb0ef41Sopenharmony_ci      // Error emitted on Windows.
401cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ECONNRESET');
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    conn.on('connect', common.mustCall(() => {
441cb0ef41Sopenharmony_ci      cp.kill('SIGKILL');
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci}
48