Skip to content

Commit cf5dee9

Browse files
authored
test(ollama): prove failed discovery sockets close
1 parent 67c3dcd commit cf5dee9

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

‎extensions/ollama/src/provider-models.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Ollama tests cover provider models plugin behavior.
2+
import { once } from "node:events";
3+
import { createServer } from "node:http";
4+
import type { Socket } from "node:net";
25
import { expectDefined } from "@openclaw/normalization-core";
36
import { jsonResponse, requestBodyText, requestUrl } from "openclaw/plugin-sdk/test-env";
47
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -478,6 +481,120 @@ describe("ollama provider models", () => {
478481
expect(showResponse.wasCanceled()).toBe(true);
479482
});
480483

484+
it("closes real failed discovery sockets while preserving successful discovery", async () => {
485+
const sockets = new Set<Socket>();
486+
const socketClosures = new Map<string, Promise<void>>();
487+
let mode: "failure" | "success" = "failure";
488+
489+
const server = createServer((request, response) => {
490+
const path = request.url;
491+
if (path !== "/api/tags" && path !== "/api/show") {
492+
response.writeHead(404);
493+
response.end();
494+
return;
495+
}
496+
497+
if (mode === "failure") {
498+
socketClosures.set(
499+
path,
500+
new Promise<void>((resolve) => {
501+
request.socket.once("close", () => resolve());
502+
}),
503+
);
504+
response.writeHead(503, { "content-type": "text/plain" });
505+
// Leave the body open so only real client cancellation can close its socket.
506+
response.write("ollama unavailable");
507+
return;
508+
}
509+
510+
response.writeHead(200, { "content-type": "application/json" });
511+
if (path === "/api/tags") {
512+
response.end(JSON.stringify({ models: [{ name: "llama3:8b" }] }));
513+
return;
514+
}
515+
response.end(
516+
JSON.stringify({
517+
model_info: { "llama.context_length": 32768 },
518+
capabilities: ["completion", "tools"],
519+
}),
520+
);
521+
});
522+
523+
server.on("connection", (socket) => {
524+
sockets.add(socket);
525+
socket.once("close", () => sockets.delete(socket));
526+
});
527+
528+
const waitForSocketClose = async (path: string): Promise<void> => {
529+
const closed = socketClosures.get(path);
530+
if (!closed) {
531+
throw new Error(`No failed discovery socket was recorded for ${path}`);
532+
}
533+
534+
let timeout: ReturnType<typeof setTimeout> | undefined;
535+
try {
536+
await Promise.race([
537+
closed,
538+
new Promise<never>((_resolve, reject) => {
539+
timeout = setTimeout(() => {
540+
reject(new Error(`Failed discovery socket was not closed for ${path}`));
541+
}, 2_000);
542+
}),
543+
]);
544+
} finally {
545+
if (timeout !== undefined) {
546+
clearTimeout(timeout);
547+
}
548+
}
549+
};
550+
551+
const listening = once(server, "listening");
552+
try {
553+
server.listen(0, "127.0.0.1");
554+
await listening;
555+
556+
const address = server.address();
557+
if (!address || typeof address === "string") {
558+
throw new Error("Ollama test server did not expose a TCP address");
559+
}
560+
const baseUrl = `http://127.0.0.1:${address.port}`;
561+
562+
await expect(fetchOllamaModels(baseUrl)).resolves.toEqual({
563+
reachable: true,
564+
models: [],
565+
});
566+
await waitForSocketClose("/api/tags");
567+
568+
await expect(queryOllamaModelShowInfo(baseUrl, "llama3:8b")).resolves.toEqual({});
569+
await waitForSocketClose("/api/show");
570+
571+
mode = "success";
572+
await expect(fetchOllamaModels(baseUrl)).resolves.toEqual({
573+
reachable: true,
574+
models: [{ name: "llama3:8b" }],
575+
});
576+
await expect(queryOllamaModelShowInfo(baseUrl, "llama3:8b")).resolves.toEqual({
577+
contextWindow: 32768,
578+
capabilities: ["completion", "tools"],
579+
});
580+
} finally {
581+
for (const socket of sockets) {
582+
socket.destroy();
583+
}
584+
if (server.listening) {
585+
await new Promise<void>((resolve, reject) => {
586+
server.close((error) => {
587+
if (error) {
588+
reject(error);
589+
return;
590+
}
591+
resolve();
592+
});
593+
});
594+
}
595+
}
596+
});
597+
481598
it("fails soft and stops reading when discovery streams exceed the JSON byte cap", async () => {
482599
// Larger than the shared 16 MiB readProviderJsonResponse cap so the bounded reader cancels
483600
// the stream mid-flight; if the cap were removed the reader would buffer the whole payload.

0 commit comments

Comments
 (0)