Skip to content

Hotfix - Page iterator reset counter #1046

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

Merged
merged 2 commits into from
Nov 10, 2022
Merged
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
3 changes: 2 additions & 1 deletion src/tasks/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class PageIterator {
* @private
* Member holding the current position on the collection
*/
private cursor: number
private cursor: number;

/**
* @public
Expand Down Expand Up @@ -161,6 +161,7 @@ export class PageIterator {

const response: PageCollection = await graphRequest.get();
this.collection = response.value;
this.cursor = 0;
this.nextLink = response["@odata.nextLink"];
this.deltaLink = response["@odata.deltaLink"];
}
Expand Down
86 changes: 85 additions & 1 deletion test/common/tasks/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { assert } from "chai";

import { Client } from "../../../src/index";
import { ChaosHandler, ChaosHandlerOptions, ChaosStrategy, Client, ClientOptions } from "../../../src/index";
import { PageIterator, PageIteratorCallback } from "../../../src/tasks/PageIterator";
import { getClient } from "../../test-helper";

Expand Down Expand Up @@ -134,4 +134,88 @@ describe("PageIterator.ts", () => {
assert.isTrue(pageIterator.isComplete());
});
});
describe("Test iteration using ChaosHandler", () => {
it("testing with 5000 results in initial and next page", async () => {
const middleware = new ChaosHandler();

const getPageCollection = () => {
const initialPageResultValues: any[] = [];
for (let i = 0; i < 5000; i++) {
initialPageResultValues[i] = { event: "value" + i };
}
return {
value: initialPageResultValues,
"@odata.nextLink": "nextURL",
additionalContent: "additional content",
};
};
const clientOptions: ClientOptions = {
middleware,
};

const nextPageResultValues: any[] = [];

for (let i = 0; i < 5000; i++) {
nextPageResultValues[i] = { event: "valueNext" + i };
}
const responseBody = { value: nextPageResultValues };
let countNextPageResult = 0;
const callback: PageIteratorCallback = (data) => {

if (data["event"] === "valueNext" + countNextPageResult) {
countNextPageResult++;
}

return true;
};

const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))];
const requestOptions = { middlewareOptions };

const client = Client.initWithMiddleware(clientOptions);
const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions);
await pageIterator.iterate();

assert.equal(countNextPageResult, 5000);
});

it("Evaluate next page result being fetched", async () => {
const middleware = new ChaosHandler();
const getPageCollection = () => {
return {
value: [{ event1: "value1" }, { event2: "value2" }],
"@odata.nextLink": "nextURL",
additionalContent: "additional content",
};
};
const clientOptions: ClientOptions = {
middleware,
};
const responseBody = { value: [{ event3: "value3" }, { event4: "value4" }] };
let counter = 1;
let countNextPageResult = 0;
const callback: PageIteratorCallback = (data) => {
assert.equal(data["event" + counter], "value" + counter);

if (data["event" + counter] === "value3") {
countNextPageResult++;
}

if (data["event" + counter] === "value4") {
countNextPageResult++;
}
counter++;
return true;
};

const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))];
const requestOptions = { middlewareOptions };

const client = Client.initWithMiddleware(clientOptions);
const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions);
await pageIterator.iterate();

assert.equal(countNextPageResult, 2);
});
});
});
33 changes: 0 additions & 33 deletions test/development/workload/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import { Event } from "@microsoft/microsoft-graph-types";
import { assert } from "chai";

import { Client, ClientOptions } from "../../../src";
import { ChaosHandler } from "../../../src/middleware/ChaosHandler";
import { ChaosHandlerOptions } from "../../../src/middleware/options/ChaosHandlerOptions";
import { ChaosStrategy } from "../../../src/middleware/options/ChaosStrategy";
import { GraphRequestOptions, PageIterator, PageIteratorCallback } from "../../../src/tasks/PageIterator";
import { getClient } from "../test-helper";
const client = getClient();
Expand Down Expand Up @@ -91,33 +87,4 @@ describe("PageIterator", () => {
assert.isTrue(pageIterator.isComplete());
}
}).timeout(30 * 1000);

// TODO - Temporariliy commenting this test.
it("setting middleware with pageIterator", async () => {
const middleware = new ChaosHandler();
const getPageCollection = () => {
return {
value: [],
"@odata.nextLink": "nextURL",
additionalContent: "additional content",
};
};
const clientOptions: ClientOptions = {
middleware,
};
const responseBody = { value: [{ event1: "value1" }, { event2: "value2" }] };
let counter = 1;
const callback: PageIteratorCallback = (data) => {
assert.equal(data["event" + counter], "value" + counter);
counter++;
return true;
};

const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))];
const requestOptions = { middlewareOptions };

const client = Client.initWithMiddleware(clientOptions);
const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions);
await pageIterator.iterate();
});
});