refactor(compaction-tests): share aggregate timeout params

This commit is contained in:
Peter Steinberger
2026-03-17 07:26:52 +00:00
parent 1eb810a5e3
commit 168fa9d433

View File

@@ -1,6 +1,8 @@
import { describe, expect, it, vi } from "vitest";
import { waitForCompactionRetryWithAggregateTimeout } from "./compaction-retry-aggregate-timeout.js";
type AggregateTimeoutParams = Parameters<typeof waitForCompactionRetryWithAggregateTimeout>[0];
async function withFakeTimers(run: () => Promise<void>) {
vi.useFakeTimers();
try {
@@ -20,30 +22,38 @@ function expectClearedTimeoutState(onTimeout: ReturnType<typeof vi.fn>, timedOut
expect(vi.getTimerCount()).toBe(0);
}
function buildAggregateTimeoutParams(
overrides: Partial<AggregateTimeoutParams> &
Pick<AggregateTimeoutParams, "waitForCompactionRetry">,
): AggregateTimeoutParams & { onTimeout: ReturnType<typeof vi.fn> } {
const onTimeout = overrides.onTimeout ?? vi.fn();
return {
waitForCompactionRetry: overrides.waitForCompactionRetry,
abortable: overrides.abortable ?? (async (promise) => await promise),
aggregateTimeoutMs: overrides.aggregateTimeoutMs ?? 60_000,
isCompactionStillInFlight: overrides.isCompactionStillInFlight,
onTimeout,
};
}
describe("waitForCompactionRetryWithAggregateTimeout", () => {
it("times out and fires callback when compaction retry never resolves", async () => {
await withFakeTimers(async () => {
const onTimeout = vi.fn();
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
const params = buildAggregateTimeoutParams({ waitForCompactionRetry });
const resultPromise = waitForCompactionRetryWithAggregateTimeout({
waitForCompactionRetry,
abortable: async (promise) => await promise,
aggregateTimeoutMs: 60_000,
onTimeout,
});
const resultPromise = waitForCompactionRetryWithAggregateTimeout(params);
await vi.advanceTimersByTimeAsync(60_000);
const result = await resultPromise;
expect(result.timedOut).toBe(true);
expectClearedTimeoutState(onTimeout, true);
expectClearedTimeoutState(params.onTimeout, true);
});
});
it("keeps waiting while compaction remains in flight", async () => {
await withFakeTimers(async () => {
const onTimeout = vi.fn();
let compactionInFlight = true;
const waitForCompactionRetry = vi.fn(
async () =>
@@ -54,62 +64,52 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
}, 170_000);
}),
);
const resultPromise = waitForCompactionRetryWithAggregateTimeout({
const params = buildAggregateTimeoutParams({
waitForCompactionRetry,
abortable: async (promise) => await promise,
aggregateTimeoutMs: 60_000,
onTimeout,
isCompactionStillInFlight: () => compactionInFlight,
});
const resultPromise = waitForCompactionRetryWithAggregateTimeout(params);
await vi.advanceTimersByTimeAsync(170_000);
const result = await resultPromise;
expect(result.timedOut).toBe(false);
expectClearedTimeoutState(onTimeout, false);
expectClearedTimeoutState(params.onTimeout, false);
});
});
it("times out after an idle timeout window", async () => {
await withFakeTimers(async () => {
const onTimeout = vi.fn();
let compactionInFlight = true;
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
setTimeout(() => {
compactionInFlight = false;
}, 90_000);
const resultPromise = waitForCompactionRetryWithAggregateTimeout({
const params = buildAggregateTimeoutParams({
waitForCompactionRetry,
abortable: async (promise) => await promise,
aggregateTimeoutMs: 60_000,
onTimeout,
isCompactionStillInFlight: () => compactionInFlight,
});
const resultPromise = waitForCompactionRetryWithAggregateTimeout(params);
await vi.advanceTimersByTimeAsync(120_000);
const result = await resultPromise;
expect(result.timedOut).toBe(true);
expectClearedTimeoutState(onTimeout, true);
expectClearedTimeoutState(params.onTimeout, true);
});
});
it("does not time out when compaction retry resolves", async () => {
await withFakeTimers(async () => {
const onTimeout = vi.fn();
const waitForCompactionRetry = vi.fn(async () => {});
const params = buildAggregateTimeoutParams({ waitForCompactionRetry });
const result = await waitForCompactionRetryWithAggregateTimeout({
waitForCompactionRetry,
abortable: async (promise) => await promise,
aggregateTimeoutMs: 60_000,
onTimeout,
});
const result = await waitForCompactionRetryWithAggregateTimeout(params);
expect(result.timedOut).toBe(false);
expectClearedTimeoutState(onTimeout, false);
expectClearedTimeoutState(params.onTimeout, false);
});
});
@@ -117,21 +117,17 @@ describe("waitForCompactionRetryWithAggregateTimeout", () => {
await withFakeTimers(async () => {
const abortError = new Error("aborted");
abortError.name = "AbortError";
const onTimeout = vi.fn();
const waitForCompactionRetry = vi.fn(async () => await new Promise<void>(() => {}));
const params = buildAggregateTimeoutParams({
waitForCompactionRetry,
abortable: async () => {
throw abortError;
},
});
await expect(
waitForCompactionRetryWithAggregateTimeout({
waitForCompactionRetry,
abortable: async () => {
throw abortError;
},
aggregateTimeoutMs: 60_000,
onTimeout,
}),
).rejects.toThrow("aborted");
await expect(waitForCompactionRetryWithAggregateTimeout(params)).rejects.toThrow("aborted");
expectClearedTimeoutState(onTimeout, false);
expectClearedTimeoutState(params.onTimeout, false);
});
});
});