-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Added retry utility functions #66
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2108c5d
feat: Added retry utility functions
klmhyeonwoo 4eaa07b
Merge branch 'main' into feature/65
klmhyeonwoo ac5345c
fix: remove retry utility output console.error
klmhyeonwoo a278f58
docs: Remove incorrectly inserted comments
klmhyeonwoo dc6c7bd
Update README.md
klmhyeonwoo 55e4086
Update package/commonUtil/retry/index.ts
klmhyeonwoo 667d981
Update README.md
klmhyeonwoo 50b52b9
Update package/commonUtil/retry/index.test.ts
klmhyeonwoo cc45149
Update package/commonUtil/retry/index.test.ts
klmhyeonwoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,66 @@ | ||
| import { describe, expect, test, vi } from "vitest"; | ||
| import retry from "."; | ||
|
|
||
| describe("retry 유틸 함수 테스트", () => { | ||
| test("정상적으로 실행되는 함수는 한번만 실행한다.", async () => { | ||
| const fn = vi.fn().mockResolvedValue("success!"); | ||
|
|
||
| const result = await retry(fn, 3); | ||
|
|
||
| expect(result).toBe("success!"); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("함수를 실행하며 오류가 발생하면 재시도한다.", async () => { | ||
| const fn = vi | ||
| .fn() | ||
| .mockRejectedValueOnce(new Error("fail 1")) | ||
| .mockRejectedValueOnce(new Error("fail 2")) | ||
| .mockResolvedValue("success!"); | ||
|
|
||
| const result = await retry(fn, 3); | ||
|
|
||
| expect(result).toBe("success!"); | ||
| expect(fn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| test("재시도 한도를 초과하면 마지막 에러를 던진다.", async () => { | ||
| const error = new Error("always fail"); | ||
| const fn = vi.fn().mockRejectedValue(error); | ||
|
|
||
| await expect(retry(fn, 2)).rejects.toThrow("always fail"); | ||
| expect(fn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| test("기본 재시도 횟수는 3번이다.", async () => { | ||
| const fn = vi.fn().mockRejectedValue(new Error("fail")); | ||
|
|
||
| await expect(retry(fn)).rejects.toThrow("fail"); | ||
| expect(fn).toHaveBeenCalledTimes(4); | ||
|
klmhyeonwoo marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| test("타입 안전성: 반환 타입이 올바르게 추론된다.", async () => { | ||
| const stringFn = vi.fn().mockResolvedValue("hello"); | ||
| const numberFn = vi.fn().mockResolvedValue(42); | ||
| const objectFn = vi.fn().mockResolvedValue({ id: 1 }); | ||
|
|
||
| const stringResult = await retry(stringFn); | ||
| const numberResult = await retry(numberFn); | ||
| const objectResult = await retry(objectFn); | ||
|
|
||
| expect(typeof stringResult).toBe("string"); | ||
| expect(typeof numberResult).toBe("number"); | ||
| expect(typeof objectResult).toBe("object"); | ||
|
|
||
| expect(stringResult).toBe("hello"); | ||
| expect(numberResult).toBe(42); | ||
| expect(objectResult).toEqual({ id: 1 }); | ||
| }); | ||
|
|
||
| test("0번 재시도 시 한 번만 실행한다.", async () => { | ||
| const fn = vi.fn().mockRejectedValue(new Error("fail")); | ||
|
|
||
| await expect(retry(fn, 0)).rejects.toThrow("fail"); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,21 @@ | ||
| export default async function retry<T>( | ||
| fn: () => Promise<T>, | ||
| loop: number = 3 | ||
| ): Promise<T> { | ||
| let count = 0; | ||
|
|
||
| const attempt = async (): Promise<T> => { | ||
| try { | ||
| return await fn(); | ||
| } catch (e) { | ||
| count++; | ||
| if (count > loop) { | ||
|
klmhyeonwoo marked this conversation as resolved.
Outdated
|
||
| throw e; | ||
| } | ||
| console.error(`Retry attempt ${count} / ${loop} failed:`, e); | ||
|
klmhyeonwoo marked this conversation as resolved.
Outdated
|
||
| return attempt(); | ||
| } | ||
| }; | ||
|
klmhyeonwoo marked this conversation as resolved.
|
||
|
|
||
| return attempt(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.