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(util-waiters): waiters should call operation once before entering waiting #1915

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 7 additions & 7 deletions packages/util-waiter/src/poller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@ describe(runPolling.name, () => {
expect(mockAcceptorChecks).toHaveBeenCalled();
expect(mockAcceptorChecks).toHaveBeenCalledTimes(1);
expect(mockAcceptorChecks).toHaveBeenCalledWith(config.client, input);

expect(sleep).toHaveBeenCalled();
expect(sleep).toHaveBeenCalledTimes(1);
expect(sleep).toHaveBeenCalledWith(config.minDelay);
expect(sleep).toHaveBeenCalledTimes(0);
});

it("returns state in case of success", async () => {
mockAcceptorChecks = jest.fn().mockResolvedValueOnce(successState);
await expect(runPolling(config, input, mockAcceptorChecks)).resolves.toStrictEqual(successState);
expect(sleep).toHaveBeenCalled();
expect(sleep).toHaveBeenCalledTimes(1);
expect(sleep).toHaveBeenCalledWith(config.minDelay);
expect(mockAcceptorChecks).toHaveBeenCalled();
expect(mockAcceptorChecks).toHaveBeenCalledTimes(1);
expect(mockAcceptorChecks).toHaveBeenCalledWith(config.client, input);
expect(sleep).toHaveBeenCalledTimes(0);
});

it("sleeps as per exponentialBackoff in case of retry", async () => {
Expand All @@ -72,11 +70,13 @@ describe(runPolling.name, () => {
.mockResolvedValueOnce(retryState)
.mockResolvedValueOnce(retryState)
.mockResolvedValueOnce(retryState)
.mockResolvedValueOnce(retryState)
.mockResolvedValueOnce(successState);

await expect(runPolling(config, input, mockAcceptorChecks)).resolves.toStrictEqual(successState);

expect(sleep).toHaveBeenCalled();
expect(mockAcceptorChecks).toHaveBeenCalledTimes(8);
expect(sleep).toHaveBeenCalledTimes(7);
expect(sleep).toHaveBeenNthCalledWith(1, 2); // min delay. random(2, 2)
expect(sleep).toHaveBeenNthCalledWith(2, 3); // random(2, 4)
Expand Down
8 changes: 7 additions & 1 deletion packages/util-waiter/src/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const exponentialBackoffWithJitter = (minDelay: number, maxDelay: number, attemp
const randomInRange = (min: number, max: number) => min + Math.random() * (max - min);

/**
* Function that runs indefinite polling as part of waiters.
* Function that runs polling as part of waiters. This will make one inital attempt and then
* subsequent attempts with an increasing delay.
* @param params options passed to the waiter.
* @param client AWS SDK Client
* @param input client input
Expand All @@ -24,6 +25,11 @@ export const runPolling = async <Client, Input>(
input: Input,
acceptorChecks: (client: Client, input: Input) => Promise<WaiterResult>
): Promise<WaiterResult> => {
const { state } = await acceptorChecks(client, input);
if (state !== WaiterState.RETRY) {
return { state };
}
trivikr marked this conversation as resolved.
Show resolved Hide resolved

let currentAttempt = 1;
const waitUntil = Date.now() + maxWaitTime * 1000;
// The max attempt number that the derived delay time tend to increase.
Expand Down
Loading