11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/13435
41cb0ef41Sopenharmony_ci// Tests that `socket.server` is correctly set when a socket is sent to a worker
51cb0ef41Sopenharmony_ci// and the `'connection'` event is emitted manually on an HTTP server.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst cluster = require('cluster');
101cb0ef41Sopenharmony_ciconst http = require('http');
111cb0ef41Sopenharmony_ciconst net = require('net');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
141cb0ef41Sopenharmony_ci  const worker = cluster.fork();
151cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustCall((socket) => {
161cb0ef41Sopenharmony_ci    worker.send('socket', socket);
171cb0ef41Sopenharmony_ci  }));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  worker.on('exit', common.mustCall((code) => {
201cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
211cb0ef41Sopenharmony_ci    server.close();
221cb0ef41Sopenharmony_ci  }));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
251cb0ef41Sopenharmony_ci    net.createConnection(server.address().port);
261cb0ef41Sopenharmony_ci  }));
271cb0ef41Sopenharmony_ci} else {
281cb0ef41Sopenharmony_ci  const server = http.createServer();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  server.on('connection', common.mustCall((socket) => {
311cb0ef41Sopenharmony_ci    assert.strictEqual(socket.server, server);
321cb0ef41Sopenharmony_ci    socket.destroy();
331cb0ef41Sopenharmony_ci    cluster.worker.disconnect();
341cb0ef41Sopenharmony_ci  }));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  process.on('message', common.mustCall((message, socket) => {
371cb0ef41Sopenharmony_ci    server.emit('connection', socket);
381cb0ef41Sopenharmony_ci  }));
391cb0ef41Sopenharmony_ci}
40