bash-process-stop.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type { ChildProcess } from "node:child_process";
  2. import { afterEach, expect, it, vi } from "vitest";
  3. import { NodeExecutionAdapterProcess } from "../src/exec/node-execution-adapter-process.js";
  4. import { BASH_SIGTERM_TO_SIGKILL_MS, signalPosixProcessTree } from "../src/exec/process-tree.js";
  5. vi.mock("../src/exec/process-tree.js", async (importOriginal) => ({
  6. ...(await importOriginal<typeof import("../src/exec/process-tree.js")>()),
  7. signalPosixProcessTree: vi.fn(),
  8. }));
  9. class StopTestAdapter extends NodeExecutionAdapterProcess {
  10. stopBash(): void {
  11. this.terminateProcessTree({ pid: 12345 } as ChildProcess, true);
  12. }
  13. }
  14. afterEach(() => {
  15. vi.restoreAllMocks();
  16. vi.useRealTimers();
  17. });
  18. it("waits for asynchronous SIGKILL dispatch, not just its escalation timer", async () => {
  19. vi.useFakeTimers();
  20. vi.spyOn(process, "kill").mockReturnValue(true);
  21. let finishDispatch!: () => void;
  22. const dispatch = new Promise<void>((resolve) => {
  23. finishDispatch = resolve;
  24. });
  25. vi.mocked(signalPosixProcessTree).mockImplementation((_pid, signal) =>
  26. signal === "SIGKILL" ? dispatch : Promise.resolve(),
  27. );
  28. const adapter = new StopTestAdapter({ platform: "linux" });
  29. let closed = false;
  30. adapter.stopBash();
  31. const closing = adapter.close().then(() => {
  32. closed = true;
  33. });
  34. try {
  35. await vi.advanceTimersByTimeAsync(BASH_SIGTERM_TO_SIGKILL_MS);
  36. expect(signalPosixProcessTree).toHaveBeenCalledWith(12345, "SIGKILL");
  37. expect(closed).toBe(false);
  38. finishDispatch();
  39. await closing;
  40. expect(closed).toBe(true);
  41. } finally {
  42. finishDispatch();
  43. await closing;
  44. }
  45. });