1- import { MongoClient , Db , Collection , GridFSBucket , Document } from '../../../src/index' ;
1+ import {
2+ MongoClient ,
3+ Db ,
4+ Collection ,
5+ GridFSBucket ,
6+ Document ,
7+ HostAddress
8+ } from '../../../src/index' ;
29import { ReadConcern } from '../../../src/read_concern' ;
310import { WriteConcern } from '../../../src/write_concern' ;
411import { ReadPreference } from '../../../src/read_preference' ;
@@ -18,10 +25,6 @@ interface UnifiedChangeStream extends ChangeStream {
1825 eventCollector : InstanceType < typeof import ( '../../tools/utils' ) [ 'EventCollector' ] > ;
1926}
2027
21- interface UnifiedClientSession extends ClientSession {
22- client : UnifiedMongoClient ;
23- }
24-
2528export type CommandEvent = CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent ;
2629
2730export class UnifiedMongoClient extends MongoClient {
@@ -75,23 +78,49 @@ export class UnifiedMongoClient extends MongoClient {
7578 }
7679 return this . events ;
7780 }
81+ }
7882
79- async enableFailPoint ( failPoint : Document ) : Promise < Document > {
80- const admin = this . db ( ) . admin ( ) ;
83+ export class FailPointMap extends Map < string , Document > {
84+ async enableFailPoint (
85+ addressOrClient : HostAddress | UnifiedMongoClient ,
86+ failPoint : Document
87+ ) : Promise < Document > {
88+ let client : MongoClient ;
89+ let address : string ;
90+ if ( addressOrClient instanceof MongoClient ) {
91+ client = addressOrClient ;
92+ address = client . topology . s . seedlist . join ( ',' ) ;
93+ } else {
94+ // create a new client
95+ address = addressOrClient . toString ( ) ;
96+ client = new MongoClient ( `mongodb://${ address } ` ) ;
97+ await client . connect ( ) ;
98+ }
99+
100+ const admin = client . db ( 'admin' ) ;
81101 const result = await admin . command ( failPoint ) ;
102+
103+ if ( ! ( addressOrClient instanceof MongoClient ) ) {
104+ // we created this client
105+ await client . close ( ) ;
106+ }
107+
82108 expect ( result ) . to . have . property ( 'ok' , 1 ) ;
83- this . failPoints . push ( failPoint . configureFailPoint ) ;
109+ this . set ( address , failPoint . configureFailPoint ) ;
84110 return result ;
85111 }
86112
87- async disableFailPoints ( ) : Promise < Document [ ] > {
88- return Promise . all (
89- this . failPoints . map ( configureFailPoint =>
90- this . db ( ) . admin ( ) . command ( {
91- configureFailPoint,
92- mode : 'off'
93- } )
94- )
113+ async disableFailPoints ( ) : Promise < void > {
114+ const entries = Array . from ( this . entries ( ) ) ;
115+ await Promise . all (
116+ entries . map ( async ( [ hostAddress , configureFailPoint ] ) => {
117+ const client = new MongoClient ( `mongodb://${ hostAddress } ` ) ;
118+ await client . connect ( ) ;
119+ const admin = client . db ( 'admin' ) ;
120+ const result = await admin . command ( { configureFailPoint, mode : 'off' } ) ;
121+ expect ( result ) . to . have . property ( 'ok' , 1 ) ;
122+ await client . close ( ) ;
123+ } )
95124 ) ;
96125 }
97126}
@@ -100,7 +129,7 @@ export type Entity =
100129 | UnifiedMongoClient
101130 | Db
102131 | Collection
103- | UnifiedClientSession
132+ | ClientSession
104133 | UnifiedChangeStream
105134 | GridFSBucket
106135 | Document ; // Results from operations
@@ -124,10 +153,17 @@ ENTITY_CTORS.set('bucket', GridFSBucket);
124153ENTITY_CTORS . set ( 'stream' , ChangeStream ) ;
125154
126155export class EntitiesMap < E = Entity > extends Map < string , E > {
156+ failPoints : FailPointMap ;
157+
158+ constructor ( entries ?: readonly ( readonly [ string , E ] ) [ ] | null ) {
159+ super ( entries ) ;
160+ this . failPoints = new FailPointMap ( ) ;
161+ }
162+
127163 mapOf ( type : 'client' ) : EntitiesMap < UnifiedMongoClient > ;
128164 mapOf ( type : 'db' ) : EntitiesMap < Db > ;
129165 mapOf ( type : 'collection' ) : EntitiesMap < Collection > ;
130- mapOf ( type : 'session' ) : EntitiesMap < UnifiedClientSession > ;
166+ mapOf ( type : 'session' ) : EntitiesMap < ClientSession > ;
131167 mapOf ( type : 'bucket' ) : EntitiesMap < GridFSBucket > ;
132168 mapOf ( type : 'stream' ) : EntitiesMap < UnifiedChangeStream > ;
133169 mapOf ( type : EntityTypeId ) : EntitiesMap < Entity > {
@@ -141,7 +177,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
141177 getEntity ( type : 'client' , key : string , assertExists ?: boolean ) : UnifiedMongoClient ;
142178 getEntity ( type : 'db' , key : string , assertExists ?: boolean ) : Db ;
143179 getEntity ( type : 'collection' , key : string , assertExists ?: boolean ) : Collection ;
144- getEntity ( type : 'session' , key : string , assertExists ?: boolean ) : UnifiedClientSession ;
180+ getEntity ( type : 'session' , key : string , assertExists ?: boolean ) : ClientSession ;
145181 getEntity ( type : 'bucket' , key : string , assertExists ?: boolean ) : GridFSBucket ;
146182 getEntity ( type : 'stream' , key : string , assertExists ?: boolean ) : UnifiedChangeStream ;
147183 getEntity ( type : EntityTypeId , key : string , assertExists = true ) : Entity {
@@ -161,8 +197,8 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
161197 }
162198
163199 async cleanup ( ) : Promise < void > {
200+ await this . failPoints . disableFailPoints ( ) ;
164201 for ( const [ , client ] of this . mapOf ( 'client' ) ) {
165- await client . disableFailPoints ( ) ;
166202 await client . close ( ) ;
167203 }
168204 for ( const [ , session ] of this . mapOf ( 'session' ) ) {
@@ -178,7 +214,9 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
178214 const map = new EntitiesMap ( ) ;
179215 for ( const entity of entities ?? [ ] ) {
180216 if ( 'client' in entity ) {
181- const uri = config . url ( { useMultipleMongoses : entity . client . useMultipleMongoses } ) ;
217+ const useMultipleMongoses =
218+ config . topologyType === 'Sharded' && entity . client . useMultipleMongoses ;
219+ const uri = config . url ( { useMultipleMongoses } ) ;
182220 const client = new UnifiedMongoClient ( uri , entity . client ) ;
183221 await client . connect ( ) ;
184222 map . set ( entity . client . id , client ) ;
@@ -228,10 +266,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
228266 }
229267 }
230268
231- const session = client . startSession ( options ) as UnifiedClientSession ;
232- // targetedFailPoint operations need to access the client the session came from
233- session . client = client ;
234-
269+ const session = client . startSession ( options ) ;
235270 map . set ( entity . session . id , session ) ;
236271 } else if ( 'bucket' in entity ) {
237272 const db = map . getEntity ( 'db' , entity . bucket . database ) ;
0 commit comments