• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Regression test for https://github.com/nodejs/node/issues/13435
4// Tests that `socket.server` is correctly set when a socket is sent to a worker
5// and the `'connection'` event is emitted manually on an HTTP server.
6
7const common = require('../common');
8const assert = require('assert');
9const cluster = require('cluster');
10const http = require('http');
11const net = require('net');
12
13if (cluster.isMaster) {
14  const worker = cluster.fork();
15  const server = net.createServer(common.mustCall((socket) => {
16    worker.send('socket', socket);
17  }));
18
19  worker.on('exit', common.mustCall((code) => {
20    assert.strictEqual(code, 0);
21    server.close();
22  }));
23
24  server.listen(0, common.mustCall(() => {
25    net.createConnection(server.address().port);
26  }));
27} else {
28  const server = http.createServer();
29
30  server.on('connection', common.mustCall((socket) => {
31    assert.strictEqual(socket.server, server);
32    socket.destroy();
33    cluster.worker.disconnect();
34  }));
35
36  process.on('message', common.mustCall((message, socket) => {
37    server.emit('connection', socket);
38  }));
39}
40