Skip to content

Commit 04d86e0

Browse files
committed
test(telegram): isolate startup probe limiter timing
1 parent 578e73f commit 04d86e0

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

extensions/telegram/src/channel.gateway.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
createStartAccountContext,
77
} from "openclaw/plugin-sdk/channel-test-helpers";
88
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
9-
import { afterEach, describe, expect, it, vi } from "vitest";
9+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1010
import { readCachedTelegramBotInfo, writeCachedTelegramBotInfo } from "./bot-info-cache.js";
1111
import type { TelegramBotInfo } from "./bot-info.js";
1212
import { telegramPlugin } from "./channel.js";
@@ -160,13 +160,23 @@ async function waitForCondition(check: () => boolean, message: string, timeoutMs
160160
throw new Error(message);
161161
}
162162

163+
async function waitForMicrotaskCondition(check: () => boolean, message: string, attempts = 100) {
164+
for (let attempt = 0; attempt < attempts; attempt += 1) {
165+
if (check()) {
166+
return;
167+
}
168+
await Promise.resolve();
169+
}
170+
throw new Error(message);
171+
}
172+
163173
async function releaseStartupProbeControls(releaseProbe: Array<() => void>) {
164174
for (let attempt = 0; attempt < 10; attempt += 1) {
165175
const releases = releaseProbe.splice(0);
166176
for (const release of releases) {
167177
release();
168178
}
169-
await new Promise((resolve) => setTimeout(resolve, 0));
179+
await Promise.resolve();
170180
if (releaseProbe.length === 0) {
171181
return;
172182
}
@@ -176,7 +186,13 @@ async function releaseStartupProbeControls(releaseProbe: Array<() => void>) {
176186
}
177187
}
178188

189+
beforeEach(() => {
190+
vi.useRealTimers();
191+
resetTelegramStartupProbeLimiterForTests();
192+
});
193+
179194
afterEach(async () => {
195+
vi.useRealTimers();
180196
clearTelegramRuntime();
181197
resetTelegramPollingLeasesForTests();
182198
resetTelegramStartupProbeLimiterForTests();
@@ -464,14 +480,14 @@ describe("telegramPlugin gateway startup", () => {
464480
const third = runProbe();
465481
const tasks = [first, second, third];
466482
try {
467-
await waitForCondition(
483+
await waitForMicrotaskCondition(
468484
() => releaseProbe.length === 2,
469485
"expected two startup probes to begin",
470486
);
471487
expect(maxActiveProbes).toBe(2);
472488

473489
releaseProbe.shift()?.();
474-
await waitForCondition(
490+
await waitForMicrotaskCondition(
475491
() => releaseProbe.length === 2,
476492
"expected queued startup probe to begin after a slot opens",
477493
);
@@ -503,7 +519,7 @@ describe("telegramPlugin gateway startup", () => {
503519
(error: unknown) => error,
504520
);
505521
try {
506-
await waitForCondition(
522+
await waitForMicrotaskCondition(
507523
() => releaseProbe.length === 2,
508524
"expected startup probe slots to fill",
509525
);

0 commit comments

Comments
 (0)