/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/tests/server/httpSubscriptionLink.memory.test.ts
69 строк
2 KB
Alex / KATT
chore: test refactor (#6958)
24 сен 2025, 08:03
Не верифицирован
24 сен 2025, 08:03
466a924
Код
Авторство
О чём код?
import { EventEmitter, on } from 'node:events'; import { testServerAndClientResource } from '@trpc/client/__tests__/testClientResource'; import { httpSubscriptionLink } from '@trpc/client'; import { initTRPC } from '@trpc/server'; import { sleep } from '@trpc/server/unstable-core-do-not-import'; describe('http subscription memory', () => { it('should free data after each iteration (#6156)', async () => { const ee = new EventEmitter(); const t = initTRPC.create({ sse: { maxDurationMs: 1000, ping: { enabled: true, intervalMs: 200 }, }, }); const router = t.router({ onData: t.procedure.subscription(({ signal }) => on(ee as any, 'data', { signal }), ), }); await using ctx = testServerAndClientResource(router, { server: {}, client(opts) { return { links: [httpSubscriptionLink({ url: opts.httpUrl })], }; }, }); const onStartedMock = vi.fn(); const onDataLengthMock = vi.fn(); const subscription = ctx.client.onData.subscribe(undefined, { onStarted() { onStartedMock(); }, onData(data) { expect(Array.isArray(data)).toBe(true); onDataLengthMock(data.length); }, }); await vi.waitFor(() => { expect(onStartedMock).toHaveBeenCalled(); }); const refs = [emitData(), emitData()]; await vi.waitFor(() => { expect(onDataLengthMock).toHaveBeenCalledTimes(2); }); await sleep(0); global.gc!(); expect(refs[0]!.deref()).toBeUndefined(); expect(refs[1]!.deref()).toBeUndefined(); subscription.unsubscribe(); function emitData(): WeakRef<never[]> { const data: never[] = []; const ref = new WeakRef(data); ee.emit('data', data); return ref; } }); });