diff --git a/eslint.config.mjs b/eslint.config.mjs index 9570437df56..161b379e7e3 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -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, diff --git a/src/mongo/shell/BUILD.bazel b/src/mongo/shell/BUILD.bazel index 71d1749adae..6dd0896bc29 100644 --- a/src/mongo/shell/BUILD.bazel +++ b/src/mongo/shell/BUILD.bazel @@ -323,6 +323,7 @@ MONGO_SERVER_CPP_JS_FILES = [ "keyvault.js", "servers.js", "servers_misc.js", + "servers_misc_global.js", ] render_template( diff --git a/src/mongo/shell/servers_misc.d.ts b/src/mongo/shell/servers_misc.d.ts deleted file mode 100644 index ab8779463bf..00000000000 --- a/src/mongo/shell/servers_misc.d.ts +++ /dev/null @@ -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() diff --git a/src/mongo/shell/servers_misc.js b/src/mongo/shell/servers_misc.js index e882cb1443b..ee07b79cbe2 100644 --- a/src/mongo/shell/servers_misc.js +++ b/src/mongo/shell/servers_misc.js @@ -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, +}; diff --git a/src/mongo/shell/servers_misc_global.js b/src/mongo/shell/servers_misc_global.js new file mode 100644 index 00000000000..a1d6d58104d --- /dev/null +++ b/src/mongo/shell/servers_misc_global.js @@ -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; diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp index a8d11303b3a..ff142e0d96c 100644 --- a/src/mongo/shell/shell_utils.cpp +++ b/src/mongo/shell/shell_utils.cpp @@ -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);