Skip to content

Commit

Permalink
fix(kill): should not try to open a connection to a not running ReplSet
Browse files Browse the repository at this point in the history
  • Loading branch information
arthursimas1 committed Oct 12, 2023
1 parent c1f2333 commit b58958a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
killProcess,
ManagerBase,
checkBinaryPermissions,
isAlive,
} from './utils';
import { lt } from 'semver';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -423,7 +424,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
// wrap the actual stop in a promise, so it can be awaited in multiple calls
// for example a instanceError while stop is already running would cause another stop
this.stopPromise = (async () => {
if (!isNullOrUndefined(this.mongodProcess)) {
if (!isNullOrUndefined(this.mongodProcess) && isAlive(this.mongodProcess.pid)) {
// try to run "shutdown" before running "killProcess" (gracefull "SIGINT")
// using this, otherwise on windows nodejs will handle "SIGINT" & "SIGTERM" & "SIGKILL" the same (instant exit)
if (this.isReplSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UnexpectedCloseError,
} from '../errors';
import { assertIsError } from '../../__tests__/testUtils/test_utils';
import { MongoClient } from 'mongodb';

jest.setTimeout(100000); // 10s

Expand Down Expand Up @@ -278,6 +279,25 @@ describe('MongodbInstance', () => {
expect(dbUtil.killProcess).not.toBeCalled();
});

it('"kill" should not try to open a connection to a not running ReplSet', async () => {
const gotPort = await getFreePort();
const mongod = new MongodbInstance({
instance: {
replSet: 'testset',
ip: '127.0.0.1',
port: gotPort,
dbPath: tmpDir,
},
});
await mongod.start();
jest.spyOn(MongoClient, 'connect');
process.kill(mongod.mongodProcess!.pid, 'SIGKILL');
await new Promise<void>((resolve) => setTimeout(resolve, 100));
await mongod.stop();

expect(MongoClient.connect).not.toBeCalled();
});

it('"_launchMongod" should throw an error if "mongodProcess.pid" is undefined', () => {
const mongod = new MongodbInstance({ instance: { port: 0, dbPath: '' } }); // dummy values - they shouldnt matter
const mockBinary = '/tmp/thisShouldNotExist';
Expand Down

0 comments on commit b58958a

Please sign in to comment.