@@ -3,7 +3,6 @@ import * as sinon from 'sinon';
33
44import {
55 Code ,
6- CursorResponse ,
76 Long ,
87 type MongoClient ,
98 MongoServerError ,
@@ -1075,29 +1074,29 @@ describe('Find', function () {
10751074 } ) ;
10761075
10771076 it (
1078- 'regression test (NODE-6878): CursorResponse.emptyGetMore contains all CursorResponse fields ' ,
1077+ 'regression test (NODE-6878): cursor can be rewound after limit-based exhaustion ' ,
10791078 { requires : { topology : 'sharded' } } ,
10801079 async function ( ) {
10811080 const collection = client . db ( 'rewind-regression' ) . collection ( 'bar' ) ;
10821081
10831082 await collection . deleteMany ( { } ) ;
10841083 await collection . insertMany ( Array . from ( { length : 4 } , ( _ , i ) => ( { x : i } ) ) ) ;
10851084
1086- const getMoreSpy = sinon . spy ( CursorResponse , 'emptyGetMore' , [ 'get' ] ) ;
1087-
10881085 const cursor = collection . find ( { } , { batchSize : 1 , limit : 3 } ) ;
1089- // emptyGetMore is used internally after limit + 1 documents have been iterated
1086+ // Iterate limit + 1 times to exercise the post-limit cursor path before rewind.
10901087 await cursor . next ( ) ;
10911088 await cursor . next ( ) ;
10921089 await cursor . next ( ) ;
10931090 await cursor . next ( ) ;
10941091
1095- // assert that `emptyGetMore` is called. if it is not, this test
1096- // always passes, even without the fix in NODE-6878.
1097- expect ( getMoreSpy . get ) . to . have . been . called ;
1092+ // On servers < 9.0, mongos returns a non-zero cursorId even after the limit
1093+ // is satisfied, so the driver uses CursorResponse.emptyGetMore to avoid a
1094+ // wasted getMore. On 9.0+, mongos returns cursorId: 0 when the limit is
1095+ // reached. In both cases, rewinding and re-iterating the cursor must work.
10981096
1097+ // rewind + toArray must not throw. Before the NODE-6878 fix,
1098+ // this would fail with a TypeError from emptyGetMore lacking CursorResponse fields.
10991099 cursor . rewind ( ) ;
1100-
11011100 await cursor . toArray ( ) ;
11021101 }
11031102 ) ;
0 commit comments