Skip to content

Commit

Permalink
fix(util-watier): apply feedbacks
Browse files Browse the repository at this point in the history
Co-authored-by: Trivikram Kamat <[email protected]>
  • Loading branch information
AllanZhengYP and trivikr committed Dec 7, 2020
1 parent 1fc129f commit 2b52619
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/util-waiter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "Shared utilities for client waiters for the AWS SDK",
"dependencies": {
"tslib": "^1.8.0",
"@aws-sdk/abort-controller": "^1.0.0-rc.7"
"@aws-sdk/abort-controller": "1.0.0-rc.7"
},
"devDependencies": {
"@types/jest": "^26.0.4",
"jest": "^26.1.0",
"typescript": "~4.0.2"
"typescript": "~4.1.2"
},
"scripts": {
"prepublishOnly": "yarn build:cjs && yarn build:es",
Expand All @@ -30,7 +30,7 @@
"engines": {
"node": ">= 10.0.0"
},
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/master/package/util-waiter",
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/master/packages/util-waiter",
"repository": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-js-v3.git",
Expand Down
10 changes: 4 additions & 6 deletions packages/util-waiter/src/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ function exponentialBackoff(floor: number, ciel: number, attempt: number): numbe
* @param input client input
* @param stateChecker function that checks the acceptor states on each poll.
*/
export async function runPolling<T, S>(
export const runPolling = async <T, S>(
params: WaiterOptions,
client: T,
input: S,
acceptorChecks: (client: T, input: S) => Promise<WaiterResult>
): Promise<WaiterResult> {
): Promise<WaiterResult> => {
let currentAttempt = 1;
let currentDelay = params.minDelay;

while (true) {
// console.log("calling sleep (top of loop)", currentDelay);
await sleep(currentDelay);
const { state } = await acceptorChecks(client, input);
if (state == WaiterState.SUCCESS || state == WaiterState.FAILURE) {
if (state === WaiterState.SUCCESS || state === WaiterState.FAILURE) {
return { state };
}

currentDelay = exponentialBackoff(params.minDelay, params.maxDelay, currentAttempt);
currentAttempt += 1;
// console.log("bottom of loop");
}
}
};
27 changes: 15 additions & 12 deletions packages/util-waiter/src/utils/sleep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { abortTimeout, sleep, waiterTimeout } from "./sleep";
jest.useFakeTimers();

describe("Sleep Module", () => {
describe(sleep.name, () => {
beforeEach(() => {
jest.clearAllMocks();
jest.clearAllTimers();
});
beforeEach(() => {
jest.clearAllMocks();
jest.clearAllTimers();
});

describe(sleep.name, () => {
it("should call setTimeout with the proper number of milliseconds", () => {
sleep(1);

Expand All @@ -32,25 +32,28 @@ describe("Sleep Module", () => {
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3000);
});

it("should call return state retry", async (done) => {
it("should call return state retry", async () => {
const result = waiterTimeout(1);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000);
jest.advanceTimersByTime(1000);
expect((await result).state).toBe(WaiterState.RETRY);
done();
await expect(result).resolves.toEqual({ state: WaiterState.RETRY });
});
});

describe(abortTimeout.name, () => {
it("should listen to the abort control", async (done) => {
it("should listen to the abort control", async () => {
const abortControl = new AbortController();
const race = Promise.race([waiterTimeout(10000000), abortTimeout(abortControl.signal)]);
const mockTimeout = 1000;
const race = Promise.race([
new Promise((resolve) => setTimeout(resolve, mockTimeout)),
abortTimeout(abortControl.signal),
]);
abortControl.abort();
// jest.advanceTimersByTime(mockTimeout);
const result = await race;

expect(result.state).toBe(WaiterState.ABORTED);
done();
await expect(result).toEqual({ state: WaiterState.ABORTED });
});
});
});
12 changes: 3 additions & 9 deletions packages/util-waiter/src/utils/sleep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { AbortSignal } from "@aws-sdk/abort-controller";
import { WaiterResult, WaiterState } from "../waiter";

export const sleep = (seconds: number) => {
return new Promise((resolve) =>
setTimeout(() => {
// console.log("done with sleep");
resolve();
}, seconds * 1000)
);
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
};

export const waiterTimeout = async (seconds: number): Promise<WaiterResult> => {
Expand All @@ -17,8 +12,7 @@ export const waiterTimeout = async (seconds: number): Promise<WaiterResult> => {
};

export const abortTimeout = async (abortSignal: AbortSignal): Promise<WaiterResult> => {
await new Promise((resolve, reject) => {
abortSignal.onabort = () => resolve();
return new Promise((resolve) => {
abortSignal.onabort = () => resolve({ state: WaiterState.ABORTED });
});
return { state: WaiterState.ABORTED };
};

0 comments on commit 2b52619

Please sign in to comment.