|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { recordRequestSession, startRequestSession } from '../../../../src/integrations/http/record-request-session'; |
| 4 | +import { Scope } from '../../../../src/scope'; |
| 5 | +import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client'; |
| 6 | + |
| 7 | +function createTestClient(): TestClient { |
| 8 | + return new TestClient(getDefaultTestClientOptions({ release: '1.0.0' })); |
| 9 | +} |
| 10 | + |
| 11 | +describe('startRequestSession()', () => { |
| 12 | + beforeEach(() => { |
| 13 | + vi.useFakeTimers(); |
| 14 | + vi.setSystemTime(new Date('March 19, 1999 06:12:34 UTC')); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + vi.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('counts the session when end() is called', () => { |
| 22 | + const client = createTestClient(); |
| 23 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 24 | + const requestIsolationScope = new Scope(); |
| 25 | + |
| 26 | + const end = startRequestSession(client, { requestIsolationScope }); |
| 27 | + vi.runAllTimers(); |
| 28 | + expect(sendSessionSpy).not.toHaveBeenCalled(); |
| 29 | + |
| 30 | + end(); |
| 31 | + vi.runAllTimers(); |
| 32 | + |
| 33 | + expect(sendSessionSpy).toHaveBeenCalledWith({ |
| 34 | + aggregates: [{ crashed: 0, errored: 0, exited: 1, started: '1999-03-19T06:12:00.000Z' }], |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('counts the status set on the request isolation scope before end()', () => { |
| 39 | + const client = createTestClient(); |
| 40 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 41 | + const requestIsolationScope = new Scope(); |
| 42 | + |
| 43 | + const end = startRequestSession(client, { requestIsolationScope }); |
| 44 | + requestIsolationScope.getScopeData().sdkProcessingMetadata.requestSession!.status = 'crashed'; |
| 45 | + end(); |
| 46 | + vi.runAllTimers(); |
| 47 | + |
| 48 | + expect(sendSessionSpy).toHaveBeenCalledWith({ |
| 49 | + aggregates: [{ crashed: 1, errored: 0, exited: 0, started: '1999-03-19T06:12:00.000Z' }], |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('counts a session once when end() is called more than once', () => { |
| 54 | + const client = createTestClient(); |
| 55 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 56 | + const requestIsolationScope = new Scope(); |
| 57 | + |
| 58 | + const end = startRequestSession(client, { requestIsolationScope }); |
| 59 | + end(); |
| 60 | + end(); |
| 61 | + vi.runAllTimers(); |
| 62 | + |
| 63 | + expect(sendSessionSpy).toHaveBeenCalledTimes(1); |
| 64 | + expect(sendSessionSpy).toHaveBeenCalledWith({ |
| 65 | + aggregates: [{ crashed: 0, errored: 0, exited: 1, started: '1999-03-19T06:12:00.000Z' }], |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('emits the "startRequestSession" hook when the session starts', () => { |
| 70 | + const client = createTestClient(); |
| 71 | + const onStartRequestSession = vi.fn(); |
| 72 | + client.on('startRequestSession', onStartRequestSession); |
| 73 | + |
| 74 | + startRequestSession(client, { requestIsolationScope: new Scope() }); |
| 75 | + |
| 76 | + expect(onStartRequestSession).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('uses sessionFlushingDelayMS to schedule the aggregate', () => { |
| 80 | + const client = createTestClient(); |
| 81 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 82 | + |
| 83 | + startRequestSession(client, { requestIsolationScope: new Scope(), sessionFlushingDelayMS: 1_000 })(); |
| 84 | + |
| 85 | + vi.advanceTimersByTime(999); |
| 86 | + expect(sendSessionSpy).not.toHaveBeenCalled(); |
| 87 | + vi.advanceTimersByTime(1); |
| 88 | + expect(sendSessionSpy).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + it('sends the aggregate after 60 seconds by default', () => { |
| 91 | + const client = createTestClient(); |
| 92 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 93 | + |
| 94 | + startRequestSession(client, { requestIsolationScope: new Scope() })(); |
| 95 | + |
| 96 | + vi.advanceTimersByTime(59_999); |
| 97 | + expect(sendSessionSpy).not.toHaveBeenCalled(); |
| 98 | + vi.advanceTimersByTime(1); |
| 99 | + expect(sendSessionSpy).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('recordRequestSession()', () => { |
| 104 | + beforeEach(() => { |
| 105 | + vi.useFakeTimers(); |
| 106 | + vi.setSystemTime(new Date('March 19, 1999 06:12:34 UTC')); |
| 107 | + }); |
| 108 | + |
| 109 | + afterEach(() => { |
| 110 | + vi.useRealTimers(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('counts the session when the response emits close', () => { |
| 114 | + const client = createTestClient(); |
| 115 | + const sendSessionSpy = vi.spyOn(client, 'sendSession'); |
| 116 | + const response = Object.assign(new EventEmitter(), { statusCode: 200, headers: {} }); |
| 117 | + |
| 118 | + recordRequestSession(client, { requestIsolationScope: new Scope(), response }); |
| 119 | + vi.runAllTimers(); |
| 120 | + expect(sendSessionSpy).not.toHaveBeenCalled(); |
| 121 | + |
| 122 | + response.emit('close'); |
| 123 | + vi.runAllTimers(); |
| 124 | + |
| 125 | + expect(sendSessionSpy).toHaveBeenCalledWith({ |
| 126 | + aggregates: [{ crashed: 0, errored: 0, exited: 1, started: '1999-03-19T06:12:00.000Z' }], |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments