Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(llm-proxy): drain queued visual waiters in any grant order

Alfredo Lopez6e09f40f69c3
diff --git a/codex-llm-proxy/test/requestLimits.test.ts b/codex-llm-proxy/test/requestLimits.test.ts
index abd77dcd5..8c6bbfa68 100644
--- a/codex-llm-proxy/test/requestLimits.test.ts
+++ b/codex-llm-proxy/test/requestLimits.test.ts
@@ -425,7 +425,9 @@ describe('StreamGate', () => {
// Queue capacity: the caller after maxQueuedRequests is refused.
await expect(visualStreamGate.acquire()).rejects.toMatchObject({ kind: 'queue_full' })
for (const release of held) release()
- for (const waiter of queued) (await waiter)()
+ // The gate grants a freed slot to whichever waiter polls first, so each
+ // waiter releases its slot as soon as it gets one, in any order.
+ await Promise.all(queued.map(async waiter => (await waiter)()))
expect(visualStreamGate.snapshot()).toEqual({ running: 0, queued: 0 })
})
})
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index 5cd4a0380..e2d8b59b9 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
import jwt from 'jsonwebtoken'
import { generateKeyPairSync } from 'node:crypto'
import { createServer, request as httpRequest } from 'node:http'
@@ -2198,6 +2198,13 @@ describe('codex-llm-proxy ticket-aware stream-gate wait (#739 D1-bis)', () => {
const lookup = async () => [{ address: '1.2.3.4', family: 4 }]
const COMPLETIONS = '/internal/runtime/v1/codex/completions'
+ // The visual gate is module state shared by every test here. A test that
+ // leaves a slot or a waiter behind fails the next test by name here, instead
+ // of letting it queue behind the leftover until its timeout.
+ beforeEach(() => {
+ expect(visualStreamGate.snapshot()).toEqual({ running: 0, queued: 0 })
+ })
+
async function until(condition: () => boolean, what: string): Promise<void> {
const deadline = performance.now() + 5_000
while (!condition()) {
@@ -2509,12 +2516,16 @@ describe('codex-llm-proxy ticket-aware stream-gate wait (#739 D1-bis)', () => {
const warn = vi.spyOn(logger, 'warn')
const held: Array<() => void> = []
const waiting: Array<Promise<() => void>> = []
+ // The gate grants a freed slot to whichever waiter polls first, not in
+ // queue order. Cleanup therefore aborts every waiter before it frees the
+ // held slots, so no waiter can take a slot that cleanup would then wait on.
+ const drain = new AbortController()
try {
for (let i = 0; i < VISUAL_STREAM_LIMITS.maxConcurrentStreams; i += 1) {
held.push(await visualStreamGate.acquire())
}
for (let i = 0; i < VISUAL_STREAM_LIMITS.maxQueuedRequests; i += 1) {
- const acquired = visualStreamGate.acquire()
+ const acquired = visualStreamGate.acquire(drain.signal)
acquired.catch(() => {})
waiting.push(acquired)
}
@@ -2544,11 +2555,9 @@ describe('codex-llm-proxy ticket-aware stream-gate wait (#739 D1-bis)', () => {
queued: VISUAL_STREAM_LIMITS.maxQueuedRequests,
})
} finally {
+ drain.abort()
+ await Promise.allSettled(waiting)
for (const release of held.splice(0)) release()
- for (const acquired of waiting) {
- const release = await acquired.catch(() => undefined)
- release?.()
- }
warn.mockRestore()
}
}, 30_000)
diff --git a/grok-llm-proxy/test/requestLimits.test.ts b/grok-llm-proxy/test/requestLimits.test.ts
index bb7938658..e451b11bf 100644
--- a/grok-llm-proxy/test/requestLimits.test.ts
+++ b/grok-llm-proxy/test/requestLimits.test.ts
@@ -419,7 +419,9 @@ describe('StreamGate', () => {
expect(gate.snapshot()).toEqual({ running: 1, queued: 4 })
await expect(gate.acquire()).rejects.toBeInstanceOf(RequestLimitError)
held()
- for (const waiter of queued) (await waiter)()
+ // The gate grants a freed slot to whichever waiter polls first, so each
+ // waiter releases its slot as soon as it gets one, in any order.
+ await Promise.all(queued.map(async waiter => (await waiter)()))
expect(gate.snapshot()).toEqual({ running: 0, queued: 0 })
})
@@ -437,7 +439,9 @@ describe('StreamGate', () => {
// Queue capacity: the caller after maxQueuedRequests is refused.
await expect(visualStreamGate.acquire()).rejects.toMatchObject({ kind: 'queue_full' })
for (const release of held) release()
- for (const waiter of queued) (await waiter)()
+ // The gate grants a freed slot to whichever waiter polls first, so each
+ // waiter releases its slot as soon as it gets one, in any order.
+ await Promise.all(queued.map(async waiter => (await waiter)()))
expect(visualStreamGate.snapshot()).toEqual({ running: 0, queued: 0 })
})
})