bash-progress-poller.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import { subscribeBashOutputProgress } from "../src/exec/bash-progress-poller.js";
  3. const subscriptions: Array<() => void> = [];
  4. function subscribe(poll: (isActive: () => boolean) => Promise<void>, intervalMs = 1000) {
  5. const stop = subscribeBashOutputProgress(intervalMs, poll);
  6. subscriptions.push(stop);
  7. return stop;
  8. }
  9. beforeEach(() => vi.useFakeTimers());
  10. afterEach(() => {
  11. for (const stop of subscriptions.splice(0)) stop();
  12. expect(vi.getTimerCount()).toBe(0);
  13. vi.useRealTimers();
  14. });
  15. describe("shared Bash progress polling", () => {
  16. it("shares one interval and adding a subscriber does not reset its tick", async () => {
  17. const first = vi.fn(async () => {});
  18. const second = vi.fn(async () => {});
  19. subscribe(first);
  20. await vi.advanceTimersByTimeAsync(500);
  21. subscribe(second);
  22. expect(vi.getTimerCount()).toBe(1);
  23. await vi.advanceTimersByTimeAsync(500);
  24. expect(first).toHaveBeenCalledTimes(1);
  25. expect(second).toHaveBeenCalledTimes(1);
  26. });
  27. it("keeps other subscribers running and releases the interval after the last unsubscribe", async () => {
  28. const first = vi.fn(async () => {});
  29. const second = vi.fn(async () => {});
  30. const stopFirst = subscribe(first);
  31. const stopSecond = subscribe(second);
  32. stopFirst();
  33. expect(vi.getTimerCount()).toBe(1);
  34. await vi.advanceTimersByTimeAsync(1000);
  35. expect(first).not.toHaveBeenCalled();
  36. expect(second).toHaveBeenCalledTimes(1);
  37. stopSecond();
  38. expect(vi.getTimerCount()).toBe(0);
  39. await vi.advanceTimersByTimeAsync(3000);
  40. expect(second).toHaveBeenCalledTimes(1);
  41. });
  42. it("does not overlap a slow read or block another subscriber", async () => {
  43. let release!: () => void;
  44. const barrier = new Promise<void>((resolve) => {
  45. release = resolve;
  46. });
  47. const slow = vi.fn(async () => {
  48. await barrier;
  49. });
  50. const fast = vi.fn(async () => {});
  51. subscribe(slow);
  52. subscribe(fast);
  53. await vi.advanceTimersByTimeAsync(3000);
  54. expect(slow).toHaveBeenCalledTimes(1);
  55. expect(fast).toHaveBeenCalledTimes(3);
  56. release();
  57. await vi.advanceTimersByTimeAsync(1000);
  58. expect(slow).toHaveBeenCalledTimes(2);
  59. expect(fast).toHaveBeenCalledTimes(4);
  60. });
  61. it("isolates failing reads and callbacks and retries on the next tick", async () => {
  62. const failure = vi
  63. .fn()
  64. .mockRejectedValueOnce(new Error("read failed"))
  65. .mockResolvedValue(undefined);
  66. const throws = vi.fn(() => {
  67. throw new Error("callback failed");
  68. });
  69. const healthy = vi.fn(async () => {});
  70. subscribe(failure);
  71. const stopThrowing = subscribe(throws);
  72. subscribe(healthy);
  73. await vi.advanceTimersByTimeAsync(2000);
  74. expect(failure).toHaveBeenCalledTimes(2);
  75. expect(healthy).toHaveBeenCalledTimes(2);
  76. stopThrowing();
  77. });
  78. it("rejects late results and an old unsubscribe cannot remove a restarted subscription", async () => {
  79. let release!: () => void;
  80. const barrier = new Promise<void>((resolve) => {
  81. release = resolve;
  82. });
  83. const onRead = vi.fn();
  84. const stopOld = subscribe(async (isActive) => {
  85. await barrier;
  86. if (isActive()) onRead();
  87. });
  88. await vi.advanceTimersByTimeAsync(1000);
  89. stopOld();
  90. expect(vi.getTimerCount()).toBe(0);
  91. const fresh = vi.fn(async () => {});
  92. subscribe(fresh);
  93. stopOld();
  94. release();
  95. await vi.advanceTimersByTimeAsync(1000);
  96. expect(onRead).not.toHaveBeenCalled();
  97. expect(fresh).toHaveBeenCalledTimes(1);
  98. expect(vi.getTimerCount()).toBe(1);
  99. });
  100. it("keeps different internal intervals independent", async () => {
  101. const short = vi.fn(async () => {});
  102. const normal = vi.fn(async () => {});
  103. subscribe(short, 100);
  104. subscribe(normal);
  105. expect(vi.getTimerCount()).toBe(2);
  106. await vi.advanceTimersByTimeAsync(1000);
  107. expect(short).toHaveBeenCalledTimes(10);
  108. expect(normal).toHaveBeenCalledTimes(1);
  109. });
  110. });