mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:10:43 +00:00
test: stabilize stale-pid ancestor override
(cherry picked from commit 4e25479cb2)
This commit is contained in:
@@ -185,24 +185,19 @@ describe.skipIf(isWindows)("restart-stale-pids", () => {
|
||||
afterEach(() => {
|
||||
__testing.setSleepSyncOverride(null);
|
||||
__testing.setDateNowOverride(null);
|
||||
__testing.setParentPidOverride(null);
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
// Temporarily rewrites `process.ppid` for a block of test code. Used by the
|
||||
// Temporarily overrides the parent PID for a block of test code. Used by the
|
||||
// ancestor-exclusion tests to drive the real `getSelfAndAncestorPidsSync`
|
||||
// walk without installing a runtime-reachable override on the module. Node
|
||||
// always exposes `process.ppid` as an own property so the captured
|
||||
// descriptor is non-null in practice; the `if (orig)` guard is defensive
|
||||
// against a broken environment, not a reachable branch.
|
||||
// walk without depending on runtime-specific `process.ppid` descriptors.
|
||||
function withStubbedPpid<T>(ppid: number, fn: () => T): T {
|
||||
const orig = Object.getOwnPropertyDescriptor(process, "ppid");
|
||||
Object.defineProperty(process, "ppid", { value: ppid, configurable: true });
|
||||
__testing.setParentPidOverride(() => ppid);
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
if (orig) {
|
||||
Object.defineProperty(process, "ppid", orig);
|
||||
}
|
||||
__testing.setParentPidOverride(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ const MAX_ANCESTOR_WALK_DEPTH = 32;
|
||||
const restartLog = createSubsystemLogger("restart");
|
||||
let sleepSyncOverride: ((ms: number) => void) | null = null;
|
||||
let dateNowOverride: (() => number) | null = null;
|
||||
let parentPidOverride: (() => number) | null = null;
|
||||
|
||||
function getTimeMs(): number {
|
||||
return dateNowOverride ? dateNowOverride() : Date.now();
|
||||
@@ -70,6 +71,10 @@ function sleepSync(ms: number): void {
|
||||
}
|
||||
}
|
||||
|
||||
function getParentPid(): number {
|
||||
return parentPidOverride ? parentPidOverride() : process.ppid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a single ancestor PID from `/proc/<pid>/status` on Linux.
|
||||
* Returns null on any failure (non-Linux platform, restricted /proc, race
|
||||
@@ -135,7 +140,7 @@ function readParentPidFromProc(pid: number): number | null {
|
||||
*/
|
||||
function getSelfAndAncestorPidsSync(): Set<number> {
|
||||
const pids = new Set<number>([process.pid]);
|
||||
const immediateParent = process.ppid;
|
||||
const immediateParent = getParentPid();
|
||||
if (!Number.isFinite(immediateParent) || immediateParent <= 0) {
|
||||
return pids;
|
||||
}
|
||||
@@ -553,6 +558,9 @@ export const __testing = {
|
||||
setDateNowOverride(fn: (() => number) | null) {
|
||||
dateNowOverride = fn;
|
||||
},
|
||||
setParentPidOverride(fn: (() => number) | null) {
|
||||
parentPidOverride = fn;
|
||||
},
|
||||
/** Invoke sleepSync directly (bypasses the override) for unit-testing the real Atomics path. */
|
||||
callSleepSyncRaw: sleepSync,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user