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 <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>
  • Loading branch information
pd4d10 authored and targos committed May 17, 2021
1 parent 16b2fb4 commit 4001dd2
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 primary 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 4001dd2

Please sign in to comment.