Skip to content

Commit

Permalink
fix(util-waiters): waiters should call operation once before entering…
Browse files Browse the repository at this point in the history
… waiting (#1915)

* chore(clients): update yarn lock with types from @aws-crypto

* fix(util-waiters): waiters should call operation once before entering waiting

* fix: proper yarn.lock
  • Loading branch information
alexforsyth authored Jan 14, 2021
1 parent d322e7a commit 2a6ac11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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 };
}

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

0 comments on commit 2a6ac11

Please sign in to comment.