@@ -30,69 +30,62 @@ describe('Cursor Streams', function () {
3030 await collection . insertMany ( docs , { writeConcern : { w : 1 } } ) ;
3131 }
3232
33- describe ( 'using Async Iterator (for await...of)' , function ( ) {
34- it ( 'should stream all documents correctly, triggering getMores' , async function ( ) {
35- const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
36- await setupCollection ( collection , 100 ) ;
33+ it ( 'should stream all documents and emit "end"' , async function ( ) {
34+ const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
35+ await setupCollection ( collection , 100 ) ;
3736
38- // Use a small batchSize to force the driver to issue getMore commands
39- const cursor = collection . find ( { } , { batchSize : 10 } ) ;
40- let docCount = 0 ;
37+ const stream = collection . find ( { } , { batchSize : 10 } ) . stream ( ) ;
38+ let docCount = 0 ;
4139
42- for await ( const doc of cursor ) {
40+ // Wrap the stream logic in a Promise to use await
41+ await new Promise ( ( resolve , reject ) => {
42+ stream . on ( 'data' , doc => {
4343 expect ( doc ) . to . have . property ( '_id' , docCount ) ;
4444 docCount ++ ;
45-
46- await sleep ( 100 ) ;
47- }
48-
49- expect ( docCount ) . to . equal ( 100 ) ;
45+ } ) ;
46+ stream . on ( 'end' , resolve ) ;
47+ stream . on ( 'error' , reject ) ;
5048 } ) ;
49+
50+ expect ( docCount ) . to . equal ( 100 ) ;
5151 } ) ;
5252
53- describe ( 'using Event Emitter API' , function ( ) {
54- it ( 'should stream all documents and emit "end"' , async function ( ) {
55- const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
56- await setupCollection ( collection , 100 ) ;
57-
58- const stream = collection . find ( { } , { batchSize : 10 } ) . stream ( ) ;
59- let docCount = 0 ;
60-
61- // Wrap the stream logic in a Promise to use await
62- await new Promise ( ( resolve , reject ) => {
63- stream . on ( 'data' , doc => {
64- expect ( doc ) . to . have . property ( '_id' , docCount ) ;
65- docCount ++ ;
66- } ) ;
67- stream . on ( 'end' , resolve ) ;
68- stream . on ( 'error' , reject ) ;
69- } ) ;
53+ it ( 'should stream all documents in for..of' , async function ( ) {
54+ const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
55+ await setupCollection ( collection , 100 ) ;
7056
71- expect ( docCount ) . to . equal ( 100 ) ;
72- } ) ;
57+ const stream = collection . find ( { } , { batchSize : 10 } ) . stream ( ) ;
58+ let docCount = 0 ;
7359
74- it ( 'should respect manual pause() and resume() calls' , async function ( ) {
75- const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
76- await setupCollection ( collection , 10 ) ;
60+ for await ( const doc of stream ) {
61+ expect ( doc ) . to . have . property ( '_id' , docCount ) ;
62+ docCount ++ ;
63+ }
7764
78- const stream = collection . find ( { } , { batchSize : 2 } ) . stream ( ) ;
79- let docCount = 0 ;
65+ expect ( docCount ) . to . equal ( 100 ) ;
66+ } ) ;
8067
81- await new Promise ( ( resolve , reject ) => {
82- stream . on ( 'error' , reject ) ;
83- stream . on ( 'end' , resolve ) ;
84- stream . on ( 'data' , ( ) => {
85- docCount ++ ;
86- // Manually pause the stream
87- stream . pause ( ) ;
68+ it ( 'should respect manual pause() and resume() calls' , async function ( ) {
69+ const collection = db . collection < { _id : number } > ( 'streaming_test' ) ;
70+ await setupCollection ( collection , 10 ) ;
8871
89- // Perform an async operation, then resume
90- sleep ( 100 ) . then ( ( ) => stream . resume ( ) ) ;
91- } ) ;
92- } ) ;
72+ const stream = collection . find ( { } , { batchSize : 2 } ) . stream ( ) ;
73+ let docCount = 0 ;
74+
75+ await new Promise ( ( resolve , reject ) => {
76+ stream . on ( 'error' , reject ) ;
77+ stream . on ( 'end' , resolve ) ;
78+ stream . on ( 'data' , ( ) => {
79+ docCount ++ ;
80+ // Manually pause the stream
81+ stream . pause ( ) ;
9382
94- expect ( docCount ) . to . equal ( 10 ) ;
83+ // Perform an async operation, then resume
84+ sleep ( 100 ) . then ( ( ) => stream . resume ( ) ) ;
85+ } ) ;
9586 } ) ;
87+
88+ expect ( docCount ) . to . equal ( 10 ) ;
9689 } ) ;
9790
9891 it ( 'should throws error' , async function ( ) {
0 commit comments