mirror of https://github.com/openclaw/openclaw.git
test: share compaction retry timer helpers
This commit is contained in:
parent
88de4769de
commit
fb93acb046
|
|
@ -1,10 +1,28 @@
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { waitForCompactionRetryWithAggregateTimeout } from "./compaction-retry-aggregate-timeout.js";
|
import { waitForCompactionRetryWithAggregateTimeout } from "./compaction-retry-aggregate-timeout.js";
|
||||||
|
|
||||||
|
async function withFakeTimers(run: () => Promise<void>) {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
try {
|
||||||
|
await run();
|
||||||
|
} finally {
|
||||||
|
await vi.runOnlyPendingTimersAsync();
|
||||||
|
vi.useRealTimers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectClearedTimeoutState(onTimeout: ReturnType<typeof vi.fn>, timedOut: boolean) {
|
||||||
|
if (timedOut) {
|
||||||
|
expect(onTimeout).toHaveBeenCalledTimes(1);
|
||||||
|
} else {
|
||||||
|
expect(onTimeout).not.toHaveBeenCalled();
|
||||||
|
}
|
||||||
|
expect(vi.getTimerCount()).toBe(0);
|
||||||
|
}
|
||||||
|
|
||||||
describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
it("times out and fires callback when compaction retry never resolves", async () => {
|
it("times out and fires callback when compaction retry never resolves", async () => {
|
||||||
vi.useFakeTimers();
|
await withFakeTimers(async () => {
|
||||||
try {
|
|
||||||
const onTimeout = vi.fn();
|
const onTimeout = vi.fn();
|
||||||
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
|
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
|
||||||
|
|
||||||
|
|
@ -19,17 +37,12 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
const result = await resultPromise;
|
const result = await resultPromise;
|
||||||
|
|
||||||
expect(result.timedOut).toBe(true);
|
expect(result.timedOut).toBe(true);
|
||||||
expect(onTimeout).toHaveBeenCalledTimes(1);
|
expectClearedTimeoutState(onTimeout, true);
|
||||||
expect(vi.getTimerCount()).toBe(0);
|
});
|
||||||
} finally {
|
|
||||||
await vi.runOnlyPendingTimersAsync();
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps waiting while compaction remains in flight", async () => {
|
it("keeps waiting while compaction remains in flight", async () => {
|
||||||
vi.useFakeTimers();
|
await withFakeTimers(async () => {
|
||||||
try {
|
|
||||||
const onTimeout = vi.fn();
|
const onTimeout = vi.fn();
|
||||||
let compactionInFlight = true;
|
let compactionInFlight = true;
|
||||||
const waitForCompactionRetry = vi.fn(
|
const waitForCompactionRetry = vi.fn(
|
||||||
|
|
@ -54,17 +67,12 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
const result = await resultPromise;
|
const result = await resultPromise;
|
||||||
|
|
||||||
expect(result.timedOut).toBe(false);
|
expect(result.timedOut).toBe(false);
|
||||||
expect(onTimeout).not.toHaveBeenCalled();
|
expectClearedTimeoutState(onTimeout, false);
|
||||||
expect(vi.getTimerCount()).toBe(0);
|
});
|
||||||
} finally {
|
|
||||||
await vi.runOnlyPendingTimersAsync();
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("times out after an idle timeout window", async () => {
|
it("times out after an idle timeout window", async () => {
|
||||||
vi.useFakeTimers();
|
await withFakeTimers(async () => {
|
||||||
try {
|
|
||||||
const onTimeout = vi.fn();
|
const onTimeout = vi.fn();
|
||||||
let compactionInFlight = true;
|
let compactionInFlight = true;
|
||||||
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
|
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
|
||||||
|
|
@ -84,17 +92,12 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
const result = await resultPromise;
|
const result = await resultPromise;
|
||||||
|
|
||||||
expect(result.timedOut).toBe(true);
|
expect(result.timedOut).toBe(true);
|
||||||
expect(onTimeout).toHaveBeenCalledTimes(1);
|
expectClearedTimeoutState(onTimeout, true);
|
||||||
expect(vi.getTimerCount()).toBe(0);
|
});
|
||||||
} finally {
|
|
||||||
await vi.runOnlyPendingTimersAsync();
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not time out when compaction retry resolves", async () => {
|
it("does not time out when compaction retry resolves", async () => {
|
||||||
vi.useFakeTimers();
|
await withFakeTimers(async () => {
|
||||||
try {
|
|
||||||
const onTimeout = vi.fn();
|
const onTimeout = vi.fn();
|
||||||
const waitForCompactionRetry = vi.fn(async () => {});
|
const waitForCompactionRetry = vi.fn(async () => {});
|
||||||
|
|
||||||
|
|
@ -106,17 +109,12 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result.timedOut).toBe(false);
|
expect(result.timedOut).toBe(false);
|
||||||
expect(onTimeout).not.toHaveBeenCalled();
|
expectClearedTimeoutState(onTimeout, false);
|
||||||
expect(vi.getTimerCount()).toBe(0);
|
});
|
||||||
} finally {
|
|
||||||
await vi.runOnlyPendingTimersAsync();
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("propagates abort errors from abortable and clears timer", async () => {
|
it("propagates abort errors from abortable and clears timer", async () => {
|
||||||
vi.useFakeTimers();
|
await withFakeTimers(async () => {
|
||||||
try {
|
|
||||||
const abortError = new Error("aborted");
|
const abortError = new Error("aborted");
|
||||||
abortError.name = "AbortError";
|
abortError.name = "AbortError";
|
||||||
const onTimeout = vi.fn();
|
const onTimeout = vi.fn();
|
||||||
|
|
@ -133,11 +131,7 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
|
||||||
}),
|
}),
|
||||||
).rejects.toThrow("aborted");
|
).rejects.toThrow("aborted");
|
||||||
|
|
||||||
expect(onTimeout).not.toHaveBeenCalled();
|
expectClearedTimeoutState(onTimeout, false);
|
||||||
expect(vi.getTimerCount()).toBe(0);
|
});
|
||||||
} finally {
|
|
||||||
await vi.runOnlyPendingTimersAsync();
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue