|
1 | 1 | // Managed Child Process tests cover managed child process script behavior. |
2 | | -import { spawn, spawnSync } from "node:child_process"; |
| 2 | +import childProcess, { spawn, spawnSync } from "node:child_process"; |
3 | 3 | import fs from "node:fs"; |
| 4 | +import { syncBuiltinESMExports } from "node:module"; |
4 | 5 | import os from "node:os"; |
5 | 6 | import path from "node:path"; |
6 | 7 | import { setTimeout as delay } from "node:timers/promises"; |
@@ -589,6 +590,83 @@ setInterval(() => {}, 1_000); |
589 | 590 | ).toBe("dead"); |
590 | 591 | }); |
591 | 592 |
|
| 593 | + it.each<{ |
| 594 | + snapshot: "empty" | "live" | "failed" | "zombie"; |
| 595 | + afterSnapshot: string | null; |
| 596 | + expected: ReturnType<typeof inspectManagedProcessGroup>; |
| 597 | + policy?: Parameters<typeof inspectManagedProcessGroup>[1]["errorPolicy"]; |
| 598 | + platform?: NodeJS.Platform; |
| 599 | + running?: boolean; |
| 600 | + }>([ |
| 601 | + { snapshot: "empty", afterSnapshot: "ESRCH", expected: "dead" }, |
| 602 | + { snapshot: "live", afterSnapshot: "ESRCH", expected: "dead" }, |
| 603 | + { snapshot: "failed", afterSnapshot: "ESRCH", expected: "dead" }, |
| 604 | + { snapshot: "empty", afterSnapshot: null, expected: "live" }, |
| 605 | + { snapshot: "live", afterSnapshot: null, expected: "live" }, |
| 606 | + { snapshot: "failed", afterSnapshot: null, expected: "live" }, |
| 607 | + { snapshot: "zombie", afterSnapshot: null, expected: "dead" }, |
| 608 | + { snapshot: "empty", afterSnapshot: "EPERM", expected: "live" }, |
| 609 | + { |
| 610 | + snapshot: "empty", |
| 611 | + afterSnapshot: "EPERM", |
| 612 | + policy: "indeterminate", |
| 613 | + expected: "indeterminate", |
| 614 | + }, |
| 615 | + { snapshot: "empty", afterSnapshot: "EPERM", policy: "verify-leader", expected: "dead" }, |
| 616 | + { |
| 617 | + snapshot: "empty", |
| 618 | + afterSnapshot: "EIO", |
| 619 | + policy: "indeterminate", |
| 620 | + expected: "indeterminate", |
| 621 | + }, |
| 622 | + { snapshot: "empty", afterSnapshot: "EIO", expected: "dead" }, |
| 623 | + { snapshot: "zombie", afterSnapshot: null, platform: "darwin", expected: "live" }, |
| 624 | + { snapshot: "zombie", afterSnapshot: null, running: true, expected: "live" }, |
| 625 | + ])( |
| 626 | + "checks current group state after $snapshot snapshot ($afterSnapshot, $policy, $platform, $running)", |
| 627 | + ({ |
| 628 | + snapshot, |
| 629 | + afterSnapshot, |
| 630 | + expected, |
| 631 | + policy = "alive-on-eperm", |
| 632 | + platform = "linux", |
| 633 | + running, |
| 634 | + }) => { |
| 635 | + let inspected = false; |
| 636 | + const kill = vi.spyOn(process, "kill").mockImplementation(() => { |
| 637 | + if (inspected && afterSnapshot) { |
| 638 | + throw Object.assign(new Error("group lookup failed"), { code: afterSnapshot }); |
| 639 | + } |
| 640 | + return true; |
| 641 | + }); |
| 642 | + const ps = vi.spyOn(childProcess, "spawnSync").mockImplementation(() => { |
| 643 | + inspected = true; |
| 644 | + return { |
| 645 | + pid: 12346, |
| 646 | + output: [], |
| 647 | + signal: null, |
| 648 | + status: snapshot === "empty" ? 1 : 0, |
| 649 | + stdout: snapshot === "zombie" ? "12345 Z\n" : snapshot === "live" ? "12345 S\n" : "", |
| 650 | + stderr: "", |
| 651 | + ...(snapshot === "failed" ? { error: new Error("ps unavailable") } : {}), |
| 652 | + }; |
| 653 | + }); |
| 654 | + syncBuiltinESMExports(); |
| 655 | + try { |
| 656 | + expect( |
| 657 | + inspectManagedProcessGroup( |
| 658 | + { pid: 12345, exitCode: running ? null : 0, signalCode: null }, |
| 659 | + { errorPolicy: policy, platform }, |
| 660 | + ), |
| 661 | + ).toBe(expected); |
| 662 | + } finally { |
| 663 | + ps.mockRestore(); |
| 664 | + kill.mockRestore(); |
| 665 | + syncBuiltinESMExports(); |
| 666 | + } |
| 667 | + }, |
| 668 | + ); |
| 669 | + |
592 | 670 | it("bounds process-group waiting when the group remains live", async () => { |
593 | 671 | const kill = vi.spyOn(process, "kill").mockReturnValue(true); |
594 | 672 |
|
|
0 commit comments