Skip to content

Commit 8816e85

Browse files
committed
dgram: extract cluster lazy loading method to make it testable
1 parent 2ff93c8 commit 8816e85

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/dgram.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ const SEND_BUFFER = false;
8686

8787
// Lazily loaded
8888
let cluster = null;
89+
function lazyLoadCluster() {
90+
if (!cluster) cluster = require('cluster');
91+
}
8992

9093
const errnoException = errors.errnoException;
9194
const exceptionWithHostPort = errors.exceptionWithHostPort;
@@ -200,8 +203,7 @@ function bufferSize(self, size, buffer) {
200203

201204
// Query primary process to get the server handle and utilize it.
202205
function bindServerHandle(self, options, errCb) {
203-
if (!cluster)
204-
cluster = require('cluster');
206+
lazyLoadCluster();
205207

206208
const state = self[kStateSymbol];
207209
cluster._getServer(self, options, (err, handle) => {
@@ -262,8 +264,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
262264
const exclusive = !!port.exclusive;
263265
const state = this[kStateSymbol];
264266

265-
if (!cluster)
266-
cluster = require('cluster');
267+
lazyLoadCluster();
267268

268269
if (cluster.isWorker && !exclusive) {
269270
bindServerHandle(this, {
@@ -325,8 +326,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
325326
return;
326327
}
327328

328-
if (!cluster)
329-
cluster = require('cluster');
329+
lazyLoadCluster();
330330

331331
let flags = 0;
332332
if (state.reuseAddr)

0 commit comments

Comments
 (0)