1
1
import { expect } from 'chai' ;
2
2
import { setTimeout } from 'timers/promises' ;
3
+ import { inspect } from 'util' ;
3
4
4
5
import {
5
6
type CommandStartedEvent ,
6
7
type Connection ,
7
8
type ConnectionPool ,
8
9
type MongoClient ,
10
+ MongoError ,
9
11
MongoOperationTimeoutError ,
12
+ now ,
10
13
TimeoutContext
11
14
} from '../../mongodb' ;
12
15
import {
@@ -45,6 +48,7 @@ describe('Client Bulk Write', function () {
45
48
} ) ;
46
49
47
50
it ( 'timeoutMS is used as the timeout for the bulk write' , metadata , async function ( ) {
51
+ const start = now ( ) ;
48
52
const timeoutError = await client
49
53
. bulkWrite ( [
50
54
{
@@ -54,7 +58,9 @@ describe('Client Bulk Write', function () {
54
58
}
55
59
] )
56
60
. catch ( e => e ) ;
57
- expect ( timeoutError ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
61
+ const end = now ( ) ;
62
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
63
+ expect ( end - start ) . to . be . within ( 300 - 100 , 300 + 100 ) ;
58
64
} ) ;
59
65
} ) ;
60
66
@@ -72,6 +78,7 @@ describe('Client Bulk Write', function () {
72
78
} ) ;
73
79
74
80
it ( 'timeoutMS is used as the timeout for the bulk write' , metadata , async function ( ) {
81
+ const start = now ( ) ;
75
82
const timeoutError = await client
76
83
. bulkWrite (
77
84
[
@@ -84,7 +91,9 @@ describe('Client Bulk Write', function () {
84
91
{ timeoutMS : 300 }
85
92
)
86
93
. catch ( e => e ) ;
87
- expect ( timeoutError ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
94
+ const end = now ( ) ;
95
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
96
+ expect ( end - start ) . to . be . within ( 300 - 100 , 300 + 100 ) ;
88
97
} ) ;
89
98
} ) ;
90
99
@@ -102,6 +111,7 @@ describe('Client Bulk Write', function () {
102
111
} ) ;
103
112
104
113
it ( 'bulk write options take precedence over the client options' , metadata , async function ( ) {
114
+ const start = now ( ) ;
105
115
const timeoutError = await client
106
116
. bulkWrite (
107
117
[
@@ -114,7 +124,9 @@ describe('Client Bulk Write', function () {
114
124
{ timeoutMS : 300 }
115
125
)
116
126
. catch ( e => e ) ;
117
- expect ( timeoutError ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
127
+ const end = now ( ) ;
128
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
129
+ expect ( end - start ) . to . be . within ( 300 - 100 , 300 + 100 ) ;
118
130
} ) ;
119
131
} ) ;
120
132
@@ -150,7 +162,9 @@ describe('Client Bulk Write', function () {
150
162
await client . close ( ) ;
151
163
} ) ;
152
164
153
- it ( 'respects timeoutMS for a single batch' , async function ( ) {
165
+ it ( 'a single batch bulk write does not take longer than timeoutMS' , async function ( ) {
166
+ const start = now ( ) ;
167
+ let end ;
154
168
const timeoutError = client
155
169
. bulkWrite (
156
170
[
@@ -162,12 +176,16 @@ describe('Client Bulk Write', function () {
162
176
] ,
163
177
{ timeoutMS : 200 , writeConcern : { w : 0 } }
164
178
)
165
- . catch ( e => e ) ;
179
+ . catch ( e => e )
180
+ . then ( e => {
181
+ end = now ( ) ;
182
+ return e ;
183
+ } ) ;
166
184
167
185
await setTimeout ( 250 ) ;
168
186
169
- const error = await timeoutError ;
170
- expect ( error ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
187
+ expect ( await timeoutError ) . to . be . instanceOf ( MongoError ) ;
188
+ expect ( end - start ) . to . be . within ( 200 - 100 , 200 + 100 ) ;
171
189
} ) ;
172
190
173
191
it (
@@ -180,12 +198,18 @@ describe('Client Bulk Write', function () {
180
198
} ,
181
199
async function ( ) {
182
200
const models = await makeMultiBatchWrite ( this . configuration ) ;
201
+ const start = now ( ) ;
202
+ let end ;
183
203
const timeoutError = client
184
204
. bulkWrite ( models , {
185
205
timeoutMS : 400 ,
186
206
writeConcern : { w : 0 }
187
207
} )
188
- . catch ( e => e ) ;
208
+ . catch ( e => e )
209
+ . then ( r => {
210
+ end = now ( ) ;
211
+ return r ;
212
+ } ) ;
189
213
190
214
await setTimeout ( 210 ) ;
191
215
@@ -199,8 +223,8 @@ describe('Client Bulk Write', function () {
199
223
200
224
await setTimeout ( 210 ) ;
201
225
202
- const error = await timeoutError ;
203
- expect ( error ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
226
+ expect ( await timeoutError ) . to . be . instanceOf ( MongoError ) ;
227
+ expect ( end - start ) . to . be . within ( 400 - 100 , 400 + 100 ) ;
204
228
}
205
229
) ;
206
230
}
@@ -221,6 +245,7 @@ describe('Client Bulk Write', function () {
221
245
} ) ;
222
246
223
247
it ( 'the operation times out' , metadata , async function ( ) {
248
+ const start = now ( ) ;
224
249
const timeoutError = await client
225
250
. bulkWrite (
226
251
[
@@ -233,7 +258,9 @@ describe('Client Bulk Write', function () {
233
258
{ timeoutMS : 300 }
234
259
)
235
260
. catch ( e => e ) ;
236
- expect ( timeoutError ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
261
+ const end = now ( ) ;
262
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
263
+ expect ( end - start ) . to . be . within ( 300 - 100 , 300 + 100 ) ;
237
264
} ) ;
238
265
} ) ;
239
266
@@ -242,7 +269,7 @@ describe('Client Bulk Write', function () {
242
269
243
270
beforeEach ( async function ( ) {
244
271
client = this . configuration . newClient ( { } , { monitorCommands : true } ) ;
245
- client . on ( 'commandStarted' , filterForCommands ( 'getMore' , commands ) ) ;
272
+ client . on ( 'commandStarted' , filterForCommands ( [ 'getMore' ] , commands ) ) ;
246
273
await client . connect ( ) ;
247
274
248
275
await configureFailPoint ( this . configuration , {
@@ -254,14 +281,19 @@ describe('Client Bulk Write', function () {
254
281
255
282
it ( 'the bulk write operation times out' , metadata , async function ( ) {
256
283
const models = await makeMultiResponseBatchModelArray ( this . configuration ) ;
284
+ const start = now ( ) ;
257
285
const timeoutError = await client
258
286
. bulkWrite ( models , {
259
287
verboseResults : true ,
260
288
timeoutMS : 1500
261
289
} )
262
290
. catch ( e => e ) ;
263
291
264
- expect ( timeoutError ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
292
+ const end = now ( ) ;
293
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
294
+
295
+ // DRIVERS-3005 - killCursors causes cursor cleanup to extend past timeoutMS.
296
+ expect ( end - start ) . to . be . within ( 2000 - 100 , 2000 + 100 ) ;
265
297
expect ( commands ) . to . have . lengthOf ( 1 ) ;
266
298
} ) ;
267
299
} ) ;
@@ -333,13 +365,16 @@ describe('Client Bulk Write', function () {
333
365
} ,
334
366
async function ( ) {
335
367
const models = await makeMultiBatchWrite ( this . configuration ) ;
368
+ const start = now ( ) ;
336
369
const timeoutError = await client
337
370
. bulkWrite ( models , {
338
371
timeoutMS : 2000
339
372
} )
340
373
. catch ( e => e ) ;
341
- expect ( timeoutError , timeoutError . stack ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
342
374
375
+ const end = now ( ) ;
376
+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
377
+ expect ( end - start ) . to . be . within ( 2000 - 100 , 2000 + 100 ) ;
343
378
expect ( commands . length , 'Test must execute two batches.' ) . to . equal ( 2 ) ;
344
379
}
345
380
) ;
0 commit comments