summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-cluster-close-during-bind.js
blob: 4e8cdbf9d35cbb6aefdab799db07fc331097d160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const common = require('../common');
if (common.isWindows)
  common.skip('dgram clustering is currently not supported on windows.');

const assert = require('assert');
const cluster = require('cluster');
const dgram = require('dgram');

if (cluster.isMaster) {
  cluster.fork();
} else {
  // When the socket attempts to bind, it requests a handle from the cluster.
  // Close the socket before returning the handle from the cluster.
  const socket = dgram.createSocket('udp4');
  const _getServer = cluster._getServer;

  cluster._getServer = common.mustCall(function(self, options, callback) {
    socket.close(common.mustCall(() => {
      _getServer.call(this, self, options, common.mustCall((err, handle) => {
        assert.strictEqual(err, 0);

        // When the socket determines that it was already closed, it will
        // close the handle. Use handle.close() to terminate the test.
        const close = handle.close;

        handle.close = common.mustCall(function() {
          setImmediate(() => cluster.worker.disconnect());
          return close.call(this);
        });

        callback(err, handle);
      }));
    }));
  });

  socket.bind(common.mustNotCall('Socket should not bind.'));
}