hostShutdownPhases.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. import { runHostShutdownPhases } from "../src/host/hostShutdownPhases.js";
  3. describe("host shutdown phases", () => {
  4. it("service 清理失败后仍继续关闭远程连接", async () => {
  5. const order: string[] = [];
  6. const log = vi.fn();
  7. const result = await runHostShutdownPhases(
  8. [
  9. {
  10. name: "service-dispose",
  11. run: async () => {
  12. order.push("service");
  13. throw new Error("service failed");
  14. },
  15. },
  16. {
  17. name: "remote-connection-dispose",
  18. run: async () => {
  19. order.push("remote");
  20. },
  21. },
  22. ],
  23. { phaseTimeoutMs: 1_000, log },
  24. );
  25. expect(order).toEqual(["service", "remote"]);
  26. expect(result.exitCode).toBe(1);
  27. expect(result.failedPhases).toEqual(["service-dispose"]);
  28. expect(log).toHaveBeenCalledWith(
  29. "host shutdown phase failed",
  30. expect.objectContaining({ phase: "service-dispose" }),
  31. );
  32. });
  33. it("单个阶段 pending 时在 deadline 后继续下一阶段", async () => {
  34. vi.useFakeTimers();
  35. try {
  36. const nextPhase = vi.fn().mockResolvedValue(undefined);
  37. const shutdown = runHostShutdownPhases(
  38. [
  39. { name: "service-dispose", run: () => new Promise<void>(() => {}) },
  40. { name: "remote-connection-dispose", run: nextPhase },
  41. ],
  42. { phaseTimeoutMs: 500, log: vi.fn() },
  43. );
  44. await vi.advanceTimersByTimeAsync(500);
  45. await expect(shutdown).resolves.toMatchObject({
  46. exitCode: 1,
  47. timedOutPhases: ["service-dispose"],
  48. });
  49. expect(nextPhase).toHaveBeenCalledTimes(1);
  50. } finally {
  51. vi.useRealTimers();
  52. }
  53. });
  54. });