@@ -8,9 +8,10 @@ import {
8
8
type Document ,
9
9
type MongoClient ,
10
10
MongoOperationTimeoutError ,
11
- MongoServerError
11
+ MongoServerError ,
12
+ now
12
13
} from '../../mongodb' ;
13
- import { clearFailPoint , configureFailPoint } from '../../tools/utils' ;
14
+ import { clearFailPoint , configureFailPoint , measureDuration } from '../../tools/utils' ;
14
15
import { filterForCommands } from '../shared' ;
15
16
16
17
const explain = [ true , false , 'queryPlanner' , 'allPlansExecution' , 'executionStats' , 'invalid' ] ;
@@ -334,10 +335,14 @@ describe('CRUD API explain option', function () {
334
335
335
336
describe ( 'when a cursor api is being explained' , function ( ) {
336
337
describe ( 'when timeoutMS is provided' , function ( ) {
337
- test ( 'the explain command respects timeoutMS' , async function ( ) {
338
+ test ( 'the explain command times out after timeoutMS' , async function ( ) {
338
339
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . find ( { } , { timeoutMS : 1000 } ) ;
339
- const timeout = await cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e ) ;
340
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
340
+ const { duration, result } = await measureDuration ( ( ) =>
341
+ cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e )
342
+ ) ;
343
+
344
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
345
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
341
346
} ) ;
342
347
343
348
test ( 'the explain command has the calculated maxTimeMS value attached' , async function ( ) {
@@ -384,19 +389,23 @@ describe('CRUD API explain option', function () {
384
389
385
390
describe ( 'when a non-cursor api is being explained' , function ( ) {
386
391
describe ( 'when timeoutMS is provided' , function ( ) {
387
- test ( 'the explain command respects timeoutMS' , async function ( ) {
388
- const timeout = await client
389
- . db ( 'foo' )
390
- . collection ( 'bar' )
391
- . deleteMany (
392
- { } ,
393
- {
394
- timeoutMS : 1000 ,
395
- explain : { verbosity : 'queryPlanner' }
396
- }
397
- )
398
- . catch ( e => e ) ;
399
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
392
+ test ( 'the explain command times out after timeoutMS' , async function ( ) {
393
+ const { duration, result } = await measureDuration ( ( ) =>
394
+ client
395
+ . db ( 'foo' )
396
+ . collection ( 'bar' )
397
+ . deleteMany (
398
+ { } ,
399
+ {
400
+ timeoutMS : 1000 ,
401
+ explain : { verbosity : 'queryPlanner' }
402
+ }
403
+ )
404
+ . catch ( e => e )
405
+ ) ;
406
+
407
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
408
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
400
409
} ) ;
401
410
402
411
test ( 'the explain command has the calculated maxTimeMS value attached' , async function ( ) {
@@ -512,56 +521,70 @@ describe('CRUD API explain option', function () {
512
521
describe ( 'find({}, { timeoutMS }).explain()' , function ( ) {
513
522
test ( 'respects the timeoutMS from the find options' , async function ( ) {
514
523
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . find ( { } , { timeoutMS : 1000 } ) ;
515
- const timeout = await cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e ) ;
516
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
524
+
525
+ const { result, duration } = await measureDuration ( ( ) =>
526
+ cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e )
527
+ ) ;
528
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
529
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
517
530
} ) ;
518
531
} ) ;
519
532
520
533
describe ( 'find().explain({}, { timeoutMS })' , function ( ) {
521
534
test ( 'respects the timeoutMS from the explain helper' , async function ( ) {
522
535
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . find ( ) ;
523
- const timeout = await cursor
524
- . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } )
525
- . catch ( e => e ) ;
526
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
536
+
537
+ const { result, duration } = await measureDuration ( ( ) =>
538
+ cursor . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } ) . catch ( e => e )
539
+ ) ;
540
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
541
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
527
542
} ) ;
528
543
} ) ;
529
544
530
545
describe ( 'find({}, { timeoutMS} ).explain({}, { timeoutMS })' , function ( ) {
531
546
test ( 'the timeoutMS from the explain helper has precedence' , async function ( ) {
532
547
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . find ( { } , { timeoutMS : 2000 } ) ;
533
- const timeout = await cursor
534
- . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } )
535
- . catch ( e => e ) ;
536
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
548
+ const { result, duration } = await measureDuration ( ( ) =>
549
+ cursor . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } ) . catch ( e => e )
550
+ ) ;
551
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
552
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
537
553
} ) ;
538
554
} ) ;
539
555
540
556
describe ( 'aggregate([], { timeoutMS }).explain()' , function ( ) {
541
557
test ( 'respects the timeoutMS from the find options' , async function ( ) {
542
558
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . aggregate ( [ ] , { timeoutMS : 1000 } ) ;
543
- const timeout = await cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e ) ;
544
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
559
+ const { result, duration } = await measureDuration ( ( ) =>
560
+ cursor . explain ( { verbosity : 'queryPlanner' } ) . catch ( e => e )
561
+ ) ;
562
+
563
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
564
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
545
565
} ) ;
546
566
} ) ;
547
567
548
568
describe ( 'aggregate([], { timeoutMS })' , function ( ) {
549
569
test ( 'respects the timeoutMS from the explain helper' , async function ( ) {
550
570
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . aggregate ( ) ;
551
- const timeout = await cursor
552
- . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } )
553
- . catch ( e => e ) ;
554
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
571
+ const { result, duration } = await measureDuration ( ( ) =>
572
+ cursor . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } ) . catch ( e => e )
573
+ ) ;
574
+
575
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
576
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
555
577
} ) ;
556
578
} ) ;
557
579
558
580
describe ( 'aggregate([], { timeoutMS} ).explain({}, { timeoutMS })' , function ( ) {
559
581
test ( 'the timeoutMS from the explain helper has precedence' , async function ( ) {
560
582
const cursor = client . db ( 'foo' ) . collection ( 'bar' ) . aggregate ( [ ] , { timeoutMS : 2000 } ) ;
561
- const timeout = await cursor
562
- . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } )
563
- . catch ( e => e ) ;
564
- expect ( timeout ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
583
+ const { duration, result } = await measureDuration ( ( ) =>
584
+ cursor . explain ( { verbosity : 'queryPlanner' } , { timeoutMS : 1000 } ) . catch ( e => e )
585
+ ) ;
586
+ expect ( duration ) . to . be . within ( 1000 , 1000 + 100 ) ;
587
+ expect ( result ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
565
588
} ) ;
566
589
} ) ;
567
590
} ) ;
0 commit comments