Skip to content

Commit

Permalink
dgram: extract cluster lazy loading method to make it testable
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pd4d10 authored and targos committed May 30, 2021
1 parent ed76154 commit f36fe40
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f36fe40

Please sign in to comment.