1
- 'use strict' ;
1
+ import { EJSON } from 'bson' ;
2
+ import { expect } from 'chai' ;
3
+ import util from 'util' ;
2
4
3
- const { Logger } = require ( '../../src/logger' ) ;
4
- const { deprecateOptions } = require ( '../../src/utils' ) ;
5
- const util = require ( 'util' ) ;
6
- const chai = require ( 'chai' ) ;
5
+ import { Logger } from '../../src/logger' ;
6
+ import { deprecateOptions , DeprecateOptionsConfig } from '../../src/utils' ;
7
7
8
- const expect = chai . expect ;
9
- const sinonChai = require ( 'sinon-chai' ) ;
10
- const { EJSON } = require ( 'bson' ) ;
11
-
12
- chai . use ( sinonChai ) ;
13
-
14
- function makeTestFunction ( config ) {
15
- const fn = options => {
8
+ export function makeTestFunction ( config : DeprecateOptionsConfig ) {
9
+ const fn = ( options : any ) => {
16
10
if ( options ) options = null ;
17
11
} ;
18
12
return deprecateOptions ( config , fn ) ;
19
13
}
20
14
21
- function ensureCalledWith ( stub , args ) {
22
- args . forEach ( m => expect ( stub ) . to . have . been . calledWith ( m ) ) ;
15
+ export function ensureCalledWith ( stub : any , args : any [ ] ) {
16
+ args . forEach ( ( m : any ) => expect ( stub ) . to . have . been . calledWith ( m ) ) ;
23
17
}
24
18
25
19
// creation of class with a logger
26
- function ClassWithLogger ( ) {
20
+ export function ClassWithLogger ( ) {
27
21
this . logger = new Logger ( 'ClassWithLogger' ) ;
28
22
}
29
23
@@ -38,7 +32,9 @@ ClassWithLogger.prototype.getLogger = function () {
38
32
} ;
39
33
40
34
// creation of class without a logger
41
- function ClassWithoutLogger ( ) { }
35
+ export function ClassWithoutLogger ( ) {
36
+ // empty function for class
37
+ }
42
38
43
39
ClassWithoutLogger . prototype . f = makeTestFunction ( {
44
40
name : 'f' ,
@@ -47,7 +43,9 @@ ClassWithoutLogger.prototype.f = makeTestFunction({
47
43
} ) ;
48
44
49
45
// creation of class where getLogger returns undefined
50
- function ClassWithUndefinedLogger ( ) { }
46
+ export function ClassWithUndefinedLogger ( ) {
47
+ // empty function for class
48
+ }
51
49
52
50
ClassWithUndefinedLogger . prototype . f = makeTestFunction ( {
53
51
name : 'f' ,
@@ -59,18 +57,24 @@ ClassWithUndefinedLogger.prototype.getLogger = function () {
59
57
return undefined ;
60
58
} ;
61
59
62
- class EventCollector {
63
- constructor ( obj , events , options ) {
60
+ export class EventCollector {
61
+ private _events : Record < string , any [ ] > ;
62
+ private _timeout : number ;
63
+ constructor (
64
+ obj : { on : ( arg0 : any , arg1 : ( event : any ) => number ) => void } ,
65
+ events : any [ ] ,
66
+ options : { timeout : number }
67
+ ) {
64
68
this . _events = Object . create ( null ) ;
65
69
this . _timeout = options ? options . timeout : 5000 ;
66
70
67
- events . forEach ( eventName => {
71
+ events . forEach ( ( eventName : string | number ) => {
68
72
this . _events [ eventName ] = [ ] ;
69
- obj . on ( eventName , event => this . _events [ eventName ] . push ( event ) ) ;
73
+ obj . on ( eventName , ( event : any ) => this . _events [ eventName ] . push ( event ) ) ;
70
74
} ) ;
71
75
}
72
76
73
- waitForEvent ( eventName , count , callback ) {
77
+ waitForEvent ( eventName : any , count : number , callback : any ) {
74
78
if ( typeof count === 'function' ) {
75
79
callback = count ;
76
80
count = 1 ;
@@ -82,23 +86,20 @@ class EventCollector {
82
86
/**
83
87
* Will only return one event at a time from the front of the list
84
88
* Useful for iterating over the events in the order they occurred
85
- *
86
- * @param {string } eventName
87
- * @returns {Promise<Record<string, any>> }
88
89
*/
89
- waitAndShiftEvent ( eventName ) {
90
- return new Promise ( ( resolve , reject ) => {
90
+ waitAndShiftEvent ( eventName : string ) : Promise < Record < string , any > > {
91
+ return new Promise < Record < string , any > > ( ( resolve , reject ) => {
91
92
if ( this . _events [ eventName ] . length > 0 ) {
92
93
return resolve ( this . _events [ eventName ] . shift ( ) ) ;
93
94
}
94
- this . waitForEventImpl ( this , Date . now ( ) , eventName , 1 , error => {
95
+ this . waitForEventImpl ( this , Date . now ( ) , eventName , 1 , ( error : any ) => {
95
96
if ( error ) return reject ( error ) ;
96
97
resolve ( this . _events [ eventName ] . shift ( ) ) ;
97
98
} ) ;
98
99
} ) ;
99
100
}
100
101
101
- reset ( eventName ) {
102
+ reset ( eventName : string ) {
102
103
if ( eventName == null ) {
103
104
Object . keys ( this . _events ) . forEach ( eventName => {
104
105
this . _events [ eventName ] = [ ] ;
@@ -114,7 +115,13 @@ class EventCollector {
114
115
this . _events [ eventName ] = [ ] ;
115
116
}
116
117
117
- waitForEventImpl ( collector , start , eventName , count , callback ) {
118
+ waitForEventImpl (
119
+ collector : this,
120
+ start : number ,
121
+ eventName : string | number ,
122
+ count : number ,
123
+ callback : ( error ?: Error , events ?: any [ ] ) => void
124
+ ) {
118
125
const events = collector . _events [ eventName ] ;
119
126
if ( events . length >= count ) {
120
127
return callback ( undefined , events ) ;
@@ -128,7 +135,7 @@ class EventCollector {
128
135
}
129
136
}
130
137
131
- function getSymbolFrom ( target , symbolName , assertExists = true ) {
138
+ export function getSymbolFrom ( target : any , symbolName : any , assertExists = true ) {
132
139
const symbol = Object . getOwnPropertySymbols ( target ) . filter (
133
140
s => s . toString ( ) === `Symbol(${ symbolName } )`
134
141
) [ 0 ] ;
@@ -140,7 +147,7 @@ function getSymbolFrom(target, symbolName, assertExists = true) {
140
147
return symbol ;
141
148
}
142
149
143
- function getEnvironmentalOptions ( ) {
150
+ export function getEnvironmentalOptions ( ) {
144
151
const options = { } ;
145
152
if ( process . env . MONGODB_API_VERSION ) {
146
153
Object . assign ( options , {
@@ -160,7 +167,7 @@ function getEnvironmentalOptions() {
160
167
return options ;
161
168
}
162
169
163
- function shouldRunServerlessTest ( testRequirement , isServerless ) {
170
+ export function shouldRunServerlessTest ( testRequirement : any , isServerless : any ) {
164
171
if ( ! testRequirement ) return true ;
165
172
switch ( testRequirement ) {
166
173
case 'forbid' :
@@ -182,11 +189,11 @@ function shouldRunServerlessTest(testRequirement, isServerless) {
182
189
* Attempts to use EJSON (to make type information obvious)
183
190
* falls back to util.inspect if there's an error (circular reference)
184
191
*/
185
- function ejson ( strings , ...values ) {
192
+ export function ejson ( strings : any [ ] , ...values : any [ ] ) {
186
193
const stringParts = [ strings [ 0 ] ] ;
187
194
for ( const [ idx , value ] of values . entries ( ) ) {
188
195
if ( typeof value === 'object' ) {
189
- let stringifiedObject ;
196
+ let stringifiedObject : string ;
190
197
try {
191
198
stringifiedObject = EJSON . stringify ( value , { relaxed : false } ) ;
192
199
} catch ( error ) {
@@ -208,25 +215,24 @@ function ejson(strings, ...values) {
208
215
209
216
/**
210
217
* Run an async function after some set timeout
211
- * @param {() => Promise<void> } fn - function to run
212
- * @param {number } ms - timeout in MS
213
- * @returns {Promise<void> }
218
+ * @param fn - function to run
219
+ * @param ms - timeout in MS
214
220
*/
215
- const runLater = ( fn , ms ) => {
216
- return new Promise ( ( resolve , reject ) => {
221
+ export const runLater = ( fn : ( ) => Promise < void > , ms : number ) => {
222
+ return new Promise < void > ( ( resolve , reject ) => {
217
223
setTimeout ( ( ) => fn ( ) . then ( resolve ) . catch ( reject ) , ms ) ;
218
224
} ) ;
219
225
} ;
220
226
221
- const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
227
+ export const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
222
228
223
229
/**
224
230
* If you are using sinon fake timers, it can end up blocking queued IO from running
225
231
* awaiting a nextTick call will allow the event loop to process Networking/FS callbacks
226
232
*/
227
- const processTick = ( ) => new Promise ( resolve => process . nextTick ( resolve ) ) ;
233
+ export const processTick = ( ) => new Promise ( resolve => process . nextTick ( resolve ) ) ;
228
234
229
- function getIndicesOfAuthInUrl ( connectionString ) {
235
+ export function getIndicesOfAuthInUrl ( connectionString : string | string [ ] ) {
230
236
const doubleSlashIndex = connectionString . indexOf ( '//' ) ;
231
237
const atIndex = connectionString . indexOf ( '@' ) ;
232
238
@@ -240,7 +246,7 @@ function getIndicesOfAuthInUrl(connectionString) {
240
246
} ;
241
247
}
242
248
243
- function removeAuthFromConnectionString ( connectionString ) {
249
+ export function removeAuthFromConnectionString ( connectionString : string ) {
244
250
const indices = getIndicesOfAuthInUrl ( connectionString ) ;
245
251
if ( ! indices ) {
246
252
return connectionString ;
@@ -255,7 +261,7 @@ function removeAuthFromConnectionString(connectionString) {
255
261
return connectionString . slice ( 0 , start ) + connectionString . slice ( end + 1 ) ;
256
262
}
257
263
258
- function extractAuthFromConnectionString ( connectionString ) {
264
+ export function extractAuthFromConnectionString ( connectionString : string | any [ ] ) {
259
265
const indices = getIndicesOfAuthInUrl ( connectionString ) ;
260
266
if ( ! indices ) {
261
267
return null ;
@@ -264,20 +270,19 @@ function extractAuthFromConnectionString(connectionString) {
264
270
return connectionString . slice ( indices . start , indices . end ) ;
265
271
}
266
272
267
- module . exports = {
268
- processTick,
269
- sleep,
270
- runLater,
271
- ejson,
272
- EventCollector,
273
- makeTestFunction,
274
- ensureCalledWith,
275
- ClassWithLogger,
276
- ClassWithoutLogger,
277
- ClassWithUndefinedLogger,
278
- getSymbolFrom,
279
- getEnvironmentalOptions,
280
- shouldRunServerlessTest,
281
- removeAuthFromConnectionString,
282
- extractAuthFromConnectionString
283
- } ;
273
+ export interface FailPoint {
274
+ configureFailPoint : 'failCommand' ;
275
+ mode : { activationProbability : number } | { times : number } | 'alwaysOn' | 'off' ;
276
+ data : {
277
+ failCommands : string [ ] ;
278
+ errorCode ?: number ;
279
+ closeConnection ?: boolean ;
280
+ blockConnection ?: boolean ;
281
+ blockTimeMS ?: number ;
282
+ writeConcernError ?: { code : number ; errmsg : string } ;
283
+ threadName ?: string ;
284
+ failInternalCommands ?: boolean ;
285
+ errorLabels ?: string [ ] ;
286
+ appName ?: string ;
287
+ } ;
288
+ }
0 commit comments