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: ensure response ordering matches request when chunking a request #893

Merged
merged 2 commits into from
Sep 23, 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
9 changes: 9 additions & 0 deletions packages/core/src/__tests__/utils.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ describe('utils', () => {
).resolves.toEqual(['a', 'b', 'c']);
});

test('promiseAllAtOnce: keep ordering', async () => {
const data = [100, 50, 10];
const promiser = async (sleepInMs: number) => {
await sleepPromise(sleepInMs);
return sleepInMs;
};
await expect(promiseAllAtOnce(data, promiser)).resolves.toEqual(data);
});

test('promiseEachInSequence', async () => {
expect(
await promiseEachInSequence([], (input) => Promise.resolve(input))
Expand Down
37 changes: 26 additions & 11 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,35 @@ export async function promiseAllAtOnce<RequestType, ResponseType>(
const responses: ResponseType[] = [];
const errors: (Error | CogniteError)[] = [];

const promises = inputs.map((input) => promiser(input));
const promises = inputs.map(promiser);

const wrappedPromises = promises.map((promise, index) =>
promise
.then((result) => {
succeded.push(inputs[index]);
responses.push(result);
})
.catch((error) => {
failed.push(inputs[index]);
errors.push(error);
type SingleResult = {
succeded?: RequestType;
response?: ResponseType;
failed?: RequestType;
error?: Error | CogniteError;
};

const wrappedPromises: Promise<SingleResult>[] = promises.map(
(promise, index) =>
new Promise<SingleResult>((resolve) => {
promise
.then((result) => {
resolve({ succeded: inputs[index], response: result });
})
.catch((error) => {
resolve({ failed: inputs[index], error });
});
})
);
await Promise.all(wrappedPromises);

const results = await Promise.all(wrappedPromises);
results.forEach((res) => {
failed.push(...(res.failed ? [res.failed] : []));
succeded.push(...(res.succeded ? [res.succeded] : []));
responses.push(...(res.response ? [res.response] : []));
errors.push(...(res.error ? [res.error] : []));
});
if (failed.length) {
throw {
succeded,
Expand Down
5 changes: 3 additions & 2 deletions packages/stable/src/__tests__/api/events.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ describe('Events integration test', () => {
test('descending', async () => {
await client.events.list({ sort: { endTime: SortOrder.DESC } });
});
test('multiple props not supported', async () => {
test('multiple props supported', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The events API added support for multiple fields. So the test is not valid anymore.

await expect(
client.events.list({
limit: 1,
sort: {
startTime: 'asc',
lastUpdatedTime: 'desc',
},
})
).rejects.toThrowError();
).resolves.toBeDefined();
});
});

Expand Down