-
Notifications
You must be signed in to change notification settings - Fork 3.8k
test: add comprehensive AbortSignal.any() test coverage #25341
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
Closed
tennisleng
wants to merge
4
commits into
oven-sh:main
from
tennisleng:test/abort-signal-any-comprehensive-coverage
+204
−0
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae1225d
test: add comprehensive AbortSignal.any() test coverage
5388fec
test: remove setTimeout fallback per Bun test guidelines
dfe9966
refactor: use test.each for reason propagation tests
b78bbb8
Merge branch 'main' into test/abort-signal-any-comprehensive-coverage
tennisleng 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
|
||
| /** | ||
| * Comprehensive tests for AbortSignal.any() | ||
| * | ||
| * AbortSignal.any() creates a composite signal that aborts when any of the | ||
| * provided signals abort. This test suite covers edge cases and ensures | ||
| * standards-compliant behavior. | ||
| */ | ||
| describe("AbortSignal.any()", () => { | ||
| describe("basic functionality", () => { | ||
| test("should return a non-aborted signal for empty array", () => { | ||
| // @ts-ignore - TypeScript may not have this typed | ||
| const signal = AbortSignal.any([]); | ||
| expect(signal).toBeInstanceOf(AbortSignal); | ||
| expect(signal.aborted).toBe(false); | ||
| }); | ||
|
|
||
| test("should follow a single signal", () => { | ||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
|
|
||
| expect(composite.aborted).toBe(false); | ||
| controller.abort(); | ||
| expect(composite.aborted).toBe(true); | ||
| }); | ||
|
|
||
| test("should abort when any of multiple signals abort", () => { | ||
| const controller1 = new AbortController(); | ||
| const controller2 = new AbortController(); | ||
| const controller3 = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([ | ||
| controller1.signal, | ||
| controller2.signal, | ||
| controller3.signal | ||
| ]); | ||
|
|
||
| expect(composite.aborted).toBe(false); | ||
| controller2.abort("middle signal aborted"); | ||
| expect(composite.aborted).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("already-aborted signals", () => { | ||
| test("should immediately abort if any input signal is already aborted", () => { | ||
| const abortedController = new AbortController(); | ||
| abortedController.abort("pre-aborted"); | ||
|
|
||
| const freshController = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([ | ||
| freshController.signal, | ||
| abortedController.signal | ||
| ]); | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| }); | ||
|
|
||
| test("should use AbortSignal.abort() result correctly", () => { | ||
| const alreadyAborted = AbortSignal.abort("already aborted reason"); | ||
| const controller = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal, alreadyAborted]); | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| expect(composite.reason).toBe("already aborted reason"); | ||
| }); | ||
|
|
||
| test("should work with all signals already aborted", () => { | ||
| const aborted1 = AbortSignal.abort("first"); | ||
| const aborted2 = AbortSignal.abort("second"); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([aborted1, aborted2]); | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| // First aborted signal's reason should be used | ||
| expect(composite.reason).toBe("first"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("reason propagation", () => { | ||
| test("should propagate the reason from the aborting signal", () => { | ||
| const controller1 = new AbortController(); | ||
| const controller2 = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller1.signal, controller2.signal]); | ||
|
|
||
| const customReason = new Error("custom abort reason"); | ||
| controller1.abort(customReason); | ||
|
|
||
| expect(composite.reason).toBe(customReason); | ||
| }); | ||
|
|
||
| test("should use DOMException for default abort reason", () => { | ||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
|
|
||
| controller.abort(); | ||
|
|
||
| expect(composite.reason).toBeInstanceOf(DOMException); | ||
| expect(composite.reason.name).toBe("AbortError"); | ||
| }); | ||
|
|
||
| test("should propagate string reasons", () => { | ||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
|
|
||
| controller.abort("string reason"); | ||
|
|
||
| expect(composite.reason).toBe("string reason"); | ||
| }); | ||
|
|
||
| test("should propagate object reasons", () => { | ||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
|
|
||
| const objectReason = { code: 42, message: "custom object" }; | ||
| controller.abort(objectReason); | ||
|
|
||
| expect(composite.reason).toBe(objectReason); | ||
| }); | ||
| }); | ||
|
|
||
| describe("event handling", () => { | ||
| test("should fire abort event when composite aborts", async () => { | ||
| const { promise, resolve } = Promise.withResolvers<boolean>(); | ||
|
|
||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
|
|
||
| composite.addEventListener("abort", () => resolve(true)); | ||
|
|
||
| const timeout = setTimeout(() => resolve(false), 100); | ||
| controller.abort(); | ||
|
|
||
| const result = await promise; | ||
| clearTimeout(timeout); | ||
| expect(result).toBe(true); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test("should only fire abort event once even with multiple source aborts", async () => { | ||
| let abortCount = 0; | ||
|
|
||
| const controller1 = new AbortController(); | ||
| const controller2 = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller1.signal, controller2.signal]); | ||
|
|
||
| composite.addEventListener("abort", () => abortCount++); | ||
|
|
||
| controller1.abort(); | ||
| controller2.abort(); | ||
|
|
||
| // Wait a tick to ensure all events have fired | ||
| await Bun.sleep(10); | ||
|
|
||
| expect(abortCount).toBe(1); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| describe("nested AbortSignal.any()", () => { | ||
| test("should work with nested any() calls", () => { | ||
| const controller1 = new AbortController(); | ||
| const controller2 = new AbortController(); | ||
| const controller3 = new AbortController(); | ||
|
|
||
| // @ts-ignore | ||
| const nested = AbortSignal.any([controller1.signal, controller2.signal]); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([nested, controller3.signal]); | ||
|
|
||
| expect(composite.aborted).toBe(false); | ||
|
|
||
| controller2.abort("from nested"); | ||
|
|
||
| expect(nested.aborted).toBe(true); | ||
| expect(composite.aborted).toBe(true); | ||
| expect(composite.reason).toBe("from nested"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("with AbortSignal.timeout()", () => { | ||
| test("should work with timeout signals", async () => { | ||
| const controller = new AbortController(); | ||
| const timeoutSignal = AbortSignal.timeout(50); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal, timeoutSignal]); | ||
|
|
||
| expect(composite.aborted).toBe(false); | ||
|
|
||
| await Bun.sleep(100); | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| expect(composite.reason).toBeInstanceOf(DOMException); | ||
| expect(composite.reason.name).toBe("TimeoutError"); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test("should prefer manual abort over timeout if it comes first", async () => { | ||
| const controller = new AbortController(); | ||
| const timeoutSignal = AbortSignal.timeout(1000); | ||
|
|
||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal, timeoutSignal]); | ||
|
|
||
| controller.abort("manual abort"); | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| expect(composite.reason).toBe("manual abort"); | ||
| }); | ||
| }); | ||
| }); | ||
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.
🧹 Nitpick | 🔵 Trivial
Optional: consolidate reason propagation tests with
test.eachThe string, object, and custom-error reason tests all follow the same structure. You could reduce boilerplate and make it easier to add more cases later by using a table-driven
test.each:Purely stylistic; the current tests are fine functionally.
As per coding guidelines, using
test.eachfor similar cases is encouraged.🤖 Prompt for AI Agents