mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 17:04:58 +00:00
test(infra): dedupe gateway-lock setup and cover guard paths
This commit is contained in:
@@ -5,7 +5,7 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
|
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
|
||||||
import { acquireGatewayLock, GatewayLockError } from "./gateway-lock.js";
|
import { acquireGatewayLock, GatewayLockError, type GatewayLockOptions } from "./gateway-lock.js";
|
||||||
|
|
||||||
let fixtureRoot = "";
|
let fixtureRoot = "";
|
||||||
let fixtureCount = 0;
|
let fixtureCount = 0;
|
||||||
@@ -17,15 +17,25 @@ async function makeEnv() {
|
|||||||
await fs.writeFile(configPath, "{}", "utf8");
|
await fs.writeFile(configPath, "{}", "utf8");
|
||||||
await fs.mkdir(resolveGatewayLockDir(), { recursive: true });
|
await fs.mkdir(resolveGatewayLockDir(), { recursive: true });
|
||||||
return {
|
return {
|
||||||
env: {
|
...process.env,
|
||||||
...process.env,
|
OPENCLAW_STATE_DIR: dir,
|
||||||
OPENCLAW_STATE_DIR: dir,
|
OPENCLAW_CONFIG_PATH: configPath,
|
||||||
OPENCLAW_CONFIG_PATH: configPath,
|
|
||||||
},
|
|
||||||
cleanup: async () => {},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function acquireForTest(
|
||||||
|
env: NodeJS.ProcessEnv,
|
||||||
|
opts: Omit<GatewayLockOptions, "env" | "allowInTests"> = {},
|
||||||
|
) {
|
||||||
|
return await acquireGatewayLock({
|
||||||
|
env,
|
||||||
|
allowInTests: true,
|
||||||
|
timeoutMs: 30,
|
||||||
|
pollIntervalMs: 2,
|
||||||
|
...opts,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function resolveLockPath(env: NodeJS.ProcessEnv) {
|
function resolveLockPath(env: NodeJS.ProcessEnv) {
|
||||||
const stateDir = resolveStateDir(env);
|
const stateDir = resolveStateDir(env);
|
||||||
const configPath = resolveConfigPath(env, stateDir);
|
const configPath = resolveConfigPath(env, stateDir);
|
||||||
@@ -105,38 +115,22 @@ describe("gateway lock", () => {
|
|||||||
// Fake timers can hang on Windows CI when combined with fs open loops.
|
// Fake timers can hang on Windows CI when combined with fs open loops.
|
||||||
// Keep this test on real timers and use small timeouts.
|
// Keep this test on real timers and use small timeouts.
|
||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
const { env, cleanup } = await makeEnv();
|
const env = await makeEnv();
|
||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireForTest(env, { timeoutMs: 50 });
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 50,
|
|
||||||
pollIntervalMs: 2,
|
|
||||||
});
|
|
||||||
expect(lock).not.toBeNull();
|
expect(lock).not.toBeNull();
|
||||||
|
|
||||||
const pending = acquireGatewayLock({
|
const pending = acquireForTest(env, { timeoutMs: 15 });
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 15,
|
|
||||||
pollIntervalMs: 2,
|
|
||||||
});
|
|
||||||
await expect(pending).rejects.toBeInstanceOf(GatewayLockError);
|
await expect(pending).rejects.toBeInstanceOf(GatewayLockError);
|
||||||
|
|
||||||
await lock?.release();
|
await lock?.release();
|
||||||
const lock2 = await acquireGatewayLock({
|
const lock2 = await acquireForTest(env);
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 30,
|
|
||||||
pollIntervalMs: 2,
|
|
||||||
});
|
|
||||||
await lock2?.release();
|
await lock2?.release();
|
||||||
await cleanup();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("treats recycled linux pid as stale when start time mismatches", async () => {
|
it("treats recycled linux pid as stale when start time mismatches", async () => {
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
vi.setSystemTime(new Date("2026-02-06T10:05:00.000Z"));
|
vi.setSystemTime(new Date("2026-02-06T10:05:00.000Z"));
|
||||||
const { env, cleanup } = await makeEnv();
|
const env = await makeEnv();
|
||||||
const { lockPath, configPath } = resolveLockPath(env);
|
const { lockPath, configPath } = resolveLockPath(env);
|
||||||
const payload = createLockPayload({ configPath, startTime: 111 });
|
const payload = createLockPayload({ configPath, startTime: 111 });
|
||||||
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
||||||
@@ -146,9 +140,7 @@ describe("gateway lock", () => {
|
|||||||
onProcRead: () => statValue,
|
onProcRead: () => statValue,
|
||||||
});
|
});
|
||||||
|
|
||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireForTest(env, {
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 80,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 5,
|
pollIntervalMs: 5,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
@@ -157,12 +149,11 @@ describe("gateway lock", () => {
|
|||||||
|
|
||||||
await lock?.release();
|
await lock?.release();
|
||||||
spy.mockRestore();
|
spy.mockRestore();
|
||||||
await cleanup();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps lock on linux when proc access fails unless stale", async () => {
|
it("keeps lock on linux when proc access fails unless stale", async () => {
|
||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
const { env, cleanup } = await makeEnv();
|
const env = await makeEnv();
|
||||||
const { lockPath, configPath } = resolveLockPath(env);
|
const { lockPath, configPath } = resolveLockPath(env);
|
||||||
const payload = createLockPayload({ configPath, startTime: 111 });
|
const payload = createLockPayload({ configPath, startTime: 111 });
|
||||||
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
||||||
@@ -173,11 +164,8 @@ describe("gateway lock", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const pending = acquireGatewayLock({
|
const pending = acquireForTest(env, {
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 15,
|
timeoutMs: 15,
|
||||||
pollIntervalMs: 2,
|
|
||||||
staleMs: 10_000,
|
staleMs: 10_000,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
});
|
});
|
||||||
@@ -198,11 +186,7 @@ describe("gateway lock", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireForTest(env, {
|
||||||
env,
|
|
||||||
allowInTests: true,
|
|
||||||
timeoutMs: 30,
|
|
||||||
pollIntervalMs: 2,
|
|
||||||
staleMs: 1,
|
staleMs: 1,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
});
|
});
|
||||||
@@ -210,6 +194,33 @@ describe("gateway lock", () => {
|
|||||||
|
|
||||||
await lock?.release();
|
await lock?.release();
|
||||||
staleSpy.mockRestore();
|
staleSpy.mockRestore();
|
||||||
await cleanup();
|
});
|
||||||
|
|
||||||
|
it("returns null when multi-gateway override is enabled", async () => {
|
||||||
|
const env = await makeEnv();
|
||||||
|
const lock = await acquireGatewayLock({
|
||||||
|
env: { ...env, OPENCLAW_ALLOW_MULTI_GATEWAY: "1", VITEST: "" },
|
||||||
|
});
|
||||||
|
expect(lock).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null in test env unless allowInTests is set", async () => {
|
||||||
|
const env = await makeEnv();
|
||||||
|
const lock = await acquireGatewayLock({
|
||||||
|
env: { ...env, VITEST: "1" },
|
||||||
|
});
|
||||||
|
expect(lock).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("wraps unexpected fs errors as GatewayLockError", async () => {
|
||||||
|
const env = await makeEnv();
|
||||||
|
const openSpy = vi.spyOn(fs, "open").mockRejectedValueOnce(
|
||||||
|
Object.assign(new Error("denied"), {
|
||||||
|
code: "EACCES",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await expect(acquireForTest(env)).rejects.toBeInstanceOf(GatewayLockError);
|
||||||
|
openSpy.mockRestore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user