Skip to content

Commit 2a6ac11

Browse files
authored
fix(util-waiters): waiters should call operation once before entering waiting (aws#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
1 parent d322e7a commit 2a6ac11

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/util-waiter/src/poller.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@ describe(runPolling.name, () => {
4949
expect(mockAcceptorChecks).toHaveBeenCalled();
5050
expect(mockAcceptorChecks).toHaveBeenCalledTimes(1);
5151
expect(mockAcceptorChecks).toHaveBeenCalledWith(config.client, input);
52-
53-
expect(sleep).toHaveBeenCalled();
54-
expect(sleep).toHaveBeenCalledTimes(1);
55-
expect(sleep).toHaveBeenCalledWith(config.minDelay);
52+
expect(sleep).toHaveBeenCalledTimes(0);
5653
});
5754

5855
it("returns state in case of success", async () => {
5956
mockAcceptorChecks = jest.fn().mockResolvedValueOnce(successState);
6057
await expect(runPolling(config, input, mockAcceptorChecks)).resolves.toStrictEqual(successState);
61-
expect(sleep).toHaveBeenCalled();
62-
expect(sleep).toHaveBeenCalledTimes(1);
63-
expect(sleep).toHaveBeenCalledWith(config.minDelay);
58+
expect(mockAcceptorChecks).toHaveBeenCalled();
59+
expect(mockAcceptorChecks).toHaveBeenCalledTimes(1);
60+
expect(mockAcceptorChecks).toHaveBeenCalledWith(config.client, input);
61+
expect(sleep).toHaveBeenCalledTimes(0);
6462
});
6563

6664
it("sleeps as per exponentialBackoff in case of retry", async () => {
@@ -72,11 +70,13 @@ describe(runPolling.name, () => {
7270
.mockResolvedValueOnce(retryState)
7371
.mockResolvedValueOnce(retryState)
7472
.mockResolvedValueOnce(retryState)
73+
.mockResolvedValueOnce(retryState)
7574
.mockResolvedValueOnce(successState);
7675

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

7978
expect(sleep).toHaveBeenCalled();
79+
expect(mockAcceptorChecks).toHaveBeenCalledTimes(8);
8080
expect(sleep).toHaveBeenCalledTimes(7);
8181
expect(sleep).toHaveBeenNthCalledWith(1, 2); // min delay. random(2, 2)
8282
expect(sleep).toHaveBeenNthCalledWith(2, 3); // random(2, 4)

packages/util-waiter/src/poller.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const exponentialBackoffWithJitter = (minDelay: number, maxDelay: number, attemp
1313
const randomInRange = (min: number, max: number) => min + Math.random() * (max - min);
1414

1515
/**
16-
* Function that runs indefinite polling as part of waiters.
16+
* Function that runs polling as part of waiters. This will make one inital attempt and then
17+
* subsequent attempts with an increasing delay.
1718
* @param params options passed to the waiter.
1819
* @param client AWS SDK Client
1920
* @param input client input
@@ -24,6 +25,11 @@ export const runPolling = async <Client, Input>(
2425
input: Input,
2526
acceptorChecks: (client: Client, input: Input) => Promise<WaiterResult>
2627
): Promise<WaiterResult> => {
28+
const { state } = await acceptorChecks(client, input);
29+
if (state !== WaiterState.RETRY) {
30+
return { state };
31+
}
32+
2733
let currentAttempt = 1;
2834
const waitUntil = Date.now() + maxWaitTime * 1000;
2935
// The max attempt number that the derived delay time tend to increase.

0 commit comments

Comments
 (0)