Skip to content

Commit 2a9c3d0

Browse files
author
easonLiangWorldedtech
committed
test(openai-native): poll the observable instead of sleeping at three abort sites
Three of the seven rule-banned fixed sleeps in the merged main spec become polls on named observables, each with an explicit timeout and a reason: 517 waits for the fetch double to have been called before aborting mid-request; 542 waits for the first request to register an in-flight stream; 559 waits for both, and the original length assertion is kept because polling replaces the sleep, not the assertion. An explicit timeout is what turns a condition that never fires into a failed test instead of a slow green. The rule (packages/config-eslint/api-provider-hygiene.js) names exactly one global, setTimeout, so a macrotask yield is not what it forbids; but three remaining sites use the banned API to do work the rule's message does not describe - a macrotask-phase yield an abort bridge depends on, a second immediate yield, and a hand-written race between a deadline and an abort event - and one stream-live poll resisted measurement here. No suppression was added: repo-wide eslint reports 4 banned-globals errors instead, eslint-suppressions.json still holds only its single no-explicit-any count of 74 for this path across 265 paths, and no-restricted-globals is absent from that file entirely. Measurement: focused spec under vitest.api.config.ts passes 128 of 128 in 2.48s against a 2.32s baseline on the unedited head - 0.16s, not a multiplication.
1 parent 6e43f34 commit 2a9c3d0

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎src/api/providers/__tests__/openai-native.spec.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ describe("OpenAiNativeHandler", () => {
515515
)
516516

517517
const collected = collectStream(stream)
518-
setTimeout(() => controller.abort(), 10)
518+
// The abort must land mid-request, so poll the observable that the request
519+
// exists - the fetch double having been called - instead of a wall-clock delay.
520+
// The explicit timeout is what makes a condition that never fires fail the test.
521+
await vi.waitFor(() => mockFetch.mock.calls.length > 0, { timeout: 2000, interval: 25 })
522+
controller.abort()
519523

520524
await expect(collected).rejects.toMatchObject({ name: "AbortError" })
521525
})
@@ -540,7 +544,9 @@ describe("OpenAiNativeHandler", () => {
540544
makeCreateMessageMetadata({ abortSignal: firstController.signal }),
541545
)
542546
const firstCollected = collectStream(firstStream)
543-
await new Promise((resolve) => setTimeout(resolve, 10))
547+
// Observable: the first request has registered an in-flight stream. The
548+
// explicit timeout keeps a condition that never fires from idling to green.
549+
await vi.waitFor(() => openStreams.length >= 1, { timeout: 2000, interval: 25 })
544550
requireController(0).enqueue(
545551
new TextEncoder().encode('data: {"type":"response.text.delta","delta":"one"}\n\n'),
546552
)
@@ -557,7 +563,9 @@ describe("OpenAiNativeHandler", () => {
557563
makeCreateMessageMetadata({ abortSignal: secondController.signal }),
558564
)
559565
const secondCollected = collectStream(secondStream)
560-
await new Promise((resolve) => setTimeout(resolve, 10))
566+
// Observable: both requests are in flight; the length assertion stays because
567+
// polling replaces the sleep, not the assertion.
568+
await vi.waitFor(() => openStreams.length >= 2, { timeout: 2000, interval: 25 })
561569
expect(openStreams).toHaveLength(2)
562570

563571
// Aborting the FIRST request's signal must not leak into the second request.

0 commit comments

Comments
 (0)