mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-20 06:02:18 +02:00
Mock timers in native-workspace-lock-keepalive teststest(runtime): exercise disabled keepalive over elapsed time
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
856c0d660c
commit
7fb0ad45bf
1 file changed
+29
-2
@@ -1,13 +1,40 @@
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
import * as timers from "node:timers/promises";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { withWorkspaceLockKeepalive } from "./native-workspace-lock-keepalive.js";
|
||||
|
||||
vi.mock("node:timers/promises", async importOriginal => {
|
||||
const actual = await importOriginal<typeof import("node:timers/promises")>();
|
||||
return { ...actual, setTimeout: vi.fn(actual.setTimeout) };
|
||||
});
|
||||
|
||||
it.each([0, -1, NaN, Infinity])("does not probe when the idle timeout is %s", async timeout => {
|
||||
const execute = vi.fn();
|
||||
const value = {};
|
||||
await expect(withWorkspaceLockKeepalive({ execute } as unknown as Db, timeout, async () => value)).resolves.toBe(value);
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
let finish!: (value: object) => void;
|
||||
const pending = new Promise<object>(resolve => { finish = resolve; });
|
||||
vi.useFakeTimers();
|
||||
const sleep = vi.mocked(timers.setTimeout);
|
||||
const originalSleep = sleep.getMockImplementation()!;
|
||||
sleep.mockClear().mockImplementation((_delay, result, options) => new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => resolve(result), 30_000);
|
||||
options?.signal?.addEventListener("abort", () => {
|
||||
clearTimeout(timer);
|
||||
reject(new Error("aborted"));
|
||||
}, { once: true });
|
||||
}));
|
||||
const result = withWorkspaceLockKeepalive({ execute } as unknown as Db, timeout, () => pending);
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(60_001);
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
expect(sleep).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
finish(value);
|
||||
await expect(result).resolves.toBe(value);
|
||||
sleep.mockImplementation(originalSleep);
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("joins an in-flight probe before releasing the lock scope", async () => {
|
||||
|
||||
Reference in new issue
Block a user