fix: harden Pi lifecycle recovery
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { MaintenanceBarrier } from "../src/runtime/maintenance-gate.js";
|
||||
|
||||
test("activation waits for an in-flight admission lease and rejects later admissions", async () => {
|
||||
@@ -17,3 +20,28 @@ test("activation waits for an in-flight admission lease and rejects later admiss
|
||||
gate.deactivate();
|
||||
expect(gate.acquire()).toBeTypeOf("function");
|
||||
});
|
||||
|
||||
test("durable activation survives recreation and deactivation removes the marker first", async () => {
|
||||
const directory = mkdtempSync(join(tmpdir(), "tht-maintenance-gate-"));
|
||||
const marker = join(directory, "maintenance.json");
|
||||
try {
|
||||
const first = new MaintenanceBarrier(marker);
|
||||
const release = first.acquire();
|
||||
expect(release).toBeTypeOf("function");
|
||||
const activation = first.activate();
|
||||
expect(existsSync(marker)).toBe(true);
|
||||
expect(first.acquire()).toBeUndefined();
|
||||
expect(first.status()).toEqual({ active: true, admissions: 1 });
|
||||
release?.();
|
||||
await activation;
|
||||
expect(first.status()).toEqual({ active: true, admissions: 0 });
|
||||
|
||||
const recreated = new MaintenanceBarrier(marker);
|
||||
expect(recreated.status()).toEqual({ active: true, admissions: 0 });
|
||||
recreated.deactivate();
|
||||
expect(existsSync(marker)).toBe(false);
|
||||
expect(recreated.status()).toEqual({ active: false, admissions: 0 });
|
||||
} finally {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user