mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-21 22:32:22 +03:00
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) Two shards on release/v3.8.51 went red in one day with the same signature — "ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only .github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass alone and on re-run: the cleanup races something still writing into the directory (SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner the window opens. 1154 test files do their own cleanup with fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries. One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record): every rm / rmSync / rmdirSync option object with `recursive: true` and no `maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292 files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included. Only the option object changes: no call site, assertion or import is touched. Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292 files; a random 20-file sample runs green (quota-redis-store hangs identically on the untouched tree — it needs a Redis on localhost, an environment matter). The four unit shards on this PR are the full run. * fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff The gate shells out to `git diff` through execFileSync with Node's default 1 MB maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing anything. 64 MB is far above any real PR and costs nothing when unused.
167 lines
5.9 KiB
TypeScript
167 lines
5.9 KiB
TypeScript
/**
|
|
* tests/unit/serial/combo-strategy-fallbacks-half-open-timing.test.ts
|
|
*
|
|
* Extracted from tests/unit/combo-strategy-fallbacks.test.ts (#6803).
|
|
*
|
|
* This scenario races a real 80ms setTimeout against a 40ms circuit-breaker
|
|
* resetTimeout before asserting breaker.getStatus().state === 'HALF_OPEN'.
|
|
* Under a starved event loop (CI-runner CPU contention from concurrent
|
|
* sibling shard jobs) this timing margin can be missed even though the
|
|
* lazy-recovery contract (OPEN → HALF_OPEN once the reset timeout elapses) is
|
|
* implemented correctly.
|
|
*
|
|
* Running this in tests/unit/serial/ (--test-concurrency=1, see package.json's
|
|
* test:unit:serial) removes the intra-suite parallelism that was the dominant
|
|
* source of contention, matching the repo's established remedy pattern for
|
|
* this class of test.
|
|
*/
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), "omniroute-combo-fallbacks-half-open-")
|
|
);
|
|
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const { handleComboChat } = await import("../../../open-sse/services/combo.ts");
|
|
const core = await import("../../../src/lib/db/core.ts");
|
|
const settingsDb = await import("../../../src/lib/db/settings.ts");
|
|
const { resetAllComboMetrics } = await import("../../../open-sse/services/comboMetrics.ts");
|
|
const { resetAllCircuitBreakers, getCircuitBreaker } =
|
|
await import("../../../src/shared/utils/circuitBreaker.ts");
|
|
const { resetAll: resetAllSemaphores } =
|
|
await import("../../../open-sse/services/rateLimitSemaphore.ts");
|
|
const { _resetAllDecks } = await import("../../../src/shared/utils/shuffleDeck.ts");
|
|
const { clearSessions } = await import("../../../open-sse/services/sessionManager.ts");
|
|
|
|
type LogEntry = { level: string; tag: unknown; msg: unknown };
|
|
|
|
function createLog() {
|
|
const entries: LogEntry[] = [];
|
|
return {
|
|
info: (tag: unknown, msg: unknown) => entries.push({ level: "info", tag, msg }),
|
|
warn: (tag: unknown, msg: unknown) => entries.push({ level: "warn", tag, msg }),
|
|
error: (tag: unknown, msg: unknown) => entries.push({ level: "error", tag, msg }),
|
|
debug: (tag: unknown, msg: unknown) => entries.push({ level: "debug", tag, msg }),
|
|
entries,
|
|
};
|
|
}
|
|
|
|
function okResponse(body: unknown = { choices: [{ message: { content: "ok" } }] }) {
|
|
return new Response(JSON.stringify(body), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
async function cleanupTestDataDir() {
|
|
let lastError: unknown;
|
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
try {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
return;
|
|
} catch (error: unknown) {
|
|
lastError = error;
|
|
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
}
|
|
}
|
|
if (lastError) throw lastError;
|
|
}
|
|
|
|
test.beforeEach(async () => {
|
|
resetAllComboMetrics();
|
|
resetAllCircuitBreakers();
|
|
resetAllSemaphores();
|
|
_resetAllDecks();
|
|
clearSessions();
|
|
await cleanupTestDataDir();
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
await settingsDb.resetAllPricing();
|
|
settingsDb.clearAllLKGP();
|
|
});
|
|
|
|
test.after(async () => {
|
|
resetAllComboMetrics();
|
|
resetAllCircuitBreakers();
|
|
resetAllSemaphores();
|
|
_resetAllDecks();
|
|
settingsDb.clearAllLKGP();
|
|
if (ORIGINAL_DATA_DIR === undefined) {
|
|
delete process.env.DATA_DIR;
|
|
} else {
|
|
process.env.DATA_DIR = ORIGINAL_DATA_DIR;
|
|
}
|
|
await cleanupTestDataDir();
|
|
});
|
|
|
|
test("combo skips a provider while its breaker is OPEN and attempts it again after the reset timeout (HALF_OPEN)", async () => {
|
|
// Widened from the original 40ms/80ms margin (#6803): under contended
|
|
// CI-runner load even --test-concurrency=1 doesn't guarantee the "while
|
|
// OPEN" dispatch completes before a 40ms window elapses. A larger absolute
|
|
// margin (same ~2x wait:resetTimeout ratio) tolerates real scheduling
|
|
// jitter while still proving the lazy-recovery contract.
|
|
const breaker = getCircuitBreaker("openai", { failureThreshold: 1, resetTimeout: 300 });
|
|
try {
|
|
await breaker.execute(async () => {
|
|
throw new Error("simulated provider failure");
|
|
});
|
|
} catch {
|
|
// expected — trips the breaker OPEN
|
|
}
|
|
assert.equal(breaker.getStatus().state, "OPEN");
|
|
|
|
const comboDef = {
|
|
name: "half-open-recovery",
|
|
strategy: "priority",
|
|
models: ["openai/gpt-4o-mini", "claude/sonnet"],
|
|
config: { maxRetries: 0, retryDelayMs: 0, fallbackDelayMs: 0 },
|
|
};
|
|
|
|
// While OPEN: the openai target must be skipped, claude serves.
|
|
const callsWhileOpen: string[] = [];
|
|
const blocked = await handleComboChat({
|
|
body: {},
|
|
combo: comboDef,
|
|
handleSingleModel: async (_body: unknown, modelStr: string) => {
|
|
callsWhileOpen.push(modelStr);
|
|
return okResponse();
|
|
},
|
|
isModelAvailable: async () => true,
|
|
log: createLog(),
|
|
settings: null,
|
|
allCombos: null,
|
|
});
|
|
assert.equal(blocked.ok, true);
|
|
assert.deepEqual(callsWhileOpen, ["claude/sonnet"], "OPEN breaker target must be skipped");
|
|
|
|
// After the reset timeout the breaker reads HALF_OPEN — the combo must probe
|
|
// the provider again instead of excluding it forever (lazy recovery contract).
|
|
await new Promise((resolve) => setTimeout(resolve, 600));
|
|
assert.equal(breaker.getStatus().state, "HALF_OPEN");
|
|
|
|
const callsAfterExpiry: string[] = [];
|
|
const probed = await handleComboChat({
|
|
body: {},
|
|
combo: comboDef,
|
|
handleSingleModel: async (_body: unknown, modelStr: string) => {
|
|
callsAfterExpiry.push(modelStr);
|
|
return okResponse();
|
|
},
|
|
isModelAvailable: async () => true,
|
|
log: createLog(),
|
|
settings: null,
|
|
allCombos: null,
|
|
});
|
|
assert.equal(probed.ok, true);
|
|
assert.deepEqual(
|
|
callsAfterExpiry,
|
|
["openai/gpt-4o-mini"],
|
|
"HALF_OPEN provider must be probed again"
|
|
);
|
|
});
|