Skip to content

Commit 0f49046

Browse files
committed
test(daemon): avoid repeated host probes during virtual drain
1 parent 0bc502a commit 0f49046

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/daemon-client/__tests__/daemon-client-ensure-drain.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import assert from 'node:assert/strict';
22
import { afterEach, test, vi } from 'vitest';
33
import { closeLoopbackServer, supportsLoopbackBind } from '@agent-device/test-kit';
44
import { AppError } from '@agent-device/kernel/errors';
5-
import { isProcessAlive } from '@agent-device/host-kit/process';
5+
import {
6+
isProcessAlive,
7+
readHostProcessIdentityObservations,
8+
} from '@agent-device/host-kit/process';
69
import { finishRegisteredDaemonFixtures } from '../../__tests__/test-utils/registered-daemon-fixture.ts';
710
import {
811
draining,
@@ -23,6 +26,7 @@ vi.mock('@agent-device/host-kit/process', async (importOriginal) => {
2326
const actual = await importOriginal<typeof import('@agent-device/host-kit/process')>();
2427
return {
2528
...actual,
29+
readHostProcessIdentityObservations: vi.fn(actual.readHostProcessIdentityObservations),
2630
readProcessCommand: (pid: number) =>
2731
hostedByAnotherProcess ? 'node /opt/app/server.js' : actual.readProcessCommand(pid),
2832
};
@@ -43,6 +47,9 @@ const spawn = vi.mocked(runCmdDetachedMonitored);
4347
const actualRetry = await vi.importActual<typeof import('@agent-device/host-kit/retry')>(
4448
'@agent-device/host-kit/retry',
4549
);
50+
const actualProcess = await vi.importActual<typeof import('@agent-device/host-kit/process')>(
51+
'@agent-device/host-kit/process',
52+
);
4653
const realNow = Date.now.bind(Date);
4754
const actualKill = process.kill.bind(process);
4855

@@ -56,6 +63,9 @@ afterEach(async () => {
5663
vi.restoreAllMocks();
5764
spawn.mockReset();
5865
vi.mocked(sleep).mockReset();
66+
vi.mocked(readHostProcessIdentityObservations).mockImplementation(
67+
actualProcess.readHostProcessIdentityObservations,
68+
);
5969
await finishRegisteredDaemonFixtures();
6070
});
6171

@@ -75,6 +85,14 @@ type VirtualDrain = {
7585
*/
7686
function installVirtualDrain(pid: () => number): VirtualDrain {
7787
let virtual = false;
88+
// The fixture holds this child lifetime until it exits or is signaled; later probes use the host.
89+
let drainIdentity: ReturnType<typeof readHostProcessIdentityObservations> | undefined;
90+
vi.mocked(readHostProcessIdentityObservations).mockImplementation((pids) => {
91+
const selected = [...pids];
92+
if (virtual && drainIdentity && selected.length === 1 && selected[0] === pid())
93+
return drainIdentity;
94+
return actualProcess.readHostProcessIdentityObservations(selected);
95+
});
7896
let now = 0;
7997
let startedAt = 0;
8098
let leavesAt: number | undefined;
@@ -103,6 +121,7 @@ function installVirtualDrain(pid: () => number): VirtualDrain {
103121
return {
104122
elapsedMs: () => now - startedAt,
105123
begin: (leavesAfterMs) => {
124+
drainIdentity = actualProcess.readHostProcessIdentityObservations([pid()]);
106125
now = realNow();
107126
startedAt = now;
108127
leavesAt = leavesAfterMs === undefined ? undefined : now + leavesAfterMs;

0 commit comments

Comments
 (0)