TypeScript
test(telemetry): keep fixture PIDs from colliding with the test worker (#21446)
Victor Vogelda46db6ea300
diff --git a/apps/server/src/diagnostics/ProcessDiagnostics.test.ts b/apps/server/src/diagnostics/ProcessDiagnostics.test.tsindex b480b021375..5c981033a99 100644--- a/apps/server/src/diagnostics/ProcessDiagnostics.test.ts+++ b/apps/server/src/diagnostics/ProcessDiagnostics.test.ts@@ -15,6 +15,11 @@ import * as ResourceAttribution from "../resourceTelemetry/ResourceAttribution.timport * as ResourceTelemetry from "../resourceTelemetry/ResourceTelemetry.ts";import * as ProcessDiagnostics from "./ProcessDiagnostics.ts";+// ProcessDiagnostics treats process.pid as the server, so the fixture PID is an+// offset from it. A fixed PID collides whenever the test worker is assigned it.+const SERVER_PID = process.pid;+const CHILD_PID = SERVER_PID + 1;+function makeNativeSnapshot(processes: ResourceMonitorSnapshotEvent["processes"],): ResourceMonitorSnapshotEvent {@@ -71,7 +76,7 @@ describe("ProcessDiagnostics", () => {const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(Effect.flatMap((processDiagnostics) =>processDiagnostics.signal({- pid: 4_242,+ pid: CHILD_PID,startTimeMs: 2_000,signal: "SIGINT",}),@@ -80,10 +85,12 @@ describe("ProcessDiagnostics", () => {);expect(result).toEqual({- pid: 4242,+ pid: CHILD_PID,signal: "SIGINT",signaled: false,- message: Option.some("Process 4242 no longer matches the selected process identity."),+ message: Option.some(+ `Process ${CHILD_PID} no longer matches the selected process identity.`,+ ),});}),);@@ -92,8 +99,8 @@ describe("ProcessDiagnostics", () => {Effect.gen(function* () {const snapshot = makeNativeSnapshot([{- pid: 4_242,- ppid: process.pid,+ pid: CHILD_PID,+ ppid: SERVER_PID,startTimeMs: 2_000,runTimeMs: 4_000,name: "agent",@@ -134,7 +141,7 @@ describe("ProcessDiagnostics", () => {const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(Effect.flatMap((processDiagnostics) =>processDiagnostics.signal({- pid: 4_242,+ pid: CHILD_PID,startTimeMs: 2_000,signal: "SIGINT",}),@@ -143,11 +150,11 @@ describe("ProcessDiagnostics", () => {);expect(result).toEqual({- pid: 4_242,+ pid: CHILD_PID,signal: "SIGINT",signaled: false,message: Option.some(- "Could not refresh process 4242; refusing to signal a stale identity.",+ `Could not refresh process ${CHILD_PID}; refusing to signal a stale identity.`,),});}),@@ -160,7 +167,7 @@ describe("ProcessDiagnostics", () => {);const snapshot = makeNativeSnapshot([{- pid: 4_242,+ pid: CHILD_PID,ppid: 1,startTimeMs: 2_000,runTimeMs: 4_000,@@ -182,7 +189,7 @@ describe("ProcessDiagnostics", () => {type: "desktopTelemetry",sequence: 1,sampledAtUnixMs,- electronPid: 4_242,+ electronPid: CHILD_PID,power: {source: "electron-main",idle: "false",@@ -198,7 +205,7 @@ describe("ProcessDiagnostics", () => {speedLimitPercent: Option.none(),electronProcesses: [{- pid: 4_242,+ pid: CHILD_PID,creationTimeMs: 2_000,type: "Browser",name: "electron",@@ -214,7 +221,7 @@ describe("ProcessDiagnostics", () => {const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(Effect.flatMap((processDiagnostics) =>processDiagnostics.signal({- pid: 4_242,+ pid: CHILD_PID,startTimeMs: 2_000,signal: "SIGKILL",}),@@ -223,10 +230,10 @@ describe("ProcessDiagnostics", () => {);expect(result).toEqual({- pid: 4_242,+ pid: CHILD_PID,signal: "SIGKILL",signaled: false,- message: Option.some("Process 4242 is not a signalable T3 backend descendant."),+ message: Option.some(`Process ${CHILD_PID} is not a signalable T3 backend descendant.`),});const diagnostics = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(diff --git a/apps/server/src/resourceTelemetry/ResourceTelemetry.test.ts b/apps/server/src/resourceTelemetry/ResourceTelemetry.test.tsindex 51ed62d0898..c12e2e44ed7 100644--- a/apps/server/src/resourceTelemetry/ResourceTelemetry.test.ts+++ b/apps/server/src/resourceTelemetry/ResourceTelemetry.test.ts@@ -19,6 +19,14 @@ import * as NativeTelemetryClient from "./NativeTelemetryClient.ts";import * as ResourceAttribution from "./ResourceAttribution.ts";import * as ResourceTelemetry from "./ResourceTelemetry.ts";+// ResourceTelemetry classifies process.pid as the server, so fixture PIDs are+// offsets from it. Fixed PIDs collide whenever the test worker is assigned one.+const SERVER_PID = process.pid;+const CODEX_PID = SERVER_PID + 1;+const SHORT_LIVED_CHILD_PID = SERVER_PID + 2;+const ELECTRON_PID = SERVER_PID + 3;+const MONITOR_PID = SERVER_PID + 4;+function processSample(input: Partial<ResourceMonitorProcessSample> &Pick<ResourceMonitorProcessSample, "pid" | "ppid" | "startTimeMs">,@@ -48,14 +56,14 @@ function nativeSnapshot(input: {}): ResourceMonitorSnapshotEvent {const processes = [processSample({- pid: process.pid,+ pid: SERVER_PID,ppid: 1,startTimeMs: 100,cpuTimeMs: input.sequence * 10,}),processSample({- pid: 4_242,- ppid: process.pid,+ pid: CODEX_PID,+ ppid: SERVER_PID,startTimeMs: 200,name: "codex",command: "codex app-server",@@ -63,7 +71,7 @@ function nativeSnapshot(input: {ioWriteBytes: input.childWriteBytes,}),processSample({- pid: 5_000,+ pid: ELECTRON_PID,ppid: 1,startTimeMs: 300,name: "electron",@@ -71,8 +79,8 @@ function nativeSnapshot(input: {cpuTimeMs: input.sequence * 20,}),processSample({- pid: 9_000,- ppid: process.pid,+ pid: MONITOR_PID,+ ppid: SERVER_PID,startTimeMs: 400,name: "t3-resource-monitor",command: "t3-resource-monitor",@@ -109,7 +117,7 @@ function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshottype: "desktopTelemetry",sequence: 1,sampledAtUnixMs,- electronPid: 5_000,+ electronPid: ELECTRON_PID,power: {source: "electron-main",idle: "false",@@ -125,7 +133,7 @@ function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshotspeedLimitPercent: Option.some(90),electronProcesses: [{- pid: 5_000,+ pid: ELECTRON_PID,creationTimeMs: 300,type: "Browser",name: "electron",@@ -152,17 +160,17 @@ describe("ResourceTelemetry", () => {childWriteBytes: index * 100,});const processes = sample.processes- .filter((entry) => entry.pid === process.pid || entry.pid === 4_242)+ .filter((entry) => entry.pid === SERVER_PID || entry.pid === CODEX_PID).map((entry) =>- entry.pid === 4_242+ entry.pid === CODEX_PID? { ...entry, startTimeMs: index }: { ...entry, ioWriteBytes: index * 100 },);if (index === 0 || index === 2) {processes.push(processSample({- pid: 4_243,- ppid: process.pid,+ pid: SHORT_LIVED_CHILD_PID,+ ppid: SERVER_PID,startTimeMs: 500,cpuTimeMs: index === 0 ? 100 : 350,ioWriteBytes: index === 0 ? 1_000 : 5_000,@@ -190,18 +198,20 @@ describe("ResourceTelemetry", () => {expect(history.topProcesses).toHaveLength(sampleCount + 2);expect(- history.topProcesses.find((entry) => entry.identity.pid === process.pid),+ history.topProcesses.find((entry) => entry.identity.pid === SERVER_PID),).toMatchObject({sampleCount,cpuTimeMs: (sampleCount - 1) * 10,ioWriteBytes: (sampleCount - 1) * 100,});- const children = history.topProcesses.filter((entry) => entry.identity.pid === 4_242);+ const children = history.topProcesses.filter((entry) => entry.identity.pid === CODEX_PID);expect(children).toHaveLength(sampleCount);for (const child of children) {expect(child).toMatchObject({ sampleCount: 1, cpuTimeMs: 0, ioWriteBytes: 0 });}- expect(history.topProcesses.find((entry) => entry.identity.pid === 4_243)).toMatchObject({+ expect(+ history.topProcesses.find((entry) => entry.identity.pid === SHORT_LIVED_CHILD_PID),+ ).toMatchObject({sampleCount: 2,cpuTimeMs: 250,ioWriteBytes: 4_000,@@ -224,7 +234,7 @@ describe("ResourceTelemetry", () => {sampledAtUnixMs,childCpuTimeMs: 100,childWriteBytes: 1_000,- externalProcesses: [{ pid: 5_000, startTimeMs: 300 }],+ externalProcesses: [{ pid: ELECTRON_PID, startTimeMs: 300 }],});const desktop = {...desktopSnapshot(sampledAtUnixMs),@@ -253,9 +263,9 @@ describe("ResourceTelemetry", () => {return yield* telemetry.refresh;}).pipe(Effect.provide(layerTelemetry));- expect(snapshot.processes.find((entry) => entry.identity.pid === 5_000)?.category).toBe(- "electron-main",- );+ expect(+ snapshot.processes.find((entry) => entry.identity.pid === ELECTRON_PID)?.category,+ ).toBe("electron-main");expect(snapshot.groups.electron.processStarts).toBe(1);expect(snapshot.groups.backend.processStarts).toBe(3);}),