@@ -8,6 +8,7 @@ import * as sinon from 'sinon';
88import {
99 type AbstractCursor ,
1010 AggregationCursor ,
11+ Code ,
1112 type Collection ,
1213 Connection ,
1314 type Db ,
@@ -562,6 +563,48 @@ describe('AbortSignal support', () => {
562563 } ) ;
563564 } ) ;
564565
566+ describe . only ( 'cursor $where example' , ( ) => {
567+ beforeEach ( async function ( ) {
568+ const utilClient = this . configuration . newClient ( ) ;
569+ try {
570+ const collection = utilClient . db ( 'abortSignal' ) . collection ( 'support' ) ;
571+ await collection . drop ( { } ) . catch ( ( ) => null ) ;
572+ await collection . insertMany ( [
573+ { a : 1 , ssn : '0000-00-0001' } ,
574+ { a : 2 , ssn : '0000-00-0002' } ,
575+ { a : 3 , ssn : '0000-00-0003' }
576+ ] ) ;
577+ } finally {
578+ await utilClient . close ( ) ;
579+ }
580+ } ) ;
581+
582+ it ( 'follows expected stream error handling' , async ( ) => {
583+ const controller = new AbortController ( ) ;
584+ const { signal } = controller ;
585+ const cursor = collection . find (
586+ {
587+ $where : function ( ) {
588+ while ( true ) ;
589+ }
590+ } ,
591+ { signal, batchSize : 1 , serializeFunctions : true }
592+ ) ;
593+
594+ client . on (
595+ 'commandStarted' ,
596+ ev => ev . commandName === 'find' && sleep ( 2 ) . then ( ( ) => controller . abort ( ) )
597+ ) ;
598+
599+ const start = performance . now ( ) ;
600+ const result = await cursor . toArray ( ) . catch ( error => error ) ;
601+ const end = performance . now ( ) ;
602+ expect ( end - start ) . to . be . lessThan ( 10 ) ;
603+
604+ expect ( result ) . to . be . instanceOf ( DOMException ) ;
605+ } ) ;
606+ } ) ;
607+
565608 describe ( 'when auto connecting and the signal aborts' , ( ) => {
566609 let client : MongoClient ;
567610 let db : Db ;
0 commit comments