|
7 | 7 |
|
8 | 8 | import { assert } from "chai";
|
9 | 9 |
|
10 |
| -import { Client } from "../../../src/index"; |
| 10 | +import { ChaosHandler, ChaosHandlerOptions, ChaosStrategy, Client, ClientOptions } from "../../../src/index"; |
11 | 11 | import { PageIterator, PageIteratorCallback } from "../../../src/tasks/PageIterator";
|
12 | 12 | import { getClient } from "../../test-helper";
|
13 | 13 |
|
@@ -134,4 +134,88 @@ describe("PageIterator.ts", () => {
|
134 | 134 | assert.isTrue(pageIterator.isComplete());
|
135 | 135 | });
|
136 | 136 | });
|
| 137 | + describe("Test iteration using ChaosHandler", () => { |
| 138 | + it("testing with 5000 results in initial and next page", async () => { |
| 139 | + const middleware = new ChaosHandler(); |
| 140 | + |
| 141 | + const getPageCollection = () => { |
| 142 | + const initialPageResultValues: any[] = []; |
| 143 | + for (let i = 0; i < 5000; i++) { |
| 144 | + initialPageResultValues[i] = { event: "value" + i }; |
| 145 | + } |
| 146 | + return { |
| 147 | + value: initialPageResultValues, |
| 148 | + "@odata.nextLink": "nextURL", |
| 149 | + additionalContent: "additional content", |
| 150 | + }; |
| 151 | + }; |
| 152 | + const clientOptions: ClientOptions = { |
| 153 | + middleware, |
| 154 | + }; |
| 155 | + |
| 156 | + const nextPageResultValues: any[] = []; |
| 157 | + |
| 158 | + for (let i = 0; i < 5000; i++) { |
| 159 | + nextPageResultValues[i] = { event: "valueNext" + i }; |
| 160 | + } |
| 161 | + const responseBody = { value: nextPageResultValues }; |
| 162 | + let countNextPageResult = 0; |
| 163 | + const callback: PageIteratorCallback = (data) => { |
| 164 | + |
| 165 | + if (data["event"] === "valueNext" + countNextPageResult) { |
| 166 | + countNextPageResult++; |
| 167 | + } |
| 168 | + |
| 169 | + return true; |
| 170 | + }; |
| 171 | + |
| 172 | + const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))]; |
| 173 | + const requestOptions = { middlewareOptions }; |
| 174 | + |
| 175 | + const client = Client.initWithMiddleware(clientOptions); |
| 176 | + const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions); |
| 177 | + await pageIterator.iterate(); |
| 178 | + |
| 179 | + assert.equal(countNextPageResult, 5000); |
| 180 | + }); |
| 181 | + |
| 182 | + it("Evaluate next page result being fetched", async () => { |
| 183 | + const middleware = new ChaosHandler(); |
| 184 | + const getPageCollection = () => { |
| 185 | + return { |
| 186 | + value: [{ event1: "value1" }, { event2: "value2" }], |
| 187 | + "@odata.nextLink": "nextURL", |
| 188 | + additionalContent: "additional content", |
| 189 | + }; |
| 190 | + }; |
| 191 | + const clientOptions: ClientOptions = { |
| 192 | + middleware, |
| 193 | + }; |
| 194 | + const responseBody = { value: [{ event3: "value3" }, { event4: "value4" }] }; |
| 195 | + let counter = 1; |
| 196 | + let countNextPageResult = 0; |
| 197 | + const callback: PageIteratorCallback = (data) => { |
| 198 | + assert.equal(data["event" + counter], "value" + counter); |
| 199 | + |
| 200 | + if (data["event" + counter] === "value3") { |
| 201 | + countNextPageResult++; |
| 202 | + } |
| 203 | + |
| 204 | + if (data["event" + counter] === "value4") { |
| 205 | + countNextPageResult++; |
| 206 | + } |
| 207 | + counter++; |
| 208 | + return true; |
| 209 | + }; |
| 210 | + |
| 211 | + const middlewareOptions = [new ChaosHandlerOptions(ChaosStrategy.MANUAL, "middleware options for pageIterator", 200, 0, JSON.stringify(responseBody), new Headers({ "Content-Type": "application/json", "content-length": "100" }))]; |
| 212 | + const requestOptions = { middlewareOptions }; |
| 213 | + |
| 214 | + const client = Client.initWithMiddleware(clientOptions); |
| 215 | + const pageIterator = new PageIterator(client, getPageCollection(), callback, requestOptions); |
| 216 | + await pageIterator.iterate(); |
| 217 | + |
| 218 | + assert.equal(countNextPageResult, 2); |
| 219 | + }); |
| 220 | + }); |
137 | 221 | });
|
0 commit comments