11import { expect } from 'chai' ;
22import { setTimeout } from 'timers/promises' ;
3+ import { inspect } from 'util' ;
34
45import {
56 type CommandStartedEvent ,
67 type Connection ,
78 type ConnectionPool ,
89 type MongoClient ,
10+ MongoError ,
911 MongoOperationTimeoutError ,
12+ now ,
1013 TimeoutContext
1114} from '../../mongodb' ;
1215import {
@@ -45,6 +48,7 @@ describe('Client Bulk Write', function () {
4548 } ) ;
4649
4750 it ( 'timeoutMS is used as the timeout for the bulk write' , metadata , async function ( ) {
51+ const start = now ( ) ;
4852 const timeoutError = await client
4953 . bulkWrite ( [
5054 {
@@ -54,7 +58,9 @@ describe('Client Bulk Write', function () {
5458 }
5559 ] )
5660 . 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 ) ;
5864 } ) ;
5965 } ) ;
6066
@@ -72,6 +78,7 @@ describe('Client Bulk Write', function () {
7278 } ) ;
7379
7480 it ( 'timeoutMS is used as the timeout for the bulk write' , metadata , async function ( ) {
81+ const start = now ( ) ;
7582 const timeoutError = await client
7683 . bulkWrite (
7784 [
@@ -84,7 +91,9 @@ describe('Client Bulk Write', function () {
8491 { timeoutMS : 300 }
8592 )
8693 . 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 ) ;
8897 } ) ;
8998 } ) ;
9099
@@ -102,6 +111,7 @@ describe('Client Bulk Write', function () {
102111 } ) ;
103112
104113 it ( 'bulk write options take precedence over the client options' , metadata , async function ( ) {
114+ const start = now ( ) ;
105115 const timeoutError = await client
106116 . bulkWrite (
107117 [
@@ -114,7 +124,9 @@ describe('Client Bulk Write', function () {
114124 { timeoutMS : 300 }
115125 )
116126 . 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 ) ;
118130 } ) ;
119131 } ) ;
120132
@@ -150,7 +162,9 @@ describe('Client Bulk Write', function () {
150162 await client . close ( ) ;
151163 } ) ;
152164
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 ;
154168 const timeoutError = client
155169 . bulkWrite (
156170 [
@@ -162,12 +176,16 @@ describe('Client Bulk Write', function () {
162176 ] ,
163177 { timeoutMS : 200 , writeConcern : { w : 0 } }
164178 )
165- . catch ( e => e ) ;
179+ . catch ( e => e )
180+ . then ( e => {
181+ end = now ( ) ;
182+ return e ;
183+ } ) ;
166184
167185 await setTimeout ( 250 ) ;
168186
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 ) ;
171189 } ) ;
172190
173191 it (
@@ -180,12 +198,18 @@ describe('Client Bulk Write', function () {
180198 } ,
181199 async function ( ) {
182200 const models = await makeMultiBatchWrite ( this . configuration ) ;
201+ const start = now ( ) ;
202+ let end ;
183203 const timeoutError = client
184204 . bulkWrite ( models , {
185205 timeoutMS : 400 ,
186206 writeConcern : { w : 0 }
187207 } )
188- . catch ( e => e ) ;
208+ . catch ( e => e )
209+ . then ( r => {
210+ end = now ( ) ;
211+ return r ;
212+ } ) ;
189213
190214 await setTimeout ( 210 ) ;
191215
@@ -199,8 +223,8 @@ describe('Client Bulk Write', function () {
199223
200224 await setTimeout ( 210 ) ;
201225
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 ) ;
204228 }
205229 ) ;
206230 }
@@ -221,6 +245,7 @@ describe('Client Bulk Write', function () {
221245 } ) ;
222246
223247 it ( 'the operation times out' , metadata , async function ( ) {
248+ const start = now ( ) ;
224249 const timeoutError = await client
225250 . bulkWrite (
226251 [
@@ -233,7 +258,9 @@ describe('Client Bulk Write', function () {
233258 { timeoutMS : 300 }
234259 )
235260 . 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 ) ;
237264 } ) ;
238265 } ) ;
239266
@@ -242,7 +269,7 @@ describe('Client Bulk Write', function () {
242269
243270 beforeEach ( async function ( ) {
244271 client = this . configuration . newClient ( { } , { monitorCommands : true } ) ;
245- client . on ( 'commandStarted' , filterForCommands ( 'getMore' , commands ) ) ;
272+ client . on ( 'commandStarted' , filterForCommands ( [ 'getMore' ] , commands ) ) ;
246273 await client . connect ( ) ;
247274
248275 await configureFailPoint ( this . configuration , {
@@ -254,14 +281,19 @@ describe('Client Bulk Write', function () {
254281
255282 it ( 'the bulk write operation times out' , metadata , async function ( ) {
256283 const models = await makeMultiResponseBatchModelArray ( this . configuration ) ;
284+ const start = now ( ) ;
257285 const timeoutError = await client
258286 . bulkWrite ( models , {
259287 verboseResults : true ,
260288 timeoutMS : 1500
261289 } )
262290 . catch ( e => e ) ;
263291
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 ) ;
265297 expect ( commands ) . to . have . lengthOf ( 1 ) ;
266298 } ) ;
267299 } ) ;
@@ -333,13 +365,16 @@ describe('Client Bulk Write', function () {
333365 } ,
334366 async function ( ) {
335367 const models = await makeMultiBatchWrite ( this . configuration ) ;
368+ const start = now ( ) ;
336369 const timeoutError = await client
337370 . bulkWrite ( models , {
338371 timeoutMS : 2000
339372 } )
340373 . catch ( e => e ) ;
341- expect ( timeoutError , timeoutError . stack ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
342374
375+ const end = now ( ) ;
376+ expect ( timeoutError ) . to . be . instanceOf ( MongoError ) ;
377+ expect ( end - start ) . to . be . within ( 2000 - 100 , 2000 + 100 ) ;
343378 expect ( commands . length , 'Test must execute two batches.' ) . to . equal ( 2 ) ;
344379 }
345380 ) ;
0 commit comments