-
Notifications
You must be signed in to change notification settings - Fork 324
(Codex) 收紧 LimiterFileSystem 的 429 自动重试范围,避免写操作被盲目重放
#1376
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
+216
−20
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type FileSystem from "./filesystem"; | ||
| import type { FileInfo, FileReader, FileWriter } from "./filesystem"; | ||
| import LimiterFileSystem from "./limiter"; | ||
|
|
||
| function createFs(): FileSystem { | ||
| return { | ||
| verify: vi.fn(async () => {}), | ||
| open: vi.fn(async () => { | ||
| const reader: FileReader = { | ||
| read: vi.fn(async () => "content"), | ||
| }; | ||
| return reader; | ||
| }), | ||
| openDir: vi.fn(async () => createFs()), | ||
| create: vi.fn(async () => { | ||
| const writer: FileWriter = { | ||
| write: vi.fn(async () => {}), | ||
| }; | ||
| return writer; | ||
| }), | ||
| createDir: vi.fn(async () => {}), | ||
| delete: vi.fn(async () => {}), | ||
| list: vi.fn(async () => []), | ||
| getDirUrl: vi.fn(async () => "url"), | ||
| }; | ||
| } | ||
|
|
||
| const file: FileInfo = { | ||
| name: "test.user.js", | ||
| path: "/test.user.js", | ||
| size: 1, | ||
| digest: "digest", | ||
| createtime: 1, | ||
| updatetime: 1, | ||
| }; | ||
|
|
||
| describe("LimiterFileSystem", () => { | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("should retry list on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| vi.mocked(fs.list).mockRejectedValueOnce(new Error("429 Too Many Requests")).mockResolvedValueOnce([]); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| const promise = limiter.list(); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toEqual([]); | ||
| expect(fs.list).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should retry verify on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| vi.mocked(fs.verify).mockRejectedValueOnce(new Error("429 Too Many Requests")).mockResolvedValueOnce(undefined); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| const promise = limiter.verify(); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toBeUndefined(); | ||
| expect(fs.verify).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should retry open on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| const reader: FileReader = { read: vi.fn(async () => "content") }; | ||
| vi.mocked(fs.open).mockRejectedValueOnce(new Error("429 Too Many Requests")).mockResolvedValueOnce(reader); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| const promise = limiter.open(file); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toBeDefined(); | ||
| expect(fs.open).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should retry openDir on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| const inner = createFs(); | ||
| vi.mocked(fs.openDir).mockRejectedValueOnce(new Error("429 Too Many Requests")).mockResolvedValueOnce(inner); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| const promise = limiter.openDir("/dir"); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toBeDefined(); | ||
| expect(fs.openDir).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should retry getDirUrl on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| vi.mocked(fs.getDirUrl).mockRejectedValueOnce(new Error("429 Too Many Requests")).mockResolvedValueOnce("url"); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| const promise = limiter.getDirUrl(); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toBe("url"); | ||
| expect(fs.getDirUrl).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should not retry create on 429", async () => { | ||
| const fs = createFs(); | ||
| vi.mocked(fs.create).mockRejectedValueOnce(new Error("429 Too Many Requests")); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| await expect(limiter.create(file.path)).rejects.toThrow("429 Too Many Requests"); | ||
| expect(fs.create).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should pass FileCreateOptions through create", async () => { | ||
| const fs = createFs(); | ||
| const limiter = new LimiterFileSystem(fs); | ||
| await limiter.create(file.path, { modifiedDate: 123 }); | ||
| expect(fs.create).toHaveBeenCalledWith(file.path, { modifiedDate: 123 }); | ||
| }); | ||
|
|
||
| it("should pass FileCreateOptions through createDir", async () => { | ||
| const fs = createFs(); | ||
| const limiter = new LimiterFileSystem(fs); | ||
| await limiter.createDir("/dir", { modifiedDate: 456 }); | ||
| expect(fs.createDir).toHaveBeenCalledWith("/dir", { modifiedDate: 456 }); | ||
| }); | ||
|
|
||
| it("should not retry delete on 429", async () => { | ||
| const fs = createFs(); | ||
| vi.mocked(fs.delete).mockRejectedValueOnce(new Error("429 Too Many Requests")); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| await expect(limiter.delete("/test.user.js")).rejects.toThrow("429 Too Many Requests"); | ||
| expect(fs.delete).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should not retry createDir on 429", async () => { | ||
| const fs = createFs(); | ||
| vi.mocked(fs.createDir).mockRejectedValueOnce(new Error("429 Too Many Requests")); | ||
| const limiter = new LimiterFileSystem(fs); | ||
|
|
||
| await expect(limiter.createDir("/dir")).rejects.toThrow("429 Too Many Requests"); | ||
| expect(fs.createDir).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should not retry writer.write on 429", async () => { | ||
| const fs = createFs(); | ||
| const write = vi.fn(async () => {}); | ||
| vi.mocked(fs.create).mockResolvedValueOnce({ | ||
| write, | ||
| }); | ||
| write.mockRejectedValueOnce(new Error("429 Too Many Requests")); | ||
| const limiter = new LimiterFileSystem(fs); | ||
| const writer = await limiter.create(file.path); | ||
|
|
||
| await expect(writer.write("content")).rejects.toThrow("429 Too Many Requests"); | ||
| expect(write).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should retry reader.read on 429", async () => { | ||
| vi.useFakeTimers(); | ||
| const fs = createFs(); | ||
| const read = vi.fn(async () => "content"); | ||
| vi.mocked(fs.open).mockResolvedValueOnce({ | ||
| read, | ||
| }); | ||
| read.mockRejectedValueOnce(new Error("429 Too Many Requests")); | ||
| read.mockResolvedValueOnce("content"); | ||
| const limiter = new LimiterFileSystem(fs); | ||
| const reader = await limiter.open(file); | ||
|
|
||
| const promise = reader.read("string"); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
|
||
| await expect(promise).resolves.toBe("content"); | ||
| expect(read).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前新增的“按操作类型决定 429 是否重试”的策略里,
RETRYABLE_429_OPS覆盖了verify/open/openDir/getDirUrl等操作,但测试只覆盖了list/read会重试以及delete/createDir/write不重试。建议补充至少对verify/open/openDir/getDirUrl的 429 重试测试,以及对create本身(fs.create 抛 429)不重试的测试,避免后续字符串/集合项改动导致策略悄然失效。