Skip to content

Commit e7978f0

Browse files
cjihrigitaloacasas
authored andcommitted
test: cover dgram socket close during cluster bind
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 <[email protected]>
1 parent 58e2517 commit e7978f0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
const dgram = require('dgram');
6+
7+
if (common.isWindows) {
8+
common.skip('dgram clustering is currently not supported on windows.');
9+
return;
10+
}
11+
12+
if (cluster.isMaster) {
13+
cluster.fork();
14+
} else {
15+
// When the socket attempts to bind, it requests a handle from the cluster.
16+
// Close the socket before returning the handle from the cluster.
17+
const socket = dgram.createSocket('udp4');
18+
const _getServer = cluster._getServer;
19+
20+
cluster._getServer = common.mustCall(function(self, options, callback) {
21+
socket.close(common.mustCall(() => {
22+
_getServer.call(this, self, options, common.mustCall((err, handle) => {
23+
assert.strictEqual(err, 0);
24+
25+
// When the socket determines that it was already closed, it will
26+
// close the handle. Use handle.close() to terminate the test.
27+
const close = handle.close;
28+
29+
handle.close = common.mustCall(function() {
30+
setImmediate(() => cluster.worker.disconnect());
31+
return close.call(this);
32+
});
33+
34+
callback(err, handle);
35+
}));
36+
}));
37+
});
38+
39+
socket.bind(common.mustNotCall('Socket should not bind.'));
40+
}

0 commit comments

Comments
 (0)