Skip to content

Commit 4746fa0

Browse files
committed
fixup: cr, export interfaces, fix auth test for 8.x
1 parent 04249ed commit 4746fa0

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

packages/mongodb-runner/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export {
88
type MongoClusterEvents,
99
MongoClusterOptions,
1010
MongoDBUserDoc,
11+
RSMemberOptions as MongoClusterRSMemberOptions,
12+
RSOptions as MongoClusterRSOptions,
13+
CommonOptions as MongoClusterCommonOptions,
14+
ShardedOptions as MongoClusterShardedOptions,
1115
} from './mongocluster';
1216
export type { LogEntry } from './mongologreader';
1317
export type { ConnectionString } from 'mongodb-connection-string-url';

packages/mongodb-runner/src/mongocluster.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { MongoServer } from './mongoserver';
33
import { ConnectionString } from 'mongodb-connection-string-url';
44
import type { DownloadOptions } from '@mongodb-js/mongodb-downloader';
55
import { downloadMongoDb } from '@mongodb-js/mongodb-downloader';
6-
import type { Document, MongoClientOptions, TagSet } from 'mongodb';
6+
import type {
7+
Document,
8+
MongoClientOptions,
9+
TagSet,
10+
WriteConcernSettings,
11+
} from 'mongodb';
712
import { MongoClient } from 'mongodb';
813
import {
914
sleep,
@@ -470,6 +475,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
470475
const admin = client.db('admin');
471476
for (const user of this.users) {
472477
const { username, password, ...rest } = user;
478+
debug('adding new user', { username, ...rest }, this.connectionString);
473479
await admin.command({ createUser: username, pwd: password, ...rest });
474480
}
475481
});
@@ -507,6 +513,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
507513
clientOptions: MongoClientOptions = {},
508514
): Promise<ReturnType<Fn>> {
509515
const client = await MongoClient.connect(this.connectionString, {
516+
...this.getFullWriteConcernOptions(),
510517
...this.defaultConnectionOptions,
511518
...clientOptions,
512519
});
@@ -524,4 +531,29 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
524531
unref(): void {
525532
for (const child of this.children()) child.unref();
526533
}
534+
535+
// Return maximal write concern options based on topology
536+
getFullWriteConcernOptions(): { writeConcern?: WriteConcernSettings } {
537+
switch (this.topology) {
538+
case 'standalone':
539+
return {};
540+
case 'replset':
541+
return { writeConcern: { w: this.servers.length, j: true } };
542+
case 'sharded':
543+
return {
544+
writeConcern: {
545+
w: Math.min(
546+
...this.shards
547+
.map((s) => s.getFullWriteConcernOptions().writeConcern?.w)
548+
.filter((w) => typeof w === 'number'),
549+
),
550+
j: true,
551+
},
552+
};
553+
default:
554+
throw new Error(
555+
`Not implemented for topology ${this.topology as string}`,
556+
);
557+
}
558+
}
527559
}

packages/mongodb-runner/src/mongoserver.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,20 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
289289
async updateDefaultConnectionOptions(
290290
options: Partial<MongoClientOptions>,
291291
): Promise<void> {
292-
const buildInfoError = await this._populateBuildInfo('restore-check', {
293-
...options,
294-
});
295-
if (buildInfoError) {
292+
let buildInfoError: Error | null = null;
293+
for (let attempts = 0; attempts < 10; attempts++) {
294+
buildInfoError = await this._populateBuildInfo('restore-check', {
295+
...options,
296+
});
297+
if (!buildInfoError) break;
296298
debug(
297299
'failed to get buildInfo when setting new options',
298300
buildInfoError,
299301
options,
302+
this.connectionString,
300303
);
301-
throw buildInfoError;
302304
}
305+
if (buildInfoError) throw buildInfoError;
303306
this.defaultConnectionOptions = {
304307
...this.defaultConnectionOptions,
305308
...options,

0 commit comments

Comments
 (0)