@@ -9,11 +9,18 @@ import type {
9
9
} from '../../../src/cmap/events' ;
10
10
import { patchCollectionOptions , patchDbOptions } from './unified-utils' ;
11
11
import { TestConfiguration } from './unified.test' ;
12
+ import { expect } from 'chai' ;
13
+
14
+ interface UnifiedChangeStream extends ChangeStream {
15
+ eventCollector : InstanceType < typeof import ( '../../tools/utils' ) [ 'EventCollector' ] > ;
16
+ }
12
17
13
18
export type CommandEvent = CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent ;
14
19
15
20
export class UnifiedMongoClient extends MongoClient {
16
21
events : CommandEvent [ ] ;
22
+ failPoints : Document [ ] ;
23
+ ignoredEvents : string [ ] ;
17
24
observedEvents : ( 'commandStarted' | 'commandSucceeded' | 'commandFailed' ) [ ] ;
18
25
19
26
static EVENT_NAME_LOOKUP = {
@@ -25,6 +32,11 @@ export class UnifiedMongoClient extends MongoClient {
25
32
constructor ( url : string , description : ClientEntity ) {
26
33
super ( url , { monitorCommands : true , ...description . uriOptions } ) ;
27
34
this . events = [ ] ;
35
+ this . failPoints = [ ] ;
36
+ this . ignoredEvents = [
37
+ ...( description . ignoreCommandMonitoringEvents ?? [ ] ) ,
38
+ 'configureFailPoint'
39
+ ] ;
28
40
// apm
29
41
this . observedEvents = ( description . observeEvents ?? [ ] ) . map (
30
42
e => UnifiedMongoClient . EVENT_NAME_LOOKUP [ e ]
@@ -34,9 +46,11 @@ export class UnifiedMongoClient extends MongoClient {
34
46
}
35
47
}
36
48
37
- // NOTE: this must be an arrow function for `this` to work.
49
+ // NOTE: pushEvent must be an arrow function
38
50
pushEvent : ( e : CommandEvent ) => void = e => {
39
- this . events . push ( e ) ;
51
+ if ( ! this . ignoredEvents . includes ( e . commandName ) ) {
52
+ this . events . push ( e ) ;
53
+ }
40
54
} ;
41
55
42
56
/** Disables command monitoring for the client and returns a list of the captured events. */
@@ -46,14 +60,33 @@ export class UnifiedMongoClient extends MongoClient {
46
60
}
47
61
return this . events ;
48
62
}
63
+
64
+ async enableFailPoint ( failPoint : Document ) : Promise < Document > {
65
+ const admin = this . db ( ) . admin ( ) ;
66
+ const result = await admin . command ( failPoint ) ;
67
+ expect ( result ) . to . have . property ( 'ok' , 1 ) ;
68
+ this . failPoints . push ( failPoint . configureFailPoint ) ;
69
+ return result ;
70
+ }
71
+
72
+ async disableFailPoints ( ) : Promise < Document [ ] > {
73
+ return Promise . all (
74
+ this . failPoints . map ( configureFailPoint =>
75
+ this . db ( ) . admin ( ) . command ( {
76
+ configureFailPoint,
77
+ mode : 'off'
78
+ } )
79
+ )
80
+ ) ;
81
+ }
49
82
}
50
83
51
84
export type Entity =
52
85
| UnifiedMongoClient
53
86
| Db
54
87
| Collection
55
88
| ClientSession
56
- | ChangeStream
89
+ | UnifiedChangeStream
57
90
| GridFSBucket
58
91
| Document ; // Results from operations
59
92
@@ -81,7 +114,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
81
114
mapOf ( type : 'collection' ) : EntitiesMap < Collection > ;
82
115
mapOf ( type : 'session' ) : EntitiesMap < ClientSession > ;
83
116
mapOf ( type : 'bucket' ) : EntitiesMap < GridFSBucket > ;
84
- mapOf ( type : 'stream' ) : EntitiesMap < ChangeStream > ;
117
+ mapOf ( type : 'stream' ) : EntitiesMap < UnifiedChangeStream > ;
85
118
mapOf ( type : EntityTypeId ) : EntitiesMap < Entity > {
86
119
const ctor = ENTITY_CTORS . get ( type ) ;
87
120
if ( ! ctor ) {
@@ -95,7 +128,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
95
128
getEntity ( type : 'collection' , key : string , assertExists ?: boolean ) : Collection ;
96
129
getEntity ( type : 'session' , key : string , assertExists ?: boolean ) : ClientSession ;
97
130
getEntity ( type : 'bucket' , key : string , assertExists ?: boolean ) : GridFSBucket ;
98
- getEntity ( type : 'stream' , key : string , assertExists ?: boolean ) : ChangeStream ;
131
+ getEntity ( type : 'stream' , key : string , assertExists ?: boolean ) : UnifiedChangeStream ;
99
132
getEntity ( type : EntityTypeId , key : string , assertExists = true ) : Entity {
100
133
const entity = this . get ( key ) ;
101
134
if ( ! entity ) {
@@ -114,6 +147,7 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
114
147
115
148
async cleanup ( ) : Promise < void > {
116
149
for ( const [ , client ] of this . mapOf ( 'client' ) ) {
150
+ await client . disableFailPoints ( ) ;
117
151
await client . close ( ) ;
118
152
}
119
153
for ( const [ , session ] of this . mapOf ( 'session' ) ) {
0 commit comments