@@ -4,10 +4,17 @@ import { calculateObjectSize } from "bson";
44import { iterateCursorUntilMaxBytes } from "../../../src/helpers/iterateCursor.js" ;
55
66describe ( "iterateCursorUntilMaxBytes" , ( ) => {
7- function createMockCursor ( docs : unknown [ ] ) : FindCursor < unknown > {
7+ function createMockCursor (
8+ docs : unknown [ ] ,
9+ { abortController, abortOnIdx } : { abortController ?: AbortController ; abortOnIdx ?: number } = { }
10+ ) : FindCursor < unknown > {
811 let idx = 0 ;
912 return {
1013 tryNext : vi . fn ( ( ) => {
14+ if ( idx === abortOnIdx ) {
15+ abortController ?. abort ( ) ;
16+ }
17+
1118 if ( idx < docs . length ) {
1219 return Promise . resolve ( docs [ idx ++ ] ) ;
1320 }
@@ -23,23 +30,36 @@ describe("iterateCursorUntilMaxBytes", () => {
2330 const docs = Array . from ( { length : 1000 } ) . map ( ( _ , idx ) => ( { value : idx } ) ) ;
2431 const cursor = createMockCursor ( docs ) ;
2532 const maxBytes = - 1 ;
26- const result = await iterateCursorUntilMaxBytes ( cursor , maxBytes ) ;
33+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : maxBytes } ) ;
2734 expect ( result ) . toEqual ( docs ) ;
2835 } ) ;
2936
3037 it ( "returns all docs if maxBytesPerQuery is 0" , async ( ) => {
3138 const docs = Array . from ( { length : 1000 } ) . map ( ( _ , idx ) => ( { value : idx } ) ) ;
3239 const cursor = createMockCursor ( docs ) ;
3340 const maxBytes = 0 ;
34- const result = await iterateCursorUntilMaxBytes ( cursor , maxBytes ) ;
41+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : maxBytes } ) ;
3542 expect ( result ) . toEqual ( docs ) ;
3643 } ) ;
3744
45+ it ( "respects abort signal and breaks out of loop when aborted" , async ( ) => {
46+ const docs = Array . from ( { length : 20 } ) . map ( ( _ , idx ) => ( { value : idx } ) ) ;
47+ const abortController = new AbortController ( ) ;
48+ const cursor = createMockCursor ( docs , { abortOnIdx : 9 , abortController } ) ;
49+ const maxBytes = 10000 ;
50+ const result = await iterateCursorUntilMaxBytes ( {
51+ cursor,
52+ maxBytesPerQuery : maxBytes ,
53+ abortSignal : abortController . signal ,
54+ } ) ;
55+ expect ( result ) . toEqual ( Array . from ( { length : 10 } ) . map ( ( _ , idx ) => ( { value : idx } ) ) ) ;
56+ } ) ;
57+
3858 it ( "returns all docs if under maxBytesPerQuery" , async ( ) => {
3959 const docs = [ { a : 1 } , { b : 2 } ] ;
4060 const cursor = createMockCursor ( docs ) ;
4161 const maxBytes = 10000 ;
42- const result = await iterateCursorUntilMaxBytes ( cursor , maxBytes ) ;
62+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : maxBytes } ) ;
4363 expect ( result ) . toEqual ( docs ) ;
4464 } ) ;
4565
@@ -49,20 +69,20 @@ describe("iterateCursorUntilMaxBytes", () => {
4969 const docs = [ doc1 , doc2 ] ;
5070 const cursor = createMockCursor ( docs ) ;
5171 const maxBytes = calculateObjectSize ( doc1 ) + 10 ;
52- const result = await iterateCursorUntilMaxBytes ( cursor , maxBytes ) ;
72+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : maxBytes } ) ;
5373 expect ( result ) . toEqual ( [ doc1 ] ) ;
5474 } ) ;
5575
5676 it ( "returns empty array if maxBytesPerQuery is smaller than even the first doc" , async ( ) => {
5777 const docs = [ { a : "x" . repeat ( 100 ) } ] ;
5878 const cursor = createMockCursor ( docs ) ;
59- const result = await iterateCursorUntilMaxBytes ( cursor , 10 ) ;
79+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : 10 } ) ;
6080 expect ( result ) . toEqual ( [ ] ) ;
6181 } ) ;
6282
6383 it ( "handles empty cursor" , async ( ) => {
6484 const cursor = createMockCursor ( [ ] ) ;
65- const result = await iterateCursorUntilMaxBytes ( cursor , 1000 ) ;
85+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : 1000 } ) ;
6686 expect ( result ) . toEqual ( [ ] ) ;
6787 } ) ;
6888
@@ -73,7 +93,7 @@ describe("iterateCursorUntilMaxBytes", () => {
7393 const cursor = createMockCursor ( docs ) ;
7494 // Set maxBytes so that after doc1, biggestDocSizeSoFar would prevent fetching doc2
7595 const maxBytes = calculateObjectSize ( doc1 ) + calculateObjectSize ( doc2 ) - 1 ;
76- const result = await iterateCursorUntilMaxBytes ( cursor , maxBytes ) ;
96+ const result = await iterateCursorUntilMaxBytes ( { cursor, maxBytesPerQuery : maxBytes } ) ;
7797 // Should only include doc1, not doc2
7898 expect ( result ) . toEqual ( [ doc1 ] ) ;
7999 } ) ;
0 commit comments