SERVER-105597: Make servers_misc.js a module (#38494)

GitOrigin-RevId: 2c3ef1ed3cc219669a6a548cc3e858885ea97999
This commit is contained in:
Steve McClure 2025-07-14 14:52:08 -04:00 committed by MongoDB Bot
parent 276d937703
commit a6f2fa1f90
6 changed files with 51 additions and 36 deletions

View File

@ -96,7 +96,7 @@ export default [
startMongoProgram: true,
startMongoProgramNoConnect: true,
// src/mongo/shell/servers_misc.d.ts
// src/mongo/shell/servers_misc_global.js
ToolTest: true,
allocatePort: true,
allocatePorts: true,

View File

@ -323,6 +323,7 @@ MONGO_SERVER_CPP_JS_FILES = [
"keyvault.js",
"servers.js",
"servers_misc.js",
"servers_misc_global.js",
]
render_template(

View File

@ -1,9 +0,0 @@
// type declarations for servers_misc.js
declare function ToolTest()
declare function allocatePort()
declare function allocatePorts()
declare function resetAllocatedPorts()
declare function startParallelShell()
declare var testingReplication
declare function uncheckedParallelShellPidsString()

View File

@ -1,4 +1,4 @@
ToolTest = function(name, extraOptions) {
function ToolTest(name, extraOptions) {
this.name = name;
this.options = extraOptions;
this.port = allocatePort();
@ -62,33 +62,18 @@ ToolTest.prototype.runTool = function() {
return runMongoProgram.apply(null, a);
};
/**
* Returns a port number that has not been given out to any other caller from the same mongo shell.
*/
var allocatePort;
/**
* Resets the range of ports which have already been given out to callers of allocatePort().
*
* This function can be used to allow a test to allocate a large number of ports as part of starting
* many MongoDB deployments without worrying about hitting the configured maximum. Callers of this
* function should take care to ensure MongoDB deployments started earlier have been terminated and
* won't be reused.
*/
var resetAllocatedPorts;
var uncheckedParallelShellPids;
var startParallelShell;
(function() {
// Defer initializing these variables until the first call, as TestData attributes may be
// initialized as part of the --eval argument (e.g. by resmoke.py), which will not be evaluated
// until after this has loaded.
var maxPort;
var nextPort;
allocatePort = function() {
/**
* Returns a port number that has not been given out to any other caller from the same mongo shell.
*/
function allocatePort() {
// The default port was chosen in an attempt to have a large number of unassigned ports that
// are also outside the ephemeral port range.
nextPort = nextPort || jsTestOptions().minPort || 20000;
@ -100,17 +85,25 @@ allocatePort = function() {
return nextPort++;
};
resetAllocatedPorts = function() {
/**
* Resets the range of ports which have already been given out to callers of allocatePort().
*
* This function can be used to allow a test to allocate a large number of ports as part of starting
* many MongoDB deployments without worrying about hitting the configured maximum. Callers of this
* function should take care to ensure MongoDB deployments started earlier have been terminated and
* won't be reused.
*/
function resetAllocatedPorts() {
jsTest.log("Resetting the range of allocated ports");
maxPort = nextPort = undefined;
};
var parallelShellPids = [];
uncheckedParallelShellPidsString = function() {
function uncheckedParallelShellPidsString() {
return parallelShellPids.join(", ");
};
startParallelShell = function(jsCode, port, noConnect, ...optionArgs) {
function startParallelShell(jsCode, port, noConnect, ...optionArgs) {
var shellPath = MongoRunner.getMongoShellPath();
var args = [shellPath];
@ -189,13 +182,12 @@ startParallelShell = function(jsCode, port, noConnect, ...optionArgs) {
return exitCode;
};
};
})();
/**
* Returns a list of 'numPorts' port numbers that have not been given out to any other caller from
* the same mongo shell.
*/
allocatePorts = function(numPorts) {
function allocatePorts(numPorts) {
var ports = [];
for (var i = 0; i < numPorts; i++) {
ports.push(allocatePort());
@ -205,3 +197,13 @@ allocatePorts = function(numPorts) {
};
var testingReplication = false;
export {
ToolTest,
allocatePort,
allocatePorts,
resetAllocatedPorts,
startParallelShell,
testingReplication,
uncheckedParallelShellPidsString,
};

View File

@ -0,0 +1,19 @@
// Populate global variables from modules for backwards compatibility
import {
allocatePort,
allocatePorts,
resetAllocatedPorts,
startParallelShell,
testingReplication,
ToolTest,
uncheckedParallelShellPidsString,
} from "src/mongo/shell/servers_misc.js";
globalThis.ToolTest = ToolTest;
globalThis.allocatePort = allocatePort;
globalThis.allocatePorts = allocatePorts;
globalThis.resetAllocatedPorts = resetAllocatedPorts;
globalThis.startParallelShell = startParallelShell;
globalThis.testingReplication = testingReplication;
globalThis.uncheckedParallelShellPidsString = uncheckedParallelShellPidsString;

View File

@ -163,6 +163,7 @@ extern const JSFile feature_compatibility_version;
extern const JSFile feature_compatibility_version_global;
extern const JSFile servers;
extern const JSFile servers_misc;
extern const JSFile servers_misc_global;
} // namespace JSFiles
namespace {
@ -1234,15 +1235,16 @@ void initScope(Scope& scope) {
scope.execSetup(JSFiles::bridge);
scope.execSetup(JSFiles::data_consistency_checker);
scope.execSetup(JSFiles::feature_compatibility_version);
scope.execSetup(JSFiles::servers_misc);
// globals
scope.execSetup(JSFiles::bridge_global);
scope.execSetup(JSFiles::data_consistency_checker_global);
scope.execSetup(JSFiles::feature_compatibility_version_global);
scope.execSetup(JSFiles::servers_misc_global);
// scripts
scope.execSetup(JSFiles::servers);
scope.execSetup(JSFiles::servers_misc);
initializeEnterpriseScope(scope);