Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NODEJS-668: Handle empty pages in async pagination #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/types/result-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ ResultSet.prototype[asyncIteratorSymbol] = function getAsyncGenerator() {
let pageState = this.rawPageState;
let rows = this.rows;

if (!rows || rows.length === 0) {
if (!pageState && (!rows || rows.length === 0)) {
return { next: () => Promise.resolve({ done: true }) };
}

Expand All @@ -257,7 +257,13 @@ ResultSet.prototype[asyncIteratorSymbol] = function getAsyncGenerator() {
return { done: false, value: rows[index++] };
}

return { done: true };
if (!pageState) {
return { done: true };
}

// We could reach this point if an empty page is returned.
// An empty page doesn't necessarily mean the end of the results.
return this.next();
}
};
};
Expand Down
20 changes: 20 additions & 0 deletions test/unit/result-set-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ describe('ResultSet', function () {
assert.ok(rs.nextPageAsync.firstCall.calledWithExactly(pageState));
});

it('should continue past empty pages', async () => {
const pageState = utils.allocBuffer(1);
const rs = new ResultSet( { rows: [ 100, 101, 102 ], meta: { pageState } });
const pages = [
[],
[],
[],
[ 103, 104, 105 ],
];
rs.nextPageAsync = sinon.spy(function () {
const rows = pages.shift();
return Promise.resolve({ rows: rows, rawPageState: pages.length > 0 ? pageState : undefined });
});

const result = await helper.asyncIteratorToArray(rs);
assert.deepEqual(result, [ 100, 101, 102, 103, 104, 105 ]);
assert.strictEqual(rs.nextPageAsync.callCount, 4);
assert.ok(rs.nextPageAsync.firstCall.calledWithExactly(pageState));
});

it('should reject when nextPageAsync rejects', async () => {
const rs = new ResultSet( { rows: [ 100 ], meta: { pageState: utils.allocBuffer(1)} });
const error = new Error('Test dummy error');
Expand Down