Skip to content

Commit 17522dc

Browse files
committed
fix(MongoMemoryReplSet::_initReplSet): attempt to fix "server selection timed out" when using auth
1 parent 69f7261 commit 17522dc

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,9 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
692692
await this.stop({ doCleanup: false, force: false }); // stop all servers for enabling auth
693693
log('_initReplSet: starting all server again with auth');
694694
await this.initAllServers(); // start all servers again with "auth" enabled
695+
await this._waitForPrimary(); // wait for a primary to come around, because otherwise server selection may time out, this may take more than 30s
695696

696-
con = await MongoClient.connect(this.getUri('admin'), {
697+
con = await MongoClient.connect(this.getUri(), {
697698
authSource: 'admin',
698699
authMechanism: 'SCRAM-SHA-256',
699700
auth: {
@@ -728,6 +729,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
728729
this.stateChange(MongoMemoryReplSetStates.running);
729730
log('_initReplSet: running');
730731
} finally {
732+
log('_initReplSet: finally closing connection');
731733
await con.close();
732734
}
733735
}

0 commit comments

Comments
 (0)