-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: wait for all PRs to attempt to merge before throwing error (#4663)
* feat: add helper for running work items in parallel * fix: run cleanDatastore and checkPRMergeability in forAllInAsyncGroups with thrown errors
- Loading branch information
Showing
5 changed files
with
185 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import AggregateError from 'aggregate-error'; | ||
|
||
const DEFAULT_GROUP_SIZE = 4; | ||
|
||
interface AsyncGroupsOptions { | ||
groupSize?: number; | ||
throwOnError?: boolean; | ||
} | ||
type SingleItemHandler<TItem, TResult> = (item: TItem) => Promise<TResult>; | ||
/** | ||
* Helper that executes a single item handler for each item in parallel | ||
* groups. | ||
* @param {TItem[]} items The list of items to handle. | ||
* @param {SingleItemHandler<TItem, TResult>} asyncHandler The single item async | ||
* handler | ||
* @param {number} options.groupSize The number of parallel executions. Defaults to 4. | ||
* @param {boolean} options.throwOnError Whether to throw an AggregateError if any items fail | ||
* @returns A list of settled promise results (either failure or success). | ||
* @throws {AggregateError} if any items fails which contains all the thrown Errors. | ||
*/ | ||
export async function forAllInAsyncGroups<TItem, TResult>( | ||
items: TItem[], | ||
asyncHandler: SingleItemHandler<TItem, TResult>, | ||
options: AsyncGroupsOptions = {} | ||
): Promise<PromiseSettledResult<TResult>[]> { | ||
const groupSize = options?.groupSize ?? DEFAULT_GROUP_SIZE; | ||
const throwOnError = options?.throwOnError ?? false; | ||
let results: PromiseSettledResult<TResult>[] = []; | ||
for (let i = 0; i < items.length; i += groupSize) { | ||
const group = items.slice(i, i + groupSize); | ||
const partial = await Promise.allSettled(group.map(pr => asyncHandler(pr))); | ||
results = results.concat(...partial); | ||
} | ||
|
||
if (throwOnError) { | ||
const errors: Error[] = []; | ||
for (const result of results) { | ||
if (result.status === 'rejected') { | ||
errors.push(result.reason); | ||
} | ||
} | ||
if (errors.length > 0) { | ||
throw new AggregateError(errors); | ||
} | ||
} | ||
|
||
return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {describe, it} from 'mocha'; | ||
import assert from 'assert'; | ||
import {forAllInAsyncGroups} from '../src/parallel-work'; | ||
import AggregateError from 'aggregate-error'; | ||
|
||
describe('forAllInAsyncGroups', () => { | ||
const handler = async function (x: number): Promise<number> { | ||
if (x % 2 === 0) { | ||
return x; | ||
} else { | ||
throw new Error(`odd number: ${x}`); | ||
} | ||
}; | ||
const input: number[] = []; | ||
for (let i = 0; i < 10; i++) { | ||
input.push(i); | ||
} | ||
|
||
it('runs all work items', async () => { | ||
const results = await forAllInAsyncGroups(input, handler); | ||
assert.strictEqual(results.length, 10); | ||
const successes = results.filter(result => result.status === 'fulfilled'); | ||
assert.strictEqual(successes.length, 5); | ||
const failures = results.filter(result => result.status === 'rejected'); | ||
assert.strictEqual(failures.length, 5); | ||
for (const failure of failures) { | ||
assert.ok( | ||
failure.status === 'rejected' && failure.reason instanceof Error | ||
); | ||
} | ||
}); | ||
|
||
it('throws if throwOnError is specified', async () => { | ||
await assert.rejects( | ||
async () => { | ||
await forAllInAsyncGroups(input, handler, {throwOnError: true}); | ||
}, | ||
err => { | ||
return err instanceof AggregateError; | ||
} | ||
); | ||
}); | ||
|
||
it('succeeds when throwOnError is specified', async () => { | ||
const results = await forAllInAsyncGroups( | ||
input.map(x => x * 2), | ||
handler, | ||
{throwOnError: true} | ||
); | ||
assert.strictEqual(results.length, 10); | ||
const successes = results.filter(result => result.status === 'fulfilled'); | ||
assert.strictEqual(successes.length, 10); | ||
}); | ||
}); |