Skip to content

Commit

Permalink
feat(MongoInstance::start): error when already having a "mongodProces…
Browse files Browse the repository at this point in the history
…s" defined

fixes #662
  • Loading branch information
hasezoey committed Aug 17, 2022
1 parent df41101 commit cb80073
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/mongodb-memory-server-core/src/util/MongoInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import {
import { lt } from 'semver';
import { EventEmitter } from 'events';
import { MongoClient, MongoClientOptions, MongoNetworkError } from 'mongodb';
import { KeyFileMissingError, StartBinaryFailedError, StdoutInstanceError } from './errors';
import {
GenericMMSError,
KeyFileMissingError,
StartBinaryFailedError,
StdoutInstanceError,
} from './errors';

// ignore the nodejs warning for coverage
/* istanbul ignore next */
Expand Down Expand Up @@ -327,6 +332,13 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
*/
async start(): Promise<void> {
this.debug('start');

if (!isNullOrUndefined(this.mongodProcess?.pid)) {
throw new GenericMMSError(
`Cannot run "MongoInstance.start" because "mongodProcess.pid" is still defined (pid: ${this.mongodProcess?.pid})`
);
}

this.isInstancePrimary = false;
this.isInstanceReady = false;
this.isReplSet = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as dbUtil from '../utils';
import MongodbInstance, { MongoInstanceEvents } from '../MongoInstance';
import resolveConfig, { ResolveConfigVariables } from '../resolveConfig';
import getPort from 'get-port';
import { StartBinaryFailedError, StdoutInstanceError } from '../errors';
import { GenericMMSError, StartBinaryFailedError, StdoutInstanceError } from '../errors';
import { assertIsError } from '../../__tests__/testUtils/test_utils';

jest.setTimeout(100000); // 10s
Expand Down Expand Up @@ -506,4 +506,28 @@ describe('MongodbInstance', () => {
});
});
});

it('should throw error if instance is already started (#662)', async () => {
const gotPort = await getPort();
const mongod = await MongodbInstance.create({
instance: { port: gotPort, dbPath: tmpDir.name },
binary: { version },
});

try {
await mongod.start();

fail('Expected to Fail');
} catch (err) {
expect(err).toBeInstanceOf(GenericMMSError);
assertIsError(err); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(
err.message.includes(
'Cannot run "MongoInstance.start" because "mongodProcess.pid" is still defined'
)
).toBeTruthy();
} finally {
await mongod.stop();
}
});
});

0 comments on commit cb80073

Please sign in to comment.