File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
packages/mongodb-memory-server-core/src/util Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 99 killProcess ,
1010 ManagerBase ,
1111 checkBinaryPermissions ,
12+ isAlive ,
1213} from './utils' ;
1314import { lt } from 'semver' ;
1415import { EventEmitter } from 'events' ;
@@ -423,7 +424,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
423424 // wrap the actual stop in a promise, so it can be awaited in multiple calls
424425 // for example a instanceError while stop is already running would cause another stop
425426 this . stopPromise = ( async ( ) => {
426- if ( ! isNullOrUndefined ( this . mongodProcess ) ) {
427+ if ( ! isNullOrUndefined ( this . mongodProcess ) && isAlive ( this . mongodProcess . pid ) ) {
427428 // try to run "shutdown" before running "killProcess" (gracefull "SIGINT")
428429 // using this, otherwise on windows nodejs will handle "SIGINT" & "SIGTERM" & "SIGKILL" the same (instant exit)
429430 if ( this . isReplSet ) {
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import {
1010 UnexpectedCloseError ,
1111} from '../errors' ;
1212import { assertIsError } from '../../__tests__/testUtils/test_utils' ;
13+ import { MongoClient } from 'mongodb' ;
1314
1415jest . setTimeout ( 100000 ) ; // 10s
1516
@@ -278,6 +279,25 @@ describe('MongodbInstance', () => {
278279 expect ( dbUtil . killProcess ) . not . toBeCalled ( ) ;
279280 } ) ;
280281
282+ it ( '"kill" should not try to open a connection to a not running ReplSet' , async ( ) => {
283+ const gotPort = await getFreePort ( ) ;
284+ const mongod = new MongodbInstance ( {
285+ instance : {
286+ replSet : 'testset' ,
287+ ip : '127.0.0.1' ,
288+ port : gotPort ,
289+ dbPath : tmpDir ,
290+ } ,
291+ } ) ;
292+ await mongod . start ( ) ;
293+ jest . spyOn ( MongoClient , 'connect' ) ;
294+ process . kill ( mongod . mongodProcess ! . pid , 'SIGKILL' ) ;
295+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
296+ await mongod . stop ( ) ;
297+
298+ expect ( MongoClient . connect ) . not . toBeCalled ( ) ;
299+ } ) ;
300+
281301 it ( '"_launchMongod" should throw an error if "mongodProcess.pid" is undefined' , ( ) => {
282302 const mongod = new MongodbInstance ( { instance : { port : 0 , dbPath : '' } } ) ; // dummy values - they shouldnt matter
283303 const mockBinary = '/tmp/thisShouldNotExist' ;
You can’t perform that action at this time.
0 commit comments