forked from getsentry/sentry-javascript
/
sessionflusher.test.ts
124 lines (103 loc) · 4.8 KB
/
sessionflusher.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable deprecation/deprecation */
import { Client } from '@sentry/types';
import { SessionFlusher } from '../src';
describe('Session Flusher', () => {
let sendSession: jest.Mock;
let mockClient: Client;
beforeEach(() => {
jest.useFakeTimers();
sendSession = jest.fn(() => Promise.resolve({ status: 'success' }));
mockClient = {
sendSession,
} as unknown as Client;
});
afterEach(() => {
jest.restoreAllMocks();
});
test('test incrementSessionStatusCount updates the internal SessionFlusher state', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.0', environment: 'dev' });
const date = new Date('2021-04-08T12:18:23.043Z');
let count = (flusher as any)._incrementSessionStatusCount('ok', date);
expect(count).toEqual(1);
count = (flusher as any)._incrementSessionStatusCount('ok', date);
expect(count).toEqual(2);
count = (flusher as any)._incrementSessionStatusCount('errored', date);
expect(count).toEqual(1);
date.setMinutes(date.getMinutes() + 1);
count = (flusher as any)._incrementSessionStatusCount('ok', date);
expect(count).toEqual(1);
count = (flusher as any)._incrementSessionStatusCount('errored', date);
expect(count).toEqual(1);
expect(flusher.getSessionAggregates().aggregates).toEqual([
{ errored: 1, exited: 2, started: '2021-04-08T12:18:00.000Z' },
{ errored: 1, exited: 1, started: '2021-04-08T12:19:00.000Z' },
]);
expect(flusher.getSessionAggregates().attrs).toEqual({ release: '1.0.0', environment: 'dev' });
});
test('test undefined attributes are excluded, on incrementSessionStatusCount call', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.0' });
const date = new Date('2021-04-08T12:18:23.043Z');
(flusher as any)._incrementSessionStatusCount('ok', date);
(flusher as any)._incrementSessionStatusCount('errored', date);
expect(flusher.getSessionAggregates()).toEqual({
aggregates: [{ errored: 1, exited: 1, started: '2021-04-08T12:18:00.000Z' }],
attrs: { release: '1.0.0' },
});
});
test('flush is called every 60 seconds after initialisation of an instance of SessionFlusher', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.0', environment: 'dev' });
const flusherFlushFunc = jest.spyOn(flusher, 'flush');
jest.advanceTimersByTime(59000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(2000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(58000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(2000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(2);
});
test('sendSessions is called on flush if sessions were captured', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.0', environment: 'dev' });
const flusherFlushFunc = jest.spyOn(flusher, 'flush');
const date = new Date('2021-04-08T12:18:23.043Z');
(flusher as any)._incrementSessionStatusCount('ok', date);
(flusher as any)._incrementSessionStatusCount('ok', date);
expect(sendSession).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(61000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(1);
expect(sendSession).toHaveBeenCalledWith(
expect.objectContaining({
attrs: { release: '1.0.0', environment: 'dev' },
aggregates: [{ started: '2021-04-08T12:18:00.000Z', exited: 2 }],
}),
);
});
test('sendSessions is not called on flush if no sessions were captured', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.0', environment: 'dev' });
const flusherFlushFunc = jest.spyOn(flusher, 'flush');
expect(sendSession).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(61000);
expect(flusherFlushFunc).toHaveBeenCalledTimes(1);
expect(sendSession).toHaveBeenCalledTimes(0);
});
test('calling close on SessionFlusher should disable SessionFlusher', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.x' });
flusher.close();
expect((flusher as any)._isEnabled).toEqual(false);
});
test('calling close on SessionFlusher will force call flush', () => {
const flusher = new SessionFlusher(mockClient, { release: '1.0.x' });
const flusherFlushFunc = jest.spyOn(flusher, 'flush');
const date = new Date('2021-04-08T12:18:23.043Z');
(flusher as any)._incrementSessionStatusCount('ok', date);
(flusher as any)._incrementSessionStatusCount('ok', date);
flusher.close();
expect(flusherFlushFunc).toHaveBeenCalledTimes(1);
expect(sendSession).toHaveBeenCalledWith(
expect.objectContaining({
attrs: { release: '1.0.x' },
aggregates: [{ started: '2021-04-08T12:18:00.000Z', exited: 2 }],
}),
);
});
});