Skip to content

Commit

Permalink
refactor(MongoMemoryServer): try to acquire port later
Browse files Browse the repository at this point in the history
to reduce time potentially it being used elsewhere
  • Loading branch information
hasezoey committed Nov 9, 2023
1 parent ed420e8 commit f0385d6
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,6 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
*/
let isNew: boolean = true;

// use pre-defined port if available, otherwise generate a new port
let port = typeof instOpts.port === 'number' ? instOpts.port : undefined;

// if "forceSamePort" is not true, and get a available port
if (!forceSamePort || isNullOrUndefined(port)) {
port = await this.getNewPort(port);
}

const opts = await DryMongoBinary.generateOptions(this.opts.binary);
let storageEngine = instOpts.storageEngine;

Expand All @@ -398,6 +390,14 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
}
}

// use pre-defined port if available, otherwise generate a new port
let port = typeof instOpts.port === 'number' ? instOpts.port : undefined;

// if "forceSamePort" is not true, and get a available port
if (!forceSamePort || isNullOrUndefined(port)) {
port = await this.getNewPort(port);
}

// consider directly using "this.opts.instance", to pass through all options, even if not defined in "StartupInstanceData"
const data: StartupInstanceData = {
port: port,
Expand Down

0 comments on commit f0385d6

Please sign in to comment.