• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const cluster = require('cluster');
26const net = require('net');
27
28let destroyed;
29let success;
30let worker;
31let server;
32
33// Workers do not exit on disconnect, they exit under normal node rules: when
34// they have nothing keeping their loop alive, like an active connection
35//
36// test this by:
37//
38// 1 creating a server, so worker can make a connection to something
39// 2 disconnecting worker
40// 3 wait to confirm it did not exit
41// 4 destroy connection
42// 5 confirm it does exit
43if (cluster.isMaster) {
44  server = net.createServer(function(conn) {
45    server.close();
46    worker.disconnect();
47    worker.once('disconnect', function() {
48      setTimeout(function() {
49        conn.destroy();
50        destroyed = true;
51      }, 1000);
52    }).once('exit', function() {
53      // Worker should not exit while it has a connection
54      assert(destroyed, 'worker exited before connection destroyed');
55      success = true;
56    });
57
58  }).listen(0, function() {
59    const port = this.address().port;
60
61    worker = cluster.fork()
62      .on('online', function() {
63        this.send({ port });
64      });
65  });
66  process.on('exit', function() {
67    assert(success);
68  });
69} else {
70  process.on('message', function(msg) {
71    // We shouldn't exit, not while a network connection exists
72    net.connect(msg.port);
73  });
74}
75