From 37a850040d68d7ffe022e623753d8e3b174fbecf Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 22:26:17 +0900 Subject: [PATCH] =?UTF-8?q?test(http):=20deadline=20=EB=B6=84=EB=A5=98=20?= =?UTF-8?q?=EC=8B=9C=ED=97=98=EC=9D=B4=20=EB=8C=80=EA=B8=B0=20=EC=A7=80?= =?UTF-8?q?=EC=A0=90=EC=97=90=EC=84=9C=20=EA=B0=80=EC=A7=9C=20=EC=8B=9C?= =?UTF-8?q?=EA=B3=84=EB=A1=9C=20deadline=EC=9D=84=20=EB=82=B8=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 「times out at the wait when the deadline fires」가 실제 시계로 30ms deadline을 잡는다. 전송 실패(transport backoff)나 503 (retry backoff)을 처리하는 시점에 이미 30ms가 지났으면 실행기는 backoff할 시간이 없다고 보고 그 실패로 끝낸다(`remaining() > 0`이 거짓). 그래서 기대한 TIMEOUT 대신 NETWORK_FAILURE·503 결과가 나온다. desktop 부하 31에서 한 번 떨어졌다(PR #19 W2-FE 기록 F). 고른 방법: deadline 경우만 가짜 시계(`vi.useFakeTimers`)로 돌고, 그 대기에 닿은 것을 확인한 뒤 30ms를 진행한다. 시험이 보려는 것(대기 중에 deadline이 나면 DEADLINE·TIMEOUT)을 그대로 보고 기계 속도에는 기대지 않는다. 두 backoff 단계는 답하기 전에 실제 시간 40ms를 멈춘다 (`Atomics.wait`). 실제 시계로 되돌리면 이 시험이 매번 실패한다. - 버린 대안: deadline을 늘리기. 경쟁이 남고 규칙 밖이다. - 버린 대안: `vi.waitFor`로 기다리기. 가짜 시계에서는 확인할 때마다 시계를 진행해 대기에 닿기 전에 deadline이 날 수 있다. 위험·한계: 실행기 동작은 바꾸지 않았다. deadline이 이미 지난 뒤 도착한 전송 실패를 NETWORK_FAILURE로 보고하는 것이 맞는지는 이 커밋이 정하지 않는다. 검증(desktop): 시험 파일 72개 3회 연속 통과. 변이: 가짜 시계를 빼고 실제 30ms deadline에 같은 40ms 멈춤을 두면 transport backoff·retry backoff 둘이 매번 실패(나머지 넷 통과). eslint·check:types rc=0. 로그 /tmp/claude-gates/w2fe2-http1·2/. --- tests/unit/http-execution-v3.test.ts | 39 +++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/tests/unit/http-execution-v3.test.ts b/tests/unit/http-execution-v3.test.ts index ee085b00..bf5a2f60 100644 --- a/tests/unit/http-execution-v3.test.ts +++ b/tests/unit/http-execution-v3.test.ts @@ -972,8 +972,29 @@ describe("terminal owner classification at every wait", () => { type Owner = "CALLER" | "SCOPE_FENCE" | "DEADLINE"; const hang = () => new Promise(() => {}); + /** + * Blocks this thread for `ms` of real time, as a loaded machine does + * between two steps. A fake clock does not move meanwhile. + */ + const stallRealTime = (ms: number) => + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); async function endWaitAt(phase: Phase, owner: Owner) { + // The deadline fires on a fake clock once the wait under test is reached. + // On the real clock a 30ms deadline raced the machine: a transport failure + // or a 503 handled after 30ms found no time left to back off, and the + // request ended as that failure instead of TIMEOUT (seen at load 31). + // The backoff phases stall 40ms of real time before answering, so a real + // deadline here fails every time. + if (owner === "DEADLINE") vi.useFakeTimers(); + try { + return await endWaitAtOnClock(phase, owner); + } finally { + if (owner === "DEADLINE") vi.useRealTimers(); + } + } + + async function endWaitAtOnClock(phase: Phase, owner: Owner) { const caller = new AbortController(); const fence = new AbortController(); const fencedScope = Object.freeze({ @@ -1008,6 +1029,7 @@ describe("terminal owner classification at every wait", () => { ? (reach(), hang()) : { kind: "READY", headers: {} }, fetcher: vi.fn(async () => { + if (owner === "DEADLINE" && phase.endsWith("backoff")) stallRealTime(40); switch (phase) { case "transport backoff": throw new TypeError("network down"); @@ -1043,12 +1065,17 @@ describe("terminal owner classification at every wait", () => { ...(execution === undefined ? {} : { execution }), }, ); - if (owner !== "DEADLINE") { - await vi.waitFor(() => - expect(reached || execution?.traffic.concurrency.queued === 1).toBe( - true, - ), - ); + const waiting = () => + reached || execution?.traffic.concurrency.queued === 1; + if (owner === "DEADLINE") { + // vi.waitFor would advance the fake clock on every check. + for (let turn = 0; turn < 100 && !waiting(); turn += 1) { + await flushMicrotasks(); + } + expect(waiting()).toBe(true); + await vi.advanceTimersByTimeAsync(30); + } else { + await vi.waitFor(() => expect(waiting()).toBe(true)); (owner === "CALLER" ? caller : fence).abort(); } const outcome = await pending;