Files
OmniRoute/tests/unit/lib/warmupScheduler/circuitBreakerFactoryRelease.test.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

98 lines
3.4 KiB
TypeScript

/**
* getCircuitBreakerStore() must release the ioredis client it built when the
* probe fails partway through, not only when the probe succeeds.
*
* One ioredis case per file, and this is the whole reason: a client left behind
* by an earlier case in the same process wedges every later connect, so a second
* one here hangs rather than fails. Measured both ways round -- reordering does
* not help, only a fresh process does, and `node:test` gives each file one.
*/
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";
import net from "node:net";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-warmup-release-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.NODE_ENV = "test";
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
const core = await import("../../../../src/lib/db/core.ts");
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
/**
* A Redis that finishes the handshake and then refuses PING, so the probe fails
* at a point where a live client already exists -- the only way to observe
* whether that client gets released. Closure is reported from the server side,
* since the client itself is private to the factory.
*
* INFO is answered for real. Refusing it too leaves ioredis waiting on a
* ready-check that `connectTimeout` does not bound.
*/
function startProbeRefusingRedis(): Promise<{
port: number;
socketClosed: () => boolean;
close: () => void;
}> {
return new Promise((resolve) => {
let closed = false;
const server = net.createServer((socket) => {
socket.on("close", () => {
closed = true;
});
socket.on("error", () => {});
socket.on("data", (buf) => {
if (buf.toString().toLowerCase().includes("info")) {
const body = "redis_version:7.0.0\r\n";
socket.write(`$${body.length}\r\n${body}\r\n`);
return;
}
socket.write("-ERR probe refused\r\n");
});
});
server.listen(0, "127.0.0.1", () => {
const { port } = server.address() as { port: number };
resolve({ port, socketClosed: () => closed, close: () => server.close() });
});
});
}
async function waitUntil(cond: () => boolean, timeoutMs: number): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (!cond() && Date.now() < deadline) {
await new Promise((r) => setTimeout(r, 20));
}
}
test("a probe that fails after connecting still releases the Redis client", async () => {
const { getCircuitBreakerStore, __resetCircuitBreakerFactory } =
await import("../../../../src/lib/warmupScheduler/circuitBreakerFactory.ts");
const redis = await startProbeRefusingRedis();
try {
__resetCircuitBreakerFactory();
process.env.REDIS_URL = `redis://127.0.0.1:${redis.port}`;
const store = await getCircuitBreakerStore();
assert.ok(
store.constructor.name.includes("Sqlite"),
`a refused probe should fall back, got ${store.constructor.name}`
);
// The client existed by the time the probe threw, so somebody has to close
// it. Left open, its socket keeps the event loop alive.
await waitUntil(() => redis.socketClosed(), 2000);
assert.ok(redis.socketClosed(), "the failed probe leaked its Redis socket");
} finally {
delete process.env.REDIS_URL;
redis.close();
__resetCircuitBreakerFactory();
}
});