dgram: extract cluster lazy loading method to make it testable

PR-URL: https://github.com/nodejs/node/pull/38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Rongjian Zhang authored and Michaël Zasso committed 2021-06-11 09:30:15 +02:00
1 parent 3c886e0ad6
commit bac9ba4f8a
1 file changed
+8 -7
+8 -7
View File
@@ -85,7 +85,11 @@ const RECV_BUFFER = true;
const SEND_BUFFER = false;
// Lazily loaded
let cluster = null;
let _cluster = null;
function lazyLoadCluster() {
if (!_cluster) _cluster = require('cluster');
return _cluster;
}
const errnoException = errors.errnoException;
const exceptionWithHostPort = errors.exceptionWithHostPort;
@@ -200,8 +204,7 @@ function bufferSize(self, size, buffer) {
// Query master process to get the server handle and utilize it.
function bindServerHandle(self, options, errCb) {
if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();
const state = self[kStateSymbol];
cluster._getServer(self, options, (err, handle) => {
@@ -262,8 +265,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
const exclusive = !!port.exclusive;
const state = this[kStateSymbol];
if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();
if (cluster.isWorker && !exclusive) {
bindServerHandle(this, {
@@ -325,8 +327,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
return;
}
if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();
let flags = 0;
if (state.reuseAddr)