Repository navigation
test(abort): Add AbortSignal.any advanced tests #25407
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
Open
tennisleng
wants to merge
8
commits into
oven-sh:main
Choose a base branch
from
tennisleng:feat/bun-abort-advanced
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf30e3d
test(abort): Add AbortSignal.any basic tests
tennisleng 849c43d
test(abort): Add AbortSignal.any advanced tests
tennisleng a4baeda
fix: remove meta-commentary from test comment
tennisleng 6f1924f
fix: address CodeRabbit review comments
tennisleng 1b9524b
Merge branch 'main' into feat/bun-abort-advanced
tennisleng 97b9e67
Merge branch 'main' into feat/bun-abort-advanced
tennisleng 7fed168
Merge branch 'main' into feat/bun-abort-advanced
tennisleng 8ede2b0
Merge branch 'main' into feat/bun-abort-advanced
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,206 @@ | ||
| 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", () => { | ||
| // Table-driven tests for different reason types | ||
| const reasonCases = [ | ||
| { name: "Error", reason: new Error("custom abort reason") }, | ||
| { name: "string", reason: "string reason" }, | ||
| { name: "object", reason: { code: 42, message: "custom object" } }, | ||
| ]; | ||
|
|
||
| test.each(reasonCases)("should propagate $name reasons", ({ reason }) => { | ||
| const controller = new AbortController(); | ||
| // @ts-ignore | ||
| const composite = AbortSignal.any([controller.signal]); | ||
| controller.abort(reason); | ||
| expect(composite.reason).toBe(reason); | ||
| }); | ||
|
|
||
| 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"); | ||
| }); | ||
| }); | ||
|
|
||
| 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)); | ||
|
|
||
| // Abort fires synchronously, so the event listener is called before abort() returns | ||
| controller.abort(); | ||
|
|
||
| const result = await promise; | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| test("should only fire abort event once even with multiple source aborts", () => { | ||
| 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++); | ||
|
|
||
| // Abort events fire synchronously, so no need to wait | ||
| controller1.abort(); | ||
| controller2.abort(); | ||
|
|
||
| expect(abortCount).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| 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); | ||
|
|
||
| // Wait for abort event instead of arbitrary sleep | ||
| const { promise, resolve } = Promise.withResolvers<void>(); | ||
| composite.addEventListener("abort", () => resolve()); | ||
|
|
||
| await promise; | ||
|
|
||
| expect(composite.aborted).toBe(true); | ||
| expect(composite.reason).toBeInstanceOf(DOMException); | ||
| expect(composite.reason.name).toBe("TimeoutError"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("should prefer manual abort over timeout if it comes first", () => { | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.