@@ -46,46 +46,29 @@ export default class MongoDBMemoryServer {
4646 }
4747 }
4848
49+ debug ( msg : string ) {
50+ if ( this . opts . debug ) {
51+ console . log ( msg ) ;
52+ }
53+ }
54+
4955 async start ( ) : Promise < boolean > {
5056 if ( this . runningInstance ) {
5157 throw new Error (
5258 'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.'
5359 ) ;
5460 }
5561
56- this . runningInstance = Promise . resolve ( ) . then ( async ( ) => {
57- const data = { } ;
58- let tmpDir ;
59-
60- data . port = await getport ( this . opts . port ) ;
61- data . uri = await generateConnectionString ( data . port ) ;
62- data . storageEngine = this . opts . storageEngine || 'ephemeralForTest' ;
63- if ( this . opts . dbPath ) {
64- data . dbPath = this . opts . dbPath ;
65- } else {
66- tmpDir = tmp . dirSync ( { prefix : 'mongo-mem-' , unsafeCleanup : true } ) ;
67- data . dbPath = tmpDir . name ;
68- }
69-
70- if ( this . opts . debug ) {
71- console . log ( `Starting MongoDB instance with following options: ${ JSON . stringify ( data ) } ` ) ;
72- }
73-
74- const mongodCli = new MongodHelper ( [
75- '--port' ,
76- data . port ,
77- '--storageEngine' ,
78- data . storageEngine ,
79- '--dbpath' ,
80- data . dbPath ,
81- '--noauth' ,
82- ] ) ;
83-
84- mongodCli . debug . enabled = this . opts . debug ;
85-
86- // Download if not exists mongo binaries in ~/.mongodb-prebuilt
87- // After that startup MongoDB instance
88- await mongodCli . run ( ) . catch ( err => {
62+ this . runningInstance = this . _startUpInstance ( )
63+ . catch ( err => {
64+ if ( err . message === 'Mongod shutting down' || err === 'Mongod shutting down' ) {
65+ this . debug ( `Mongodb does not started. Trying to start on another port one more time...` ) ;
66+ this . opts . port = null ;
67+ return this . _startUpInstance ( ) ;
68+ }
69+ throw err ;
70+ } )
71+ . catch ( err => {
8972 if ( ! this . opts . debug ) {
9073 throw new Error (
9174 `${ err . message } \n\nUse debug option for more info: new MongoMemoryServer({ debug: true })`
@@ -94,32 +77,60 @@ export default class MongoDBMemoryServer {
9477 throw err ;
9578 } ) ;
9679
97- data . mongodCli = mongodCli ;
98- data . tmpDir = tmpDir ;
80+ return this . runningInstance . then ( ( ) => true ) ;
81+ }
9982
100- return data ;
101- } ) ;
83+ async _startUpInstance ( ) : Promise < MongoInstanceDataT > {
84+ const data = { } ;
85+ let tmpDir ;
86+
87+ data . port = await getport ( this . opts . port ) ;
88+ data . uri = await generateConnectionString ( data . port ) ;
89+ data . storageEngine = this . opts . storageEngine || 'ephemeralForTest' ;
90+ if ( this . opts . dbPath ) {
91+ data . dbPath = this . opts . dbPath ;
92+ } else {
93+ tmpDir = tmp . dirSync ( { prefix : 'mongo-mem-' , unsafeCleanup : true } ) ;
94+ data . dbPath = tmpDir . name ;
95+ }
10296
103- return this . runningInstance . then ( ( ) => true ) ;
97+ this . debug ( `Starting MongoDB instance with following options: ${ JSON . stringify ( data ) } ` ) ;
98+
99+ const mongodCli = new MongodHelper ( [
100+ '--port' ,
101+ data . port ,
102+ '--storageEngine' ,
103+ data . storageEngine ,
104+ '--dbpath' ,
105+ data . dbPath ,
106+ '--noauth' ,
107+ ] ) ;
108+
109+ mongodCli . debug . enabled = this . opts . debug ;
110+
111+ // Download if not exists mongo binaries in ~/.mongodb-prebuilt
112+ // After that startup MongoDB instance
113+ await mongodCli . run ( ) ;
114+
115+ data . mongodCli = mongodCli ;
116+ data . tmpDir = tmpDir ;
117+
118+ return data ;
104119 }
105120
106121 async stop ( ) : Promise < boolean > {
107122 const { mongodCli, port, tmpDir } = await this . getInstanceData ( ) ;
108123
109124 if ( mongodCli && mongodCli . mongoBin . childProcess ) {
110125 // .mongoBin.childProcess.connected
111- if ( this . opts . debug ) {
112- console . log (
113- `Shutdown MongoDB server on port ${ port } with pid ${ mongodCli . mongoBin . childProcess . pid } `
114- ) ;
115- }
126+ this . debug (
127+ `Shutdown MongoDB server on port ${ port } with pid ${ mongodCli . mongoBin . childProcess . pid } `
128+ ) ;
116129 mongodCli . mongoBin . childProcess . kill ( ) ;
117130 }
118131
119132 if ( tmpDir ) {
120- if ( this . opts . debug ) {
121- console . log ( `Removing tmpDir ${ tmpDir . name } ` ) ;
122- }
133+ this . debug ( `Removing tmpDir ${ tmpDir . name } ` ) ;
123134 tmpDir . removeCallback ( ) ;
124135 }
125136
0 commit comments