Handle websocket send failures in browser adapter
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
133b0abd56
commit
85ecef6fd6
2 files changed
+145
-1
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type Listener = (event: unknown) => void;
|
||||
|
||||
class MockWebSocket {
|
||||
static OPEN = 1;
|
||||
static instances: MockWebSocket[] = [];
|
||||
static failSend = false;
|
||||
|
||||
readyState = 0;
|
||||
binaryType = "blob";
|
||||
sentMessages: string[] = [];
|
||||
private listeners: Record<string, Listener[]> = {};
|
||||
|
||||
constructor(readonly url: string) {
|
||||
MockWebSocket.instances.push(this);
|
||||
queueMicrotask(() => {
|
||||
this.readyState = MockWebSocket.OPEN;
|
||||
this.emit("open", {});
|
||||
});
|
||||
}
|
||||
|
||||
addEventListener(type: string, listener: Listener) {
|
||||
const next = this.listeners[type] ?? [];
|
||||
next.push(listener);
|
||||
this.listeners[type] = next;
|
||||
}
|
||||
|
||||
send(data: string) {
|
||||
if (MockWebSocket.failSend) {
|
||||
throw new Error("mock send failure");
|
||||
}
|
||||
|
||||
this.sentMessages.push(String(data));
|
||||
}
|
||||
|
||||
close() {
|
||||
this.readyState = 3;
|
||||
this.emit("close", { code: 1000 });
|
||||
}
|
||||
|
||||
emitMessage(data: unknown) {
|
||||
this.emit("message", { data });
|
||||
}
|
||||
|
||||
private emit(type: string, event: unknown) {
|
||||
const listeners = this.listeners[type] ?? [];
|
||||
for (const listener of listeners) {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setWindowSearch(search: string) {
|
||||
vi.stubGlobal("window", {
|
||||
location: {
|
||||
search,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function waitForCondition(check: () => boolean, timeoutMs = 1_000) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const startedAt = Date.now();
|
||||
const timer = setInterval(() => {
|
||||
if (check()) {
|
||||
clearInterval(timer);
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Date.now() - startedAt >= timeoutMs) {
|
||||
clearInterval(timer);
|
||||
reject(new Error("Timed out waiting for test condition."));
|
||||
}
|
||||
}, 10);
|
||||
});
|
||||
}
|
||||
|
||||
describe("wsNativeApi", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
MockWebSocket.instances = [];
|
||||
MockWebSocket.failSend = false;
|
||||
vi.stubGlobal("WebSocket", MockWebSocket as unknown as typeof WebSocket);
|
||||
});
|
||||
|
||||
it("connects using ws query parameter and resolves responses", async () => {
|
||||
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4400%3Ftoken%3Dabc");
|
||||
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = getOrCreateWsNativeApi();
|
||||
|
||||
const request = api.todos.list();
|
||||
|
||||
const socket = MockWebSocket.instances[0];
|
||||
await waitForCondition(() => (socket?.sentMessages.length ?? 0) > 0);
|
||||
expect(socket?.url).toBe("ws://127.0.0.1:4400?token=abc");
|
||||
const requestEnvelope = JSON.parse(socket?.sentMessages[0] ?? "{}") as {
|
||||
type: string;
|
||||
id: string;
|
||||
method: string;
|
||||
};
|
||||
expect(requestEnvelope.type).toBe("request");
|
||||
expect(requestEnvelope.method).toBe("todos.list");
|
||||
|
||||
socket?.emitMessage(
|
||||
JSON.stringify({
|
||||
type: "response",
|
||||
id: requestEnvelope.id,
|
||||
ok: true,
|
||||
result: [],
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(request).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects immediately when websocket send throws", async () => {
|
||||
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4401");
|
||||
MockWebSocket.failSend = true;
|
||||
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = getOrCreateWsNativeApi();
|
||||
|
||||
await expect(api.todos.list()).rejects.toThrow(
|
||||
"Failed to send runtime request 'todos.list': mock send failure",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -92,7 +92,23 @@ class WsNativeApiClient {
|
||||
this.pending.set(id, { resolve, reject, timeout });
|
||||
});
|
||||
|
||||
socket.send(JSON.stringify(requestMessage));
|
||||
try {
|
||||
socket.send(JSON.stringify(requestMessage));
|
||||
} catch (error) {
|
||||
const pending = this.pending.get(id);
|
||||
if (pending) {
|
||||
clearTimeout(pending.timeout);
|
||||
this.pending.delete(id);
|
||||
pending.reject(
|
||||
new Error(
|
||||
`Failed to send runtime request '${method}': ${
|
||||
error instanceof Error ? error.message : "unknown websocket failure"
|
||||
}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return requestPromise;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user