Skip to content

Commit 3082fc0

Browse files
committed
test: extract shared pin-own-start-time host-process mock
The cache-own-pid readProcessStartTime vi.mock factory was duplicated verbatim in three files (cli-device-status from #1556 plus the two device-claim files), each carrying its own paragraph justifying it. One helper now owns the factory and the why; call sites are one line.
1 parent 66fe1c0 commit 3082fc0

4 files changed

Lines changed: 48 additions & 79 deletions

File tree

‎src/__tests__/cli-device-status.test.ts‎

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,9 @@ import { readCurrentOwnerIdentity } from '../utils/owner-identity.ts';
66
import { runCliCapture } from './cli-capture.ts';
77
import { mkdtempForTestSync } from './test-utils/tmp-dir.ts';
88

9-
// readProcessStartTime shells out to `ps` with a 1s timeout (see
10-
// utils/host-process.ts). This file's fixtures read our own start time once
11-
// to build a "live" claim, then production re-reads it during classification
12-
// to confirm identity; under full-suite CPU contention that second `ps` call
13-
// can miss its deadline and return null, mismatching the cached value and
14-
// flipping a genuinely-live claim to 'owner-process-dead'. Cache our own
15-
// pid's start time after the first (real) read so every later read is
16-
// self-consistent with the fixtures instead of racing a fresh `ps` call.
17-
// isProcessAlive is left real (a plain `kill(pid, 0)` syscall, not a
18-
// subprocess), so the fabricated-dead-pid fixtures below still classify as
19-
// stale through that real, non-flaky check.
20-
vi.mock('../utils/host-process.ts', async () => {
21-
const actual = await vi.importActual<typeof import('../utils/host-process.ts')>(
22-
'../utils/host-process.ts',
23-
);
24-
let cachedOwnStartTime: string | null | undefined;
25-
return {
26-
...actual,
27-
readProcessStartTime: (pid: number) => {
28-
if (pid !== process.pid) return null;
29-
if (cachedOwnStartTime === undefined) {
30-
cachedOwnStartTime = actual.readProcessStartTime(process.pid);
31-
}
32-
return cachedOwnStartTime;
33-
},
34-
};
35-
});
9+
vi.mock('../utils/host-process.ts', async (importOriginal) =>
10+
(await import('./test-utils/host-process-mock.ts')).pinOwnProcessStartTime(importOriginal),
11+
);
3612

3713
test('device status is daemonless and does not send a daemon request', async () => {
3814
const result = await runCliCapture(['device', 'status', '--json']);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type HostProcessModule = typeof import('../../utils/host-process.ts');
2+
3+
/**
4+
* `vi.mock` factory for `utils/host-process.ts` in tests that seed a "live"
5+
* owner from the test process's own identity (readCurrentOwnerIdentity and
6+
* friends) and later have production code re-read it during classification.
7+
*
8+
* readProcessStartTime shells out to `ps` with a 1s timeout; under full-suite
9+
* CPU contention a re-read can miss that deadline and return null, mismatching
10+
* the seeded value and flipping a genuinely-live owner to 'owner-process-dead'.
11+
* This factory reads our own pid's start time once for real, then serves the
12+
* cached value so every read is self-consistent. Other pids read as null, same
13+
* as a real `ps` miss. isProcessAlive stays real (a plain `kill(pid, 0)`
14+
* syscall, not a subprocess), so fabricated dead-pid fixtures still classify
15+
* as dead through that non-flaky check.
16+
*
17+
* Usage: `vi.mock('<path>/utils/host-process.ts', async (importOriginal) =>
18+
* (await import('<path>/test-utils/host-process-mock.ts')).pinOwnProcessStartTime(importOriginal))`
19+
*/
20+
export async function pinOwnProcessStartTime(
21+
importOriginal: () => Promise<HostProcessModule>,
22+
): Promise<HostProcessModule> {
23+
const actual = await importOriginal();
24+
let cachedOwnStartTime: string | null | undefined;
25+
return {
26+
...actual,
27+
readProcessStartTime: (pid: number) => {
28+
if (pid !== process.pid) return null;
29+
if (cachedOwnStartTime === undefined) {
30+
cachedOwnStartTime = actual.readProcessStartTime(process.pid);
31+
}
32+
return cachedOwnStartTime;
33+
},
34+
};
35+
}

‎src/daemon/__tests__/device-claim-prune.test.ts‎

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,11 @@ import { acquireProcessLock } from '../../utils/process-lock.ts';
99
import { readCurrentOwnerIdentity } from '../../utils/owner-identity.ts';
1010
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
1111

12-
// readProcessStartTime shells out to `ps` with a 1s timeout (see
13-
// utils/host-process.ts). This file seeds "live" claims from our own identity
14-
// via readCurrentOwnerIdentity(), then the prune's classification re-reads our
15-
// start time; under full-suite CPU contention that second `ps` call can miss
16-
// its deadline and return null, mismatching the seeded value and flipping a
17-
// genuinely-live claim to 'owner-process-dead'. Cache our own pid's start time
18-
// after the first (real) read so every later read is self-consistent with the
19-
// fixtures instead of racing a fresh `ps` call. isProcessAlive is left real (a
20-
// plain `kill(pid, 0)` syscall, not a subprocess), so the fabricated dead-pid
21-
// fixtures still classify as dead through that real, non-flaky check.
22-
vi.mock('../../utils/host-process.ts', async () => {
23-
const actual = await vi.importActual<typeof import('../../utils/host-process.ts')>(
24-
'../../utils/host-process.ts',
25-
);
26-
let cachedOwnStartTime: string | null | undefined;
27-
return {
28-
...actual,
29-
readProcessStartTime: (pid: number) => {
30-
if (pid !== process.pid) return null;
31-
if (cachedOwnStartTime === undefined) {
32-
cachedOwnStartTime = actual.readProcessStartTime(process.pid);
33-
}
34-
return cachedOwnStartTime;
35-
},
36-
};
37-
});
12+
vi.mock('../../utils/host-process.ts', async (importOriginal) =>
13+
(await import('../../__tests__/test-utils/host-process-mock.ts')).pinOwnProcessStartTime(
14+
importOriginal,
15+
),
16+
);
3817

3918
const previousClaimsDir = process.env.AGENT_DEVICE_CLAIMS_DIR;
4019

‎src/daemon/__tests__/device-claims.test.ts‎

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,11 @@ import { inspectDeviceClaims } from '../device-claim-inspection.ts';
1212
import type { DeviceInfo } from '@agent-device/kernel/device';
1313
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
1414

15-
// readProcessStartTime shells out to `ps` with a 1s timeout (see
16-
// utils/host-process.ts). acquireAdvisoryDeviceClaim records our own identity
17-
// into each claim, then later acquire/inspect calls classify that claim by
18-
// re-reading our start time; under full-suite CPU contention that second `ps`
19-
// call can miss its deadline and return null, mismatching the recorded value
20-
// and flipping a genuinely-live claim to 'owner-process-dead'. Cache our own
21-
// pid's start time after the first (real) read so every later read is
22-
// self-consistent instead of racing a fresh `ps` call. isProcessAlive is left
23-
// real (a plain `kill(pid, 0)` syscall, not a subprocess), so the fabricated
24-
// dead-pid fixture still classifies as dead through that real, non-flaky check.
25-
vi.mock('../../utils/host-process.ts', async () => {
26-
const actual = await vi.importActual<typeof import('../../utils/host-process.ts')>(
27-
'../../utils/host-process.ts',
28-
);
29-
let cachedOwnStartTime: string | null | undefined;
30-
return {
31-
...actual,
32-
readProcessStartTime: (pid: number) => {
33-
if (pid !== process.pid) return null;
34-
if (cachedOwnStartTime === undefined) {
35-
cachedOwnStartTime = actual.readProcessStartTime(process.pid);
36-
}
37-
return cachedOwnStartTime;
38-
},
39-
};
40-
});
15+
vi.mock('../../utils/host-process.ts', async (importOriginal) =>
16+
(await import('../../__tests__/test-utils/host-process-mock.ts')).pinOwnProcessStartTime(
17+
importOriginal,
18+
),
19+
);
4120

4221
const device: DeviceInfo = {
4322
platform: 'android',

0 commit comments

Comments
 (0)