|
| 1 | +/** |
| 2 | + * The timing of idle-session eviction (#5558), with fake timers and no HTTP: |
| 3 | + * what `SESSION_IDLE_TIMEOUT_MILLIS` is read as, that a session is evicted |
| 4 | + * once, only after it has had no open response for the timeout, and that a |
| 5 | + * timeout of 0 and `stop()` both end the watching. A response is an |
| 6 | + * `EventEmitter` here, since "close" is all the module listens for. What |
| 7 | + * eviction does to a real session is in `streamable-http.test.ts`. |
| 8 | + */ |
| 9 | +import { EventEmitter } from "node:events"; |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | +import { |
| 12 | + DEFAULT_SESSION_IDLE_TIMEOUT_MILLIS, |
| 13 | + sessionIdleTimeoutMillis, |
| 14 | + watchIdleSessions, |
| 15 | +} from "../transports/idle-sessions.js"; |
| 16 | + |
| 17 | +describe("sessionIdleTimeoutMillis", () => { |
| 18 | + afterEach(() => { |
| 19 | + vi.unstubAllEnvs(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("defaults to 30 minutes", () => { |
| 23 | + expect(DEFAULT_SESSION_IDLE_TIMEOUT_MILLIS).toBe(1_800_000); |
| 24 | + expect(sessionIdleTimeoutMillis(undefined)).toBe(1_800_000); |
| 25 | + }); |
| 26 | + |
| 27 | + it("reads SESSION_IDLE_TIMEOUT_MILLIS from the environment", () => { |
| 28 | + vi.stubEnv("SESSION_IDLE_TIMEOUT_MILLIS", "5000"); |
| 29 | + expect(sessionIdleTimeoutMillis()).toBe(5000); |
| 30 | + }); |
| 31 | + |
| 32 | + it("takes a number of milliseconds, and 0 as disabled", () => { |
| 33 | + expect(sessionIdleTimeoutMillis("60000")).toBe(60_000); |
| 34 | + expect(sessionIdleTimeoutMillis(" 250 ")).toBe(250); |
| 35 | + expect(sessionIdleTimeoutMillis("0")).toBe(0); |
| 36 | + }); |
| 37 | + |
| 38 | + it.each(["", " ", "-1", "soon", "Infinity"])( |
| 39 | + "falls back to the default for %j", |
| 40 | + (value) => { |
| 41 | + expect(sessionIdleTimeoutMillis(value)).toBe( |
| 42 | + DEFAULT_SESSION_IDLE_TIMEOUT_MILLIS, |
| 43 | + ); |
| 44 | + }, |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("watchIdleSessions", () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.useFakeTimers(); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + vi.useRealTimers(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("evicts a session once, after it has been idle for the timeout", () => { |
| 58 | + const evict = vi.fn(); |
| 59 | + const idle = watchIdleSessions(5000, evict); |
| 60 | + idle.add("a"); |
| 61 | + vi.advanceTimersByTime(4999); |
| 62 | + expect(evict).not.toHaveBeenCalled(); |
| 63 | + vi.advanceTimersByTime(1); |
| 64 | + expect(evict).toHaveBeenCalledExactlyOnceWith("a"); |
| 65 | + vi.advanceTimersByTime(60_000); |
| 66 | + expect(evict).toHaveBeenCalledOnce(); |
| 67 | + idle.stop(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps a session with an open response, and times it from when the response closes", () => { |
| 71 | + const evict = vi.fn(); |
| 72 | + const idle = watchIdleSessions(5000, evict); |
| 73 | + idle.add("active"); |
| 74 | + idle.add("abandoned"); |
| 75 | + const stream = new EventEmitter(); |
| 76 | + const call = new EventEmitter(); |
| 77 | + idle.hold("active", stream); |
| 78 | + idle.hold("active", call); |
| 79 | + vi.advanceTimersByTime(5000); |
| 80 | + expect(evict).toHaveBeenCalledExactlyOnceWith("abandoned"); |
| 81 | + |
| 82 | + // One of two responses closing leaves the session in use. |
| 83 | + call.emit("close"); |
| 84 | + vi.advanceTimersByTime(60_000); |
| 85 | + expect(evict).toHaveBeenCalledOnce(); |
| 86 | + |
| 87 | + // The clock starts when the last one closes, not when it opened. |
| 88 | + stream.emit("close"); |
| 89 | + vi.advanceTimersByTime(4999); |
| 90 | + expect(evict).toHaveBeenCalledOnce(); |
| 91 | + vi.advanceTimersByTime(1); |
| 92 | + expect(evict).toHaveBeenLastCalledWith("active"); |
| 93 | + expect(evict).toHaveBeenCalledTimes(2); |
| 94 | + idle.stop(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("restarts a session's clock on each request", () => { |
| 98 | + const evict = vi.fn(); |
| 99 | + const idle = watchIdleSessions(5000, evict); |
| 100 | + idle.add("a"); |
| 101 | + for (let i = 0; i < 4; i++) { |
| 102 | + vi.advanceTimersByTime(4000); |
| 103 | + const res = new EventEmitter(); |
| 104 | + idle.hold("a", res); |
| 105 | + res.emit("close"); |
| 106 | + } |
| 107 | + expect(evict).not.toHaveBeenCalled(); |
| 108 | + idle.stop(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("does not evict a session that was removed, or hold one it does not watch", () => { |
| 112 | + const evict = vi.fn(); |
| 113 | + const idle = watchIdleSessions(5000, evict); |
| 114 | + idle.add("ended"); |
| 115 | + idle.remove("ended"); |
| 116 | + const res = new EventEmitter(); |
| 117 | + idle.hold("unknown", res); |
| 118 | + expect(res.listenerCount("close")).toBe(0); |
| 119 | + vi.advanceTimersByTime(60_000); |
| 120 | + expect(evict).not.toHaveBeenCalled(); |
| 121 | + idle.stop(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("looks for idle sessions every tenth of the timeout", () => { |
| 125 | + const evict = vi.fn(); |
| 126 | + const idle = watchIdleSessions(50_000, evict); |
| 127 | + // Idle from 2 s in: past the timeout at 52 s, and seen at the 55 s look. |
| 128 | + vi.advanceTimersByTime(2000); |
| 129 | + idle.add("a"); |
| 130 | + vi.advanceTimersByTime(52_999); |
| 131 | + expect(evict).not.toHaveBeenCalled(); |
| 132 | + vi.advanceTimersByTime(1); |
| 133 | + expect(evict).toHaveBeenCalledExactlyOnceWith("a"); |
| 134 | + idle.stop(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("looks at least every minute, however long the timeout", () => { |
| 138 | + const evict = vi.fn(); |
| 139 | + const idle = watchIdleSessions(30 * 60_000, evict); |
| 140 | + vi.advanceTimersByTime(60_000); |
| 141 | + idle.add("a"); |
| 142 | + // Idle from 1:00, so past the timeout at 31:00, the next look. |
| 143 | + vi.advanceTimersByTime(30 * 60_000 - 1); |
| 144 | + expect(evict).not.toHaveBeenCalled(); |
| 145 | + vi.advanceTimersByTime(1); |
| 146 | + expect(evict).toHaveBeenCalledExactlyOnceWith("a"); |
| 147 | + idle.stop(); |
| 148 | + }); |
| 149 | + |
| 150 | + it("looks no more often than once a second when the timeout is shorter", () => { |
| 151 | + const evict = vi.fn(); |
| 152 | + const idle = watchIdleSessions(10, evict); |
| 153 | + idle.add("a"); |
| 154 | + vi.advanceTimersByTime(999); |
| 155 | + expect(evict).not.toHaveBeenCalled(); |
| 156 | + vi.advanceTimersByTime(1); |
| 157 | + expect(evict).toHaveBeenCalledExactlyOnceWith("a"); |
| 158 | + idle.stop(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("never evicts, and runs no timer, when the timeout is 0", () => { |
| 162 | + const evict = vi.fn(); |
| 163 | + const idle = watchIdleSessions(0, evict); |
| 164 | + expect(vi.getTimerCount()).toBe(0); |
| 165 | + idle.add("a"); |
| 166 | + vi.advanceTimersByTime(24 * 60 * 60 * 1000); |
| 167 | + expect(evict).not.toHaveBeenCalled(); |
| 168 | + idle.stop(); |
| 169 | + }); |
| 170 | + |
| 171 | + it("stops looking once stopped", () => { |
| 172 | + const evict = vi.fn(); |
| 173 | + const idle = watchIdleSessions(5000, evict); |
| 174 | + idle.add("a"); |
| 175 | + idle.stop(); |
| 176 | + expect(vi.getTimerCount()).toBe(0); |
| 177 | + vi.advanceTimersByTime(60_000); |
| 178 | + expect(evict).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments