Skip to content

Commit 4001dd2

Browse files
pd4d10targos
authored andcommitted
dgram: extract cluster lazy loading method to make it testable
PR-URL: #38563 Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 16b2fb4 commit 4001dd2

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/dgram.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ const RECV_BUFFER = true;
8585
const SEND_BUFFER = false;
8686

8787
// Lazily loaded
88-
let cluster = null;
88+
let _cluster = null;
89+
function lazyLoadCluster() {
90+
if (!_cluster) _cluster = require('cluster');
91+
return _cluster;
92+
}
8993

9094
const errnoException = errors.errnoException;
9195
const exceptionWithHostPort = errors.exceptionWithHostPort;
@@ -200,8 +204,7 @@ function bufferSize(self, size, buffer) {
200204

201205
// Query primary process to get the server handle and utilize it.
202206
function bindServerHandle(self, options, errCb) {
203-
if (!cluster)
204-
cluster = require('cluster');
207+
const cluster = lazyLoadCluster();
205208

206209
const state = self[kStateSymbol];
207210
cluster._getServer(self, options, (err, handle) => {
@@ -262,8 +265,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
262265
const exclusive = !!port.exclusive;
263266
const state = this[kStateSymbol];
264267

265-
if (!cluster)
266-
cluster = require('cluster');
268+
const cluster = lazyLoadCluster();
267269

268270
if (cluster.isWorker && !exclusive) {
269271
bindServerHandle(this, {
@@ -325,8 +327,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
325327
return;
326328
}
327329

328-
if (!cluster)
329-
cluster = require('cluster');
330+
const cluster = lazyLoadCluster();
330331

331332
let flags = 0;
332333
if (state.reuseAddr)

0 commit comments

Comments
 (0)