test: 운영 시간 상수를 실제 시계로 기다리는 테스트 5개를 빠르게 한다

문제
테스트 5개가 운영 코드의 시간 상수를 실제 시계로 기다린다.
- tests/unit/service-worker-runtime.test.ts "bounds a stream that never
  produces a chunk": ACTIVATION_MARKER_READ_DEADLINE_MS(5초)를 그대로
  기다린다. 10초 제한 중 5초를 쓴다.
- tests/unit/browser-rpc/browser-rpc-remediation.test.ts "keeps the
  operation DRAINING when waitClosed …" 4개: 닫힘 확인이 없으면 stream
  정리가 STREAM_CLEANUP_BOUND_MS(1초)를 기다린다. 각 1초.

고른 방법과 버린 대안
- SW 테스트: 이 테스트만 vi.useFakeTimers()로 돌리고 deadline을
  advanceTimersByTimeAsync로 넘긴다. 마커를 여러 번 읽을 수 있어
  끝날 때까지 5초씩 최대 4번 넘긴다. deadline이 없으면 활성화가 끝나지
  않아 "settled" 단언에서 실패한다.
- RPC 테스트: 런타임은 이미 ClockPort를 주입받는다. 모든 대기를 30ms로
  자르는 시스템 시계를 넘긴다. 작업 자체의 25ms deadline은 그 아래라
  그대로다.
- SW lifecycle이 deadline을 의존성으로 받게 하는 안은 버렸다. 운영
  코드에 테스트용 손잡이를 하나 더 열어야 한다.
- 상수 값(5,000·1,000)은 운영 코드 한 곳에 그대로 있다.

위험·한계
- 가짜 시계가 stream·마이크로태스크 순서와 얽히면 SW 테스트가
  불안정해질 수 있다. 그때는 실제 시계로 되돌린다(1101/10-07 재검토
  조건).
- 완료 기준(각 100ms 아래)은 junit 시간으로 desktop에서 본다.

검증
- desktop batch에서 확인:
  node scripts/run-vitest.ts run tests/unit/service-worker-runtime.test.ts tests/unit/browser-rpc/browser-rpc-remediation.test.ts --reporter=junit --outputFile.junit=artifacts/tests/r1105-10-07.xml
- 실행 시간만 바꾼 커밋이라 RED는 없다. 두 파일이 통과하고 다섯
  테스트가 각각 100ms 아래여야 한다.

Refs: 1101/10-07
This commit is contained in:
DongHyeonka
2026-11-06 01:05:22 +09:00
parent 7ead8d369a
commit ce191bef4b
2 changed files with 48 additions and 15 deletions
@@ -8,6 +8,8 @@ import {
type BrowserRpcTransport,
} from "../../../src/adapters/browser-rpc/index.ts";
import { decodeServerStreamLease } from "../../../src/adapters/browser-rpc/transport.ts";
import { systemClock } from "../../../src/adapters/platform/system-clock.ts";
import type { ClockPort } from "../../../src/application/ports/clock-port.ts";
import { installBrowserRpcContractBindings } from "../../../src/contracts/browser-rpc.ts";
import {
MAPPERS,
@@ -539,6 +541,18 @@ describe("RPC-01 only a positive receipt confirms physical closure", () => {
{ label: "never settles", waitClosed: () => new Promise<void>(() => {}) },
];
/**
* 1101/10-07. The system clock with every wait capped at 30 ms. Without a
* positive receipt the stream cleanup waits out its 1 s bound; capped, the
* test passes that bound instead of sleeping through it. The operation's own
* 25 ms deadlines are below the cap and unchanged.
*/
const cappedClock: ClockPort = Object.freeze({
now: () => systemClock.now(),
sleep: (milliseconds: number, signal?: AbortSignal) =>
systemClock.sleep(Math.min(milliseconds, 30), signal),
});
for (const { label, waitClosed } of negativeReceipts) {
it(`keeps the operation DRAINING when waitClosed ${label}`, async () => {
const probe = leaseProbeWith(waitClosed);
@@ -549,6 +563,7 @@ describe("RPC-01 only a positive receipt confirms physical closure", () => {
mappers: MAPPERS,
requestEncoders: { RpcResourceStreamRequestEncoder: STREAM_ENCODER },
transports: { CONNECT_REFERENCE_STREAM: probe.transport },
clock: cappedClock,
});
const stream = runtime.bindServerStream(
"WATCH_RPC_RESOURCES",
+33 -15
View File
@@ -2004,22 +2004,40 @@ describe("SW-01 the activation marker read is bounded in bytes", () => {
});
it("bounds a stream that never produces a chunk", async () => {
let cancels = 0;
const stalled = new ReadableStream<Uint8Array>({
pull() {
return new Promise<void>(() => {});
},
cancel() {
cancels += 1;
},
});
const fixture = scopeWithMarkerBody(stalled);
// 1101/10-07. The read deadline is the runtime's own 5 s constant. The
// clock is faked, so the test passes the deadline instead of waiting it
// out; without a deadline the activation never settles and this fails.
vi.useFakeTimers();
try {
let cancels = 0;
const stalled = new ReadableStream<Uint8Array>({
pull() {
return new Promise<void>(() => {});
},
cancel() {
cancels += 1;
},
});
const fixture = scopeWithMarkerBody(stalled);
let settled = false;
await expect(
runtimeFor(fixture.scope).onActivate(),
).resolves.toBeTypeOf("number");
expect(cancels).toBeGreaterThanOrEqual(1);
}, 10_000);
const activation = runtimeFor(fixture.scope)
.onActivate()
.finally(() => {
settled = true;
});
// One read deadline per marker probe.
for (let probe = 0; probe < 4 && !settled; probe += 1) {
await vi.advanceTimersByTimeAsync(5_000);
}
expect(settled).toBe(true);
await expect(activation).resolves.toBeTypeOf("number");
expect(cancels).toBeGreaterThanOrEqual(1);
} finally {
vi.useRealTimers();
}
});
it("cancels the body it refuses for a declared oversize", async () => {
let cancels = 0;