Skip to content

Commit 3ca7b03

Browse files
authored
test(azure-speech): make voices timeout proof deterministic (#124694)
1 parent 551c65e commit 3ca7b03

2 files changed

Lines changed: 63 additions & 24 deletions

File tree

‎extensions/azure-speech/voices-timeout.test.ts‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// the real fetch abort path without depending on Azure latency.
44
import { createServer, type Server } from "node:http";
55
import type { AddressInfo } from "node:net";
6+
import { clearTimeout as clearRealTimeout, setTimeout as setRealTimeout } from "node:timers";
67
import { afterEach, describe, expect, it, vi } from "vitest";
78
import { listAzureSpeechVoices } from "./tts.js";
89

@@ -39,8 +40,13 @@ describe("listAzureSpeechVoices timeout", () => {
3940
{ timeout: 2_000 },
4041
async () => {
4142
let requestCount = 0;
42-
const server = createServer((_req, _res) => {
43+
let notifyRequest = () => {};
44+
const requestReceived = new Promise<void>((resolve) => {
45+
notifyRequest = resolve;
46+
});
47+
const server = createServer((_request, _response) => {
4348
requestCount += 1;
49+
notifyRequest();
4450
});
4551

4652
const port = await listenLocal(server);
@@ -56,26 +62,32 @@ describe("listAzureSpeechVoices timeout", () => {
5662
);
5763

5864
const startedAt = Date.now();
59-
let watchdog: ReturnType<typeof setTimeout> | undefined;
65+
let watchdog: ReturnType<typeof setRealTimeout> | undefined;
6066

6167
try {
62-
await expect(
63-
Promise.race([
64-
listAzureSpeechVoices({
65-
apiKey: "not-a-real",
66-
baseUrl: "https://custom.example.com",
67-
timeoutMs: 100,
68-
}),
69-
new Promise<never>((_, reject) => {
70-
watchdog = setTimeout(() => reject(new Error("voices list did not time out")), 1_000);
71-
}),
72-
]),
73-
).rejects.toThrow(/aborted|timeout|timed out/i);
74-
expect(Date.now() - startedAt).toBeLessThan(1_000);
68+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
69+
const watchdogPromise = new Promise<never>((_, reject) => {
70+
watchdog = setRealTimeout(() => reject(new Error("voices list did not time out")), 1_000);
71+
});
72+
const request = Promise.race([
73+
listAzureSpeechVoices({
74+
apiKey: "not-a-real",
75+
baseUrl: "https://custom.example.com",
76+
timeoutMs: 100,
77+
}),
78+
watchdogPromise,
79+
]);
80+
const rejection = expect(request).rejects.toThrow(/aborted|timeout|timed out/i);
81+
82+
await Promise.race([requestReceived, watchdogPromise]);
7583
expect(requestCount).toBe(1);
84+
await vi.advanceTimersByTimeAsync(100);
85+
await rejection;
86+
expect(Date.now() - startedAt).toBeLessThan(1_000);
7687
} finally {
88+
vi.useRealTimers();
7789
if (watchdog) {
78-
clearTimeout(watchdog);
90+
clearRealTimeout(watchdog);
7991
}
8092
await closeServer(server);
8193
}

‎extensions/inworld/voices-timeout.test.ts‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { clearTimeout as clearRealTimeout, setTimeout as setRealTimeout } from "node:timers";
12
import { withServer } from "openclaw/plugin-sdk/test-env";
23
import { afterEach, describe, expect, it, vi } from "vitest";
34
import { listInworldVoices } from "./tts.js";
@@ -14,9 +15,14 @@ describe("listInworldVoices live timeout", () => {
1415
{ timeout: 2_000 },
1516
async () => {
1617
let requestCount = 0;
18+
let notifyRequest = () => {};
19+
const requestReceived = new Promise<void>((resolve) => {
20+
notifyRequest = resolve;
21+
});
1722
await withServer(
1823
(request) => {
1924
requestCount += 1;
25+
notifyRequest();
2026
request.resume();
2127
},
2228
async (baseUrl) => {
@@ -27,14 +33,35 @@ describe("listInworldVoices live timeout", () => {
2733
}) as unknown as typeof globalThis.fetch,
2834
);
2935

30-
await expect(
31-
listInworldVoices({
32-
apiKey: "test-key",
33-
baseUrl: "https://custom.inworld.example.com",
34-
timeoutMs: 250,
35-
}),
36-
).rejects.toThrow(/aborted|timeout|timed out/i);
37-
expect(requestCount).toBe(1);
36+
let watchdog: ReturnType<typeof setRealTimeout> | undefined;
37+
try {
38+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
39+
const watchdogPromise = new Promise<never>((_, reject) => {
40+
watchdog = setRealTimeout(
41+
() => reject(new Error("voices list did not time out")),
42+
1_000,
43+
);
44+
});
45+
const request = Promise.race([
46+
listInworldVoices({
47+
apiKey: "test-key",
48+
baseUrl: "https://custom.inworld.example.com",
49+
timeoutMs: 250,
50+
}),
51+
watchdogPromise,
52+
]);
53+
const rejection = expect(request).rejects.toThrow(/aborted|timeout|timed out/i);
54+
55+
await Promise.race([requestReceived, watchdogPromise]);
56+
expect(requestCount).toBe(1);
57+
await vi.advanceTimersByTimeAsync(250);
58+
await rejection;
59+
} finally {
60+
vi.useRealTimers();
61+
if (watchdog) {
62+
clearRealTimeout(watchdog);
63+
}
64+
}
3865
},
3966
);
4067
},

0 commit comments

Comments
 (0)