-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cli-testing): add a retry for test (#29908)
One of our tests can remove customPermissionsBoundary creates a policy using createPolicy. Change to IAM policies/roles use eventual consistency. So, while the changes will show up right away if we were to call an API to describe that policy/role, the updates may not have actually propagated to all regions yet. This is likely the cause of the intermittent test failures for this test. This change adds the eventually block and uses it to retry initial creation of this stack in the case that the policy changes have not made it to the relevant region just yet. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
f6c902e
commit 9eb21bd
Showing
2 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
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,42 @@ | ||
/** | ||
* @param maxAttempts the maximum number of attempts | ||
* @param interval interval in milliseconds to observe between attempts | ||
*/ | ||
export type EventuallyOptions = { | ||
maxAttempts?: number; | ||
interval?: number; | ||
}; | ||
|
||
const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms)); | ||
const DEFAULT_INTERVAL = 1000; | ||
const DEFAULT_MAX_ATTEMPTS = 10; | ||
|
||
/** | ||
* Runs a function on an interval until the maximum number of attempts has | ||
* been reached. | ||
* | ||
* Default interval = 1000 milliseconds | ||
* Default maxAttempts = 10 | ||
* | ||
* @param fn function to run | ||
* @param options EventuallyOptions | ||
*/ | ||
const eventually = async <T>(call: () => Promise<T>, options?: EventuallyOptions): Promise<T> => { | ||
const opts = { | ||
interval: options?.interval ? options.interval : DEFAULT_INTERVAL, | ||
maxAttempts: options?.maxAttempts ? options.maxAttempts : DEFAULT_MAX_ATTEMPTS, | ||
}; | ||
|
||
while (opts.maxAttempts-- >= 0) { | ||
try { | ||
return await call(); | ||
} catch (err) { | ||
if (opts.maxAttempts <= 0) throw err; | ||
} | ||
await wait(opts.interval); | ||
} | ||
|
||
throw new Error('An unexpected error has occurred.'); | ||
}; | ||
|
||
export default eventually; |
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