File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed
packages/mongodb-memory-server-core/src Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -261,7 +261,7 @@ export default class MongoMemoryReplSet extends EventEmitter {
261
261
return server ;
262
262
}
263
263
264
- async _waitForPrimary ( ) : Promise < boolean > {
264
+ async _waitForPrimary ( timeout : number = 30000 ) : Promise < boolean > {
265
265
if ( ! this . admin ) {
266
266
return false ;
267
267
}
@@ -273,8 +273,14 @@ export default class MongoMemoryReplSet extends EventEmitter {
273
273
this . debug ( ' replStatus:' , replStatus ) ;
274
274
const hasPrimary = replStatus . repl . ismaster ;
275
275
if ( ! hasPrimary ) {
276
- this . debug ( 'No PRIMARY yet. Waiting...' ) ;
277
- return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( this . _waitForPrimary ( ) ) , 500 ) ) ;
276
+ const restTimeout = timeout - 500 ;
277
+ if ( restTimeout <= 0 ) {
278
+ throw new Error ( `No PRIMARY elected yet. Timeout expired. Exiting...` ) ;
279
+ }
280
+ this . debug ( `No PRIMARY yet. Waiting... ${ restTimeout } ms` ) ;
281
+ return new Promise ( ( resolve ) =>
282
+ setTimeout ( ( ) => resolve ( this . _waitForPrimary ( restTimeout ) ) , 500 )
283
+ ) ;
278
284
}
279
285
280
286
return true ;
You can’t perform that action at this time.
0 commit comments