Fix SSE keep-alive timer lifecycle in Streamable HTTP server transport (v1.x) (#2547)
This commit is contained in:
parent
bb5a718cbf
commit
e3f3daa12c
1 changed files with 1172 additions and 97 deletions
29
test/server/sseKeepAlive.test.ts
Normal file
29
test/server/sseKeepAlive.test.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { armSseKeepAlive } from '../../src/server/sseKeepAlive.js';
|
||||
|
||||
describe('armSseKeepAlive', () => {
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
it.each([0, -1, 0.5, Number.NaN, Number.POSITIVE_INFINITY])('disables invalid delay %s', delay => {
|
||||
expect(armSseKeepAlive(delay, () => {})).toBeUndefined();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('ticks at the configured interval', async () => {
|
||||
const tick = vi.fn();
|
||||
const timer = armSseKeepAlive(1_000, tick)!;
|
||||
await vi.advanceTimersByTimeAsync(3_000);
|
||||
expect(tick).toHaveBeenCalledTimes(3);
|
||||
clearInterval(timer);
|
||||
});
|
||||
|
||||
it('clamps overflowing delays instead of creating a 1ms timer', async () => {
|
||||
const tick = vi.fn();
|
||||
const timer = armSseKeepAlive(2 ** 31, tick)!;
|
||||
await vi.advanceTimersByTimeAsync(60_000);
|
||||
expect(tick).not.toHaveBeenCalled();
|
||||
clearInterval(timer);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue