|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const warn = vi.hoisted(() => vi.fn()); |
| 4 | + |
| 5 | +vi.mock("./logger", () => ({ |
| 6 | + logger: { |
| 7 | + scope: () => ({ |
| 8 | + info: vi.fn(), |
| 9 | + error: vi.fn(), |
| 10 | + warn, |
| 11 | + debug: vi.fn(), |
| 12 | + }), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +import { subscribeWithTimeout, withTimeout } from "./async"; |
| 17 | + |
| 18 | +interface FakeSubscription { |
| 19 | + unsubscribe: () => Promise<unknown>; |
| 20 | +} |
| 21 | + |
| 22 | +const makeSubscription = ( |
| 23 | + unsubscribeImpl: () => Promise<unknown> = () => Promise.resolve(), |
| 24 | +): FakeSubscription & { unsubscribe: ReturnType<typeof vi.fn> } => { |
| 25 | + const unsubscribe = vi.fn( |
| 26 | + unsubscribeImpl, |
| 27 | + ) as unknown as (() => Promise<unknown>) & ReturnType<typeof vi.fn>; |
| 28 | + return { unsubscribe }; |
| 29 | +}; |
| 30 | + |
| 31 | +const deferred = <T>() => { |
| 32 | + let resolve!: (value: T) => void; |
| 33 | + let reject!: (reason?: unknown) => void; |
| 34 | + const promise = new Promise<T>((res, rej) => { |
| 35 | + resolve = res; |
| 36 | + reject = rej; |
| 37 | + }); |
| 38 | + return { promise, resolve, reject }; |
| 39 | +}; |
| 40 | + |
| 41 | +describe("withTimeout", () => { |
| 42 | + beforeEach(() => { |
| 43 | + vi.useFakeTimers(); |
| 44 | + warn.mockClear(); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + vi.useRealTimers(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns success with the value when the operation resolves first", async () => { |
| 52 | + const result = await withTimeout(Promise.resolve("done"), 1000); |
| 53 | + expect(result).toEqual({ result: "success", value: "done" }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns timeout when the operation is slower than the deadline", async () => { |
| 57 | + const { promise } = deferred<string>(); |
| 58 | + const racePromise = withTimeout(promise, 1000); |
| 59 | + await vi.advanceTimersByTimeAsync(1000); |
| 60 | + expect(await racePromise).toEqual({ result: "timeout" }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("clears the timeout timer on success", async () => { |
| 64 | + const clearSpy = vi.spyOn(globalThis, "clearTimeout"); |
| 65 | + await withTimeout(Promise.resolve("done"), 1000); |
| 66 | + expect(clearSpy).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("subscribeWithTimeout", () => { |
| 71 | + beforeEach(() => { |
| 72 | + vi.useFakeTimers(); |
| 73 | + warn.mockClear(); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + vi.useRealTimers(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns success and the subscription when subscribe resolves first", async () => { |
| 81 | + const sub = makeSubscription(); |
| 82 | + const result = await subscribeWithTimeout( |
| 83 | + Promise.resolve(sub), |
| 84 | + 1000, |
| 85 | + "test", |
| 86 | + ); |
| 87 | + expect(result).toEqual({ result: "success", subscription: sub }); |
| 88 | + expect(sub.unsubscribe).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("clears the timeout timer on success", async () => { |
| 92 | + const clearSpy = vi.spyOn(globalThis, "clearTimeout"); |
| 93 | + await subscribeWithTimeout( |
| 94 | + Promise.resolve(makeSubscription()), |
| 95 | + 1000, |
| 96 | + "test", |
| 97 | + ); |
| 98 | + expect(clearSpy).toHaveBeenCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it("returns timeout and unsubscribes the late subscription", async () => { |
| 102 | + const sub = makeSubscription(); |
| 103 | + const { promise, resolve } = deferred<FakeSubscription>(); |
| 104 | + |
| 105 | + const racePromise = subscribeWithTimeout(promise, 1000, "late-sub"); |
| 106 | + await vi.advanceTimersByTimeAsync(1000); |
| 107 | + expect(await racePromise).toEqual({ result: "timeout" }); |
| 108 | + |
| 109 | + resolve(sub); |
| 110 | + await vi.runAllTimersAsync(); |
| 111 | + await Promise.resolve(); |
| 112 | + |
| 113 | + expect(sub.unsubscribe).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it("logs a warning when the late unsubscribe rejects", async () => { |
| 117 | + const sub = makeSubscription(() => Promise.reject(new Error("nope"))); |
| 118 | + const { promise, resolve } = deferred<FakeSubscription>(); |
| 119 | + |
| 120 | + const racePromise = subscribeWithTimeout(promise, 1000, "boom"); |
| 121 | + await vi.advanceTimersByTimeAsync(1000); |
| 122 | + await racePromise; |
| 123 | + |
| 124 | + resolve(sub); |
| 125 | + await vi.runAllTimersAsync(); |
| 126 | + await Promise.resolve(); |
| 127 | + await Promise.resolve(); |
| 128 | + |
| 129 | + expect(warn).toHaveBeenCalledWith( |
| 130 | + expect.stringContaining("Failed to tear down late subscription (boom)"), |
| 131 | + expect.any(Error), |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it("logs a warning when the subscribe promise rejects after the timeout", async () => { |
| 136 | + const { promise, reject } = deferred<FakeSubscription>(); |
| 137 | + |
| 138 | + const racePromise = subscribeWithTimeout(promise, 1000, "rejected-late"); |
| 139 | + await vi.advanceTimersByTimeAsync(1000); |
| 140 | + await racePromise; |
| 141 | + |
| 142 | + reject(new Error("subscribe blew up")); |
| 143 | + await vi.runAllTimersAsync(); |
| 144 | + await Promise.resolve(); |
| 145 | + await Promise.resolve(); |
| 146 | + |
| 147 | + expect(warn).toHaveBeenCalledWith( |
| 148 | + expect.stringContaining( |
| 149 | + "Late subscribe rejected after timeout (rejected-late)", |
| 150 | + ), |
| 151 | + expect.any(Error), |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it("propagates a subscribe rejection that beats the timeout", async () => { |
| 156 | + const failing = Promise.reject(new Error("immediate fail")); |
| 157 | + await expect( |
| 158 | + subscribeWithTimeout(failing, 1000, "early-fail"), |
| 159 | + ).rejects.toThrow("immediate fail"); |
| 160 | + }); |
| 161 | +}); |
0 commit comments