GeezerVison is showing github.com — test(telemetry): derive fixture PIDs from the test worker PID · pingdotgg/t3code@a183588
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/diagnostics/ProcessDiagnostics.test.ts +25-18Lines changed: 25 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,11 @@ import * as ResourceAttribution from "../resourceTelemetry/ResourceAttribution.t | 15 | 15 | import * as ResourceTelemetry from "../resourceTelemetry/ResourceTelemetry.ts"; | 16 | 16 | import * as ProcessDiagnostics from "./ProcessDiagnostics.ts"; | 17 | 17 |
| | 18 | +// ProcessDiagnostics treats process.pid as the server, so the fixture PID is an | | 19 | +// offset from it. A fixed PID collides whenever the test worker is assigned it. | | 20 | +const SERVER_PID = process.pid; | | 21 | +const CHILD_PID = SERVER_PID + 1; | | 22 | + | 18 | 23 | function makeNativeSnapshot( | 19 | 24 | processes: ResourceMonitorSnapshotEvent["processes"], | 20 | 25 | ): ResourceMonitorSnapshotEvent { | @@ -66,7 +71,7 @@ describe("ProcessDiagnostics", () => { | 66 | 71 | Effect.gen(function* () { | 67 | 72 | const snapshot = makeNativeSnapshot([ | 68 | 73 | { | 69 | | - pid: process.pid, | | 74 | + pid: SERVER_PID, | 70 | 75 | ppid: 1, | 71 | 76 | startTimeMs: 1_000, | 72 | 77 | runTimeMs: 60_000, | @@ -82,8 +87,8 @@ describe("ProcessDiagnostics", () => { | 82 | 87 | ioSemantics: "storage", | 83 | 88 | }, | 84 | 89 | { | 85 | | - pid: 4_242, | 86 | | - ppid: process.pid, | | 90 | + pid: CHILD_PID, | | 91 | + ppid: SERVER_PID, | 87 | 92 | startTimeMs: 2_000, | 88 | 93 | runTimeMs: 4_000, | 89 | 94 | name: "agent", | @@ -106,7 +111,7 @@ describe("ProcessDiagnostics", () => { | 106 | 111 | return yield* processDiagnostics.read; | 107 | 112 | }).pipe(Effect.provide(layer)); | 108 | 113 |
| 109 | | - expect(diagnostics.processes.map((process) => process.pid)).toEqual([4242]); | | 114 | + expect(diagnostics.processes.map((process) => process.pid)).toEqual([CHILD_PID]); | 110 | 115 | expect(diagnostics.processes[0]?.startTimeMs).toBe(2_000); | 111 | 116 | expect(diagnostics.processes[0]?.cpuPercent).toBe(1.5); | 112 | 117 | expect(diagnostics.processes[0]?.rssBytes).toBe(2_048); | @@ -122,7 +127,7 @@ describe("ProcessDiagnostics", () => { | 122 | 127 | const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe( | 123 | 128 | Effect.flatMap((processDiagnostics) => | 124 | 129 | processDiagnostics.signal({ | 125 | | - pid: 4_242, | | 130 | + pid: CHILD_PID, | 126 | 131 | startTimeMs: 2_000, | 127 | 132 | signal: "SIGINT", | 128 | 133 | }), | @@ -131,10 +136,12 @@ describe("ProcessDiagnostics", () => { | 131 | 136 | ); | 132 | 137 |
| 133 | 138 | expect(result).toEqual({ | 134 | | - pid: 4242, | | 139 | + pid: CHILD_PID, | 135 | 140 | signal: "SIGINT", | 136 | 141 | signaled: false, | 137 | | - message: Option.some("Process 4242 no longer matches the selected process identity."), | | 142 | + message: Option.some( | | 143 | + `Process ${CHILD_PID} no longer matches the selected process identity.`, | | 144 | + ), | 138 | 145 | }); | 139 | 146 | }), | 140 | 147 | ); | @@ -143,8 +150,8 @@ describe("ProcessDiagnostics", () => { | 143 | 150 | Effect.gen(function* () { | 144 | 151 | const snapshot = makeNativeSnapshot([ | 145 | 152 | { | 146 | | - pid: 4_242, | 147 | | - ppid: process.pid, | | 153 | + pid: CHILD_PID, | | 154 | + ppid: SERVER_PID, | 148 | 155 | startTimeMs: 2_000, | 149 | 156 | runTimeMs: 4_000, | 150 | 157 | name: "agent", | @@ -185,7 +192,7 @@ describe("ProcessDiagnostics", () => { | 185 | 192 | const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe( | 186 | 193 | Effect.flatMap((processDiagnostics) => | 187 | 194 | processDiagnostics.signal({ | 188 | | - pid: 4_242, | | 195 | + pid: CHILD_PID, | 189 | 196 | startTimeMs: 2_000, | 190 | 197 | signal: "SIGINT", | 191 | 198 | }), | @@ -194,11 +201,11 @@ describe("ProcessDiagnostics", () => { | 194 | 201 | ); | 195 | 202 |
| 196 | 203 | expect(result).toEqual({ | 197 | | - pid: 4_242, | | 204 | + pid: CHILD_PID, | 198 | 205 | signal: "SIGINT", | 199 | 206 | signaled: false, | 200 | 207 | message: Option.some( | 201 | | - "Could not refresh process 4242; refusing to signal a stale identity.", | | 208 | + `Could not refresh process ${CHILD_PID}; refusing to signal a stale identity.`, | 202 | 209 | ), | 203 | 210 | }); | 204 | 211 | }), | @@ -211,7 +218,7 @@ describe("ProcessDiagnostics", () => { | 211 | 218 | ); | 212 | 219 | const snapshot = makeNativeSnapshot([ | 213 | 220 | { | 214 | | - pid: 4_242, | | 221 | + pid: CHILD_PID, | 215 | 222 | ppid: 1, | 216 | 223 | startTimeMs: 2_000, | 217 | 224 | runTimeMs: 4_000, | @@ -233,7 +240,7 @@ describe("ProcessDiagnostics", () => { | 233 | 240 | type: "desktopTelemetry", | 234 | 241 | sequence: 1, | 235 | 242 | sampledAtUnixMs, | 236 | | - electronPid: 4_242, | | 243 | + electronPid: CHILD_PID, | 237 | 244 | power: { | 238 | 245 | source: "electron-main", | 239 | 246 | idle: "false", | @@ -249,7 +256,7 @@ describe("ProcessDiagnostics", () => { | 249 | 256 | speedLimitPercent: Option.none(), | 250 | 257 | electronProcesses: [ | 251 | 258 | { | 252 | | - pid: 4_242, | | 259 | + pid: CHILD_PID, | 253 | 260 | creationTimeMs: 2_000, | 254 | 261 | type: "Browser", | 255 | 262 | name: "electron", | @@ -265,7 +272,7 @@ describe("ProcessDiagnostics", () => { | 265 | 272 | const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe( | 266 | 273 | Effect.flatMap((processDiagnostics) => | 267 | 274 | processDiagnostics.signal({ | 268 | | - pid: 4_242, | | 275 | + pid: CHILD_PID, | 269 | 276 | startTimeMs: 2_000, | 270 | 277 | signal: "SIGKILL", | 271 | 278 | }), | @@ -274,10 +281,10 @@ describe("ProcessDiagnostics", () => { | 274 | 281 | ); | 275 | 282 |
| 276 | 283 | expect(result).toEqual({ | 277 | | - pid: 4_242, | | 284 | + pid: CHILD_PID, | 278 | 285 | signal: "SIGKILL", | 279 | 286 | signaled: false, | 280 | | - message: Option.some("Process 4242 is not a signalable T3 backend descendant."), | | 287 | + message: Option.some(`Process ${CHILD_PID} is not a signalable T3 backend descendant.`), | 281 | 288 | }); | 282 | 289 |
| 283 | 290 | const diagnostics = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/resourceTelemetry/ResourceTelemetry.test.ts +42-29Lines changed: 42 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,15 @@ import * as NativeTelemetryClient from "./NativeTelemetryClient.ts"; | 21 | 21 | import * as ResourceAttribution from "./ResourceAttribution.ts"; | 22 | 22 | import * as ResourceTelemetry from "./ResourceTelemetry.ts"; | 23 | 23 |
| | 24 | +// ResourceTelemetry classifies process.pid as the server, so fixture PIDs are | | 25 | +// offsets from it. Fixed PIDs collide whenever the test worker is assigned one. | | 26 | +const SERVER_PID = process.pid; | | 27 | +const CODEX_PID = SERVER_PID + 1; | | 28 | +const SHORT_LIVED_CHILD_PID = SERVER_PID + 2; | | 29 | +const ELECTRON_PID = SERVER_PID + 3; | | 30 | +const MONITOR_PID = SERVER_PID + 4; | | 31 | +const RESTARTED_MONITOR_PID = SERVER_PID + 5; | | 32 | + | 24 | 33 | function processSample( | 25 | 34 | input: Partial<ResourceMonitorProcessSample> & | 26 | 35 | Pick<ResourceMonitorProcessSample, "pid" | "ppid" | "startTimeMs">, | @@ -50,31 +59,31 @@ function nativeSnapshot(input: { | 50 | 59 | }): ResourceMonitorSnapshotEvent { | 51 | 60 | const processes = [ | 52 | 61 | processSample({ | 53 | | - pid: process.pid, | | 62 | + pid: SERVER_PID, | 54 | 63 | ppid: 1, | 55 | 64 | startTimeMs: 100, | 56 | 65 | cpuTimeMs: input.sequence * 10, | 57 | 66 | }), | 58 | 67 | processSample({ | 59 | | - pid: 4_242, | 60 | | - ppid: process.pid, | | 68 | + pid: CODEX_PID, | | 69 | + ppid: SERVER_PID, | 61 | 70 | startTimeMs: 200, | 62 | 71 | name: "codex", | 63 | 72 | command: "codex app-server", | 64 | 73 | cpuTimeMs: input.childCpuTimeMs, | 65 | 74 | ioWriteBytes: input.childWriteBytes, | 66 | 75 | }), | 67 | 76 | processSample({ | 68 | | - pid: 5_000, | | 77 | + pid: ELECTRON_PID, | 69 | 78 | ppid: 1, | 70 | 79 | startTimeMs: 300, | 71 | 80 | name: "electron", | 72 | 81 | command: "electron", | 73 | 82 | cpuTimeMs: input.sequence * 20, | 74 | 83 | }), | 75 | 84 | processSample({ | 76 | | - pid: 9_000, | 77 | | - ppid: process.pid, | | 85 | + pid: MONITOR_PID, | | 86 | + ppid: SERVER_PID, | 78 | 87 | startTimeMs: 400, | 79 | 88 | name: "t3-resource-monitor", | 80 | 89 | command: "t3-resource-monitor", | @@ -111,7 +120,7 @@ function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshot | 111 | 120 | type: "desktopTelemetry", | 112 | 121 | sequence: 1, | 113 | 122 | sampledAtUnixMs, | 114 | | - electronPid: 5_000, | | 123 | + electronPid: ELECTRON_PID, | 115 | 124 | power: { | 116 | 125 | source: "electron-main", | 117 | 126 | idle: "false", | @@ -127,7 +136,7 @@ function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshot | 127 | 136 | speedLimitPercent: Option.some(90), | 128 | 137 | electronProcesses: [ | 129 | 138 | { | 130 | | - pid: 5_000, | | 139 | + pid: ELECTRON_PID, | 131 | 140 | creationTimeMs: 300, | 132 | 141 | type: "Browser", | 133 | 142 | name: "electron", | @@ -154,17 +163,17 @@ describe("ResourceTelemetry", () => { | 154 | 163 | childWriteBytes: index * 100, | 155 | 164 | }); | 156 | 165 | const processes = sample.processes | 157 | | - .filter((entry) => entry.pid === process.pid || entry.pid === 4_242) | | 166 | + .filter((entry) => entry.pid === SERVER_PID || entry.pid === CODEX_PID) | 158 | 167 | .map((entry) => | 159 | | - entry.pid === 4_242 | | 168 | + entry.pid === CODEX_PID | 160 | 169 | ? { ...entry, startTimeMs: index } | 161 | 170 | : { ...entry, ioWriteBytes: index * 100 }, | 162 | 171 | ); | 163 | 172 | if (index === 0 || index === 2) { | 164 | 173 | processes.push( | 165 | 174 | processSample({ | 166 | | - pid: 4_243, | 167 | | - ppid: process.pid, | | 175 | + pid: SHORT_LIVED_CHILD_PID, | | 176 | + ppid: SERVER_PID, | 168 | 177 | startTimeMs: 500, | 169 | 178 | cpuTimeMs: index === 0 ? 100 : 350, | 170 | 179 | ioWriteBytes: index === 0 ? 1_000 : 5_000, | @@ -192,18 +201,20 @@ describe("ResourceTelemetry", () => { | 192 | 201 |
| 193 | 202 | expect(history.topProcesses).toHaveLength(sampleCount + 2); | 194 | 203 | expect( | 195 | | - history.topProcesses.find((entry) => entry.identity.pid === process.pid), | | 204 | + history.topProcesses.find((entry) => entry.identity.pid === SERVER_PID), | 196 | 205 | ).toMatchObject({ | 197 | 206 | sampleCount, | 198 | 207 | cpuTimeMs: (sampleCount - 1) * 10, | 199 | 208 | ioWriteBytes: (sampleCount - 1) * 100, | 200 | 209 | }); | 201 | | - const children = history.topProcesses.filter((entry) => entry.identity.pid === 4_242); | | 210 | + const children = history.topProcesses.filter((entry) => entry.identity.pid === CODEX_PID); | 202 | 211 | expect(children).toHaveLength(sampleCount); | 203 | 212 | for (const child of children) { | 204 | 213 | expect(child).toMatchObject({ sampleCount: 1, cpuTimeMs: 0, ioWriteBytes: 0 }); | 205 | 214 | } | 206 | | - expect(history.topProcesses.find((entry) => entry.identity.pid === 4_243)).toMatchObject({ | | 215 | + expect( | | 216 | + history.topProcesses.find((entry) => entry.identity.pid === SHORT_LIVED_CHILD_PID), | | 217 | + ).toMatchObject({ | 207 | 218 | sampleCount: 2, | 208 | 219 | cpuTimeMs: 250, | 209 | 220 | ioWriteBytes: 4_000, | @@ -307,7 +318,7 @@ describe("ResourceTelemetry", () => { | 307 | 318 | sampledAtUnixMs, | 308 | 319 | childCpuTimeMs: 100, | 309 | 320 | childWriteBytes: 1_000, | 310 | | - externalProcesses: [{ pid: 5_000, startTimeMs: 300 }], | | 321 | + externalProcesses: [{ pid: ELECTRON_PID, startTimeMs: 300 }], | 311 | 322 | }); | 312 | 323 | const desktop = { | 313 | 324 | ...desktopSnapshot(sampledAtUnixMs), | @@ -336,9 +347,9 @@ describe("ResourceTelemetry", () => { | 336 | 347 | return yield* telemetry.refresh; | 337 | 348 | }).pipe(Effect.provide(layerTelemetry)); | 338 | 349 |
| 339 | | - expect(snapshot.processes.find((entry) => entry.identity.pid === 5_000)?.category).toBe( | 340 | | - "electron-main", | 341 | | - ); | | 350 | + expect( | | 351 | + snapshot.processes.find((entry) => entry.identity.pid === ELECTRON_PID)?.category, | | 352 | + ).toBe("electron-main"); | 342 | 353 | expect(snapshot.groups.electron.processStarts).toBe(1); | 343 | 354 | expect(snapshot.groups.backend.processStarts).toBe(3); | 344 | 355 | }), | @@ -574,7 +585,7 @@ describe("ResourceTelemetry", () => { | 574 | 585 | version: 3, | 575 | 586 | type: "hello", | 576 | 587 | sidecarVersion: "0.1.0", | 577 | | - sidecarPid: 9_000, | | 588 | + sidecarPid: MONITOR_PID, | 578 | 589 | platform: "test", | 579 | 590 | arch: "test", | 580 | 591 | capabilities: { | @@ -629,7 +640,9 @@ describe("ResourceTelemetry", () => { | 629 | 640 | const telemetry = yield* ResourceTelemetry.ResourceTelemetry; | 630 | 641 | const attribution = yield* ResourceAttribution.ResourceAttribution; | 631 | 642 |
| 632 | | - expect(yield* Ref.get(externalProcesses)).toEqual([{ pid: 5_000, startTimeMs: 300 }]); | | 643 | + expect(yield* Ref.get(externalProcesses)).toEqual([ | | 644 | + { pid: ELECTRON_PID, startTimeMs: 300 }, | | 645 | + ]); | 633 | 646 |
| 634 | 647 | yield* attribution.record({ | 635 | 648 | component: "provider-event-log", | @@ -657,7 +670,7 @@ describe("ResourceTelemetry", () => { | 657 | 670 |
| 658 | 671 | yield* TestClock.adjust(Duration.seconds(1)); | 659 | 672 | const second = yield* telemetry.refresh; | 660 | | - const codex = second.processes.find((entry) => entry.identity.pid === 4_242); | | 673 | + const codex = second.processes.find((entry) => entry.identity.pid === CODEX_PID); | 661 | 674 | expect(codex?.cpuPercent).toBe(25); | 662 | 675 | expect(codex?.ioWriteBytesPerSecond).toBe(4_000); | 663 | 676 | expect(second.groups.backend.ioWriteBytes).toBe(4_000); | @@ -671,13 +684,13 @@ describe("ResourceTelemetry", () => { | 671 | 684 | }); | 672 | 685 | expect(history.retainedSampleCount).toBeGreaterThan(0); | 673 | 686 | expect( | 674 | | - history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.sampleCount, | | 687 | + history.topProcesses.find((entry) => entry.identity.pid === CODEX_PID)?.sampleCount, | 675 | 688 | ).toBe(2); | 676 | | - expect(history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.cpuTimeMs).toBe( | 677 | | - 250, | 678 | | - ); | 679 | 689 | expect( | 680 | | - history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.ioWriteBytes, | | 690 | + history.topProcesses.find((entry) => entry.identity.pid === CODEX_PID)?.cpuTimeMs, | | 691 | + ).toBe(250); | | 692 | + expect( | | 693 | + history.topProcesses.find((entry) => entry.identity.pid === CODEX_PID)?.ioWriteBytes, | 681 | 694 | ).toBe(4_000); | 682 | 695 | expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe( | 683 | 696 | 4_000, | @@ -691,14 +704,14 @@ describe("ResourceTelemetry", () => { | 691 | 704 | ...current, | 692 | 705 | hello: Option.map(current.hello, (hello) => ({ | 693 | 706 | ...hello, | 694 | | - sidecarPid: 9_001, | | 707 | + sidecarPid: RESTARTED_MONITOR_PID, | 695 | 708 | })), | 696 | 709 | restartCount: 3, | 697 | 710 | })); | 698 | 711 | yield* TestClock.adjust(Duration.seconds(1)); | 699 | 712 | const restarted = yield* telemetry.refresh; | 700 | 713 | expect(DateTime.toEpochMillis(restarted.readAt)).toBe(startedAt + 2_000); | 701 | | - expect(Option.getOrNull(restarted.health.sidecarPid)).toBe(9_001); | | 714 | + expect(Option.getOrNull(restarted.health.sidecarPid)).toBe(RESTARTED_MONITOR_PID); | 702 | 715 |
| 703 | 716 | yield* Ref.update(nativeHealth, (current) => ({ | 704 | 717 | ...current, | |
|