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

fix: make pagination work for empty responses #1043

Merged
merged 3 commits into from
Jul 9, 2021
Merged
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
11 changes: 10 additions & 1 deletion src/paginationCalls/pageDescriptor.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ import {NormalApiCaller} from '../normalCalls/normalApiCaller';

import {PagedApiCaller} from './pagedApiCaller';

const maxAttemptsEmptyResponse = 10;

export interface ResponseType {
[index: string]: string;
}
@@ -150,13 +152,20 @@ export class PageDescriptor implements Descriptor {
value: cache.shift(),
});
}
if (nextPageRequest) {
let attempts = 0;
while (cache.length === 0 && nextPageRequest) {
let result: {} | [ResponseType] | null;
[result, nextPageRequest] = (await apiCall(
nextPageRequest!,
options
)) as ResultTuple;
cache.push(...(result as ResponseType[]));
if (cache.length === 0) {
++attempts;
if (attempts > maxAttemptsEmptyResponse) {
break;
}
}
}
if (cache.length === 0) {
return Promise.resolve({done: true, value: undefined});
2 changes: 1 addition & 1 deletion src/paginationCalls/pagedApiCaller.ts
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ export class PagedApiCaller implements APICaller {
);
return;
}
const resources = response[resourceFieldName];
const resources = response[resourceFieldName] || [];
const pageToken = response[responsePageTokenFieldName];
let nextPageRequest = null;
if (pageToken) {
69 changes: 53 additions & 16 deletions test/unit/pagedIteration.ts
Original file line number Diff line number Diff line change
@@ -200,26 +200,23 @@ describe('paged iteration', () => {
});

describe('use async iterator', () => {
const spy = sinon.spy(func);
let apiCall: GaxCall;
beforeEach(() => {
apiCall = util.createApiCall(spy, createOptions);
});
it('returns an iterable, count to 10', async () => {
const spy = sinon.spy(func);
const apiCall = util.createApiCall(spy, createOptions);

async function iterableChecker(iterable: AsyncIterable<{} | undefined>) {
let counter = 0;
const resources = [];
for await (const resource of iterable) {
counter++;
resources.push(resource);
if (counter === 10) {
break;
async function iterableChecker(iterable: AsyncIterable<{} | undefined>) {
let counter = 0;
const resources = [];
for await (const resource of iterable) {
counter++;
resources.push(resource);
if (counter === 10) {
break;
}
}
return resources;
}
return resources;
}

it('returns an iterable, count to 10', async () => {
const settings = new gax.CallSettings(
(createOptions && createOptions.settings) || {}
);
@@ -228,6 +225,46 @@ describe('paged iteration', () => {
);
assert.strictEqual(resources.length, 10);
});

it('does not stop on empty resources list', async () => {
function func(
request: {pageToken?: number},
metadata: {},
options: {},
callback: APICallback
) {
const responsePages = [
[1, 2, 3],
[],
[4, 5, 6],
[],
[],
[7],
[8, 9],
[],
[10],
];
const pageToken = request.pageToken || 0;
if (pageToken >= responsePages.length) {
callback(null, {nums: []});
} else {
callback(null, {
nums: responsePages[pageToken],
nextPageToken: pageToken + 1,
});
}
}
const apiCall = util.createApiCall(func, createOptions);
const settings = new gax.CallSettings(
(createOptions && createOptions.settings) || {}
);
const iterable = descriptor.asyncIterate(apiCall, {}, settings);
const results = [];
for await (const result of iterable) {
results.push(result);
}
assert.deepStrictEqual(results, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
});

describe('stream conversion', () => {