Skip to content

Commit

Permalink
test: cover dgram socket close during cluster bind
Browse files Browse the repository at this point in the history
When a non-exclusive dgram socket is bound from a cluster
worker, it attempts to get a handle from the cluster module.
This commit adds coverage for the case where the socket is
closed while querying the cluster module for a handle.

PR-URL: #11292
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and italoacasas committed Feb 22, 2017
1 parent 58e2517 commit e7978f0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/parallel/test-dgram-cluster-close-during-bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const dgram = require('dgram');

if (common.isWindows) {
common.skip('dgram clustering is currently not supported on windows.');
return;
}

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.'));
}

0 comments on commit e7978f0

Please sign in to comment.