Skip to content

Commit 8deba63

Browse files
committed
ref(replay): Extract flush min and max delays as constants
1 parent 816309b commit 8deba63

File tree

6 files changed

+50
-38
lines changed

6 files changed

+50
-38
lines changed

packages/replay/src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ export const DEFAULT_ERROR_SAMPLE_RATE = 1.0;
2828

2929
/** The select to use for the `maskAllText` option */
3030
export const MASK_ALL_TEXT_SELECTOR = 'body *:not(style,script)';
31+
32+
/** Default flush delays */
33+
export const DEFAULT_FLUSH_MIN_DELAY = 5_000;
34+
export const DEFAULT_FLUSH_MAX_DELAY = 15_000;
35+
export const INITIAL_FLUSH_DELAY = 5_000;

packages/replay/src/integration.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { getCurrentHub } from '@sentry/core';
22
import type { BrowserClientReplayOptions } from '@sentry/types';
33
import { Integration } from '@sentry/types';
44

5-
import { DEFAULT_ERROR_SAMPLE_RATE, DEFAULT_SESSION_SAMPLE_RATE, MASK_ALL_TEXT_SELECTOR } from './constants';
5+
import {
6+
DEFAULT_ERROR_SAMPLE_RATE,
7+
DEFAULT_FLUSH_MAX_DELAY,
8+
DEFAULT_FLUSH_MIN_DELAY,
9+
DEFAULT_SESSION_SAMPLE_RATE,
10+
MASK_ALL_TEXT_SELECTOR,
11+
} from './constants';
612
import { ReplayContainer } from './replay';
713
import type { RecordingOptions, ReplayConfiguration, ReplayPluginOptions } from './types';
814
import { isBrowser } from './util/isBrowser';
@@ -40,9 +46,9 @@ export class Replay implements Integration {
4046
private _replay?: ReplayContainer;
4147

4248
constructor({
43-
flushMinDelay = 5000,
44-
flushMaxDelay = 15000,
45-
initialFlushDelay = 5000,
49+
flushMinDelay = DEFAULT_FLUSH_MIN_DELAY,
50+
flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY,
51+
initialFlushDelay = INITIAL_FLUSH_DELAY,
4652
stickySession = true,
4753
useCompression = true,
4854
sessionSampleRate,

packages/replay/test/unit/flush.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryUtils from '@sentry/utils';
22

3-
import { SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
3+
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
44
import * as AddMemoryEntry from '../../src/util/addMemoryEntry';
55
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
66
import { createPerformanceEntries } from './../../src/createPerformanceEntry';
@@ -145,7 +145,7 @@ it('long first flush enqueues following events', async () => {
145145
domHandler({
146146
name: 'click',
147147
});
148-
await advanceTimers(5000);
148+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
149149
// flush #2 @ t=5s - due to click
150150
expect(replay.flush).toHaveBeenCalledTimes(2);
151151

@@ -212,7 +212,7 @@ it('long first flush enqueues following events', async () => {
212212
});
213213
// flush #5 @ t=25s - debounced flush calls `flush`
214214
// 20s + `flushMinDelay` which is 5 seconds
215-
await advanceTimers(5000);
215+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
216216
expect(replay.flush).toHaveBeenCalledTimes(5);
217217
expect(replay.runFlush).toHaveBeenCalledTimes(2);
218218
expect(mockSendReplay).toHaveBeenLastCalledWith({

packages/replay/test/unit/index-errorSampleRate.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { captureException } from '@sentry/core';
22

3-
import { REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
3+
import { DEFAULT_FLUSH_MIN_DELAY, REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
44
import { addEvent } from '../../src/util/addEvent';
55
import { ReplayContainer } from './../../src/replay';
66
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
@@ -54,7 +54,7 @@ describe('Replay (errorSampleRate)', () => {
5454
expect(replay).not.toHaveLastSentReplay();
5555

5656
captureException(new Error('testing'));
57-
jest.advanceTimersByTime(5000);
57+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
5858
await new Promise(process.nextTick);
5959

6060
expect(replay).toHaveSentReplay({
@@ -99,15 +99,15 @@ describe('Replay (errorSampleRate)', () => {
9999
events: JSON.stringify([{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP + 5020, type: 2 }]),
100100
});
101101

102-
jest.advanceTimersByTime(5000);
102+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
103103

104104
// New checkout when we call `startRecording` again after uploading segment
105105
// after an error occurs
106106
expect(replay).toHaveLastSentReplay({
107107
events: JSON.stringify([
108108
{
109109
data: { isCheckout: true },
110-
timestamp: BASE_TIMESTAMP + 5000 + 20,
110+
timestamp: BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY + 20,
111111
type: 2,
112112
},
113113
]),
@@ -118,7 +118,7 @@ describe('Replay (errorSampleRate)', () => {
118118
name: 'click',
119119
});
120120

121-
jest.advanceTimersByTime(5000);
121+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
122122
await new Promise(process.nextTick);
123123

124124
expect(replay).toHaveLastSentReplay({
@@ -245,12 +245,12 @@ describe('Replay (errorSampleRate)', () => {
245245
expect(replay).not.toHaveLastSentReplay();
246246

247247
// There should also not be another attempt at an upload 5 seconds after the last replay event
248-
await advanceTimers(5000);
248+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
249249
expect(replay).not.toHaveLastSentReplay();
250250

251251
// Let's make sure it continues to work
252252
mockRecord._emitter(TEST_EVENT);
253-
await advanceTimers(5000);
253+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
254254
jest.runAllTimers();
255255
await new Promise(process.nextTick);
256256
expect(replay).not.toHaveLastSentReplay();
@@ -294,11 +294,11 @@ describe('Replay (errorSampleRate)', () => {
294294
jest.runAllTimers();
295295
await new Promise(process.nextTick);
296296

297-
jest.advanceTimersByTime(5000);
297+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
298298

299299
captureException(new Error('testing'));
300300

301-
jest.advanceTimersByTime(5000);
301+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
302302
await new Promise(process.nextTick);
303303

304304
expect(replay).toHaveSentReplay({
@@ -309,7 +309,7 @@ describe('Replay (errorSampleRate)', () => {
309309
// (advance timers + waiting for flush after the checkout) and
310310
// extra time is likely due to async of `addMemoryEntry()`
311311

312-
timestamp: (BASE_TIMESTAMP + 5000 + 5000 + 20) / 1000,
312+
timestamp: (BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY + DEFAULT_FLUSH_MIN_DELAY + 20) / 1000,
313313
error_ids: [expect.any(String)],
314314
trace_ids: [],
315315
urls: ['http://localhost/'],
@@ -400,7 +400,7 @@ it('sends a replay after loading the session multiple times', async () => {
400400

401401
captureException(new Error('testing'));
402402

403-
jest.advanceTimersByTime(5000);
403+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
404404
await new Promise(process.nextTick);
405405

406406
expect(replay).toHaveSentReplay({

packages/replay/test/unit/index-noSticky.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getCurrentHub } from '@sentry/core';
22
import { Transport } from '@sentry/types';
33
import * as SentryUtils from '@sentry/utils';
44

5-
import { SESSION_IDLE_DURATION, VISIBILITY_CHANGE_TIMEOUT } from '../../src/constants';
5+
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, VISIBILITY_CHANGE_TIMEOUT } from '../../src/constants';
66
import { addEvent } from '../../src/util/addEvent';
77
import { ReplayContainer } from './../../src/replay';
88
import { BASE_TIMESTAMP, mockRrweb, mockSdk } from './../index';
@@ -197,7 +197,7 @@ describe('Replay (no sticky)', () => {
197197

198198
// There should also not be another attempt at an upload 5 seconds after the last replay event
199199
mockTransport.mockClear();
200-
await advanceTimers(5000);
200+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
201201
expect(replay).not.toHaveLastSentReplay();
202202

203203
expect(replay.session?.lastActivity).toBe(BASE_TIMESTAMP);
@@ -208,7 +208,7 @@ describe('Replay (no sticky)', () => {
208208
// Let's make sure it continues to work
209209
mockTransport.mockClear();
210210
mockRecord._emitter(TEST_EVENT);
211-
await advanceTimers(5000);
211+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
212212
expect(replay).toHaveLastSentReplay({ events: JSON.stringify([TEST_EVENT]) });
213213
});
214214

@@ -252,7 +252,7 @@ describe('Replay (no sticky)', () => {
252252
name: 'click',
253253
});
254254

255-
await advanceTimers(5000);
255+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
256256

257257
const newTimestamp = BASE_TIMESTAMP + FIFTEEN_MINUTES;
258258
const breadcrumbTimestamp = newTimestamp + 20; // I don't know where this 20ms comes from

packages/replay/test/unit/index.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getCurrentHub } from '@sentry/core';
22
import { EventType } from 'rrweb';
33

44
import {
5+
DEFAULT_FLUSH_MIN_DELAY,
56
MASK_ALL_TEXT_SELECTOR,
67
MAX_SESSION_LIFE,
78
REPLAY_SESSION_KEY,
@@ -335,7 +336,7 @@ describe('Replay', () => {
335336

336337
// There should also not be another attempt at an upload 5 seconds after the last replay event
337338
mockTransportSend.mockClear();
338-
await advanceTimers(5000);
339+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
339340

340341
expect(replay).not.toHaveLastSentReplay();
341342

@@ -347,7 +348,7 @@ describe('Replay', () => {
347348
// Let's make sure it continues to work
348349
mockTransportSend.mockClear();
349350
mockRecord._emitter(TEST_EVENT);
350-
await advanceTimers(5000);
351+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
351352
expect(replay).toHaveLastSentReplay({ events: JSON.stringify([TEST_EVENT]) });
352353
});
353354

@@ -401,7 +402,7 @@ describe('Replay', () => {
401402
name: 'click',
402403
});
403404

404-
await advanceTimers(5000);
405+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
405406

406407
const newTimestamp = BASE_TIMESTAMP + FIFTEEN_MINUTES;
407408
const breadcrumbTimestamp = newTimestamp + 20; // I don't know where this 20ms comes from
@@ -478,7 +479,7 @@ describe('Replay', () => {
478479
});
479480

480481
WINDOW.dispatchEvent(new Event('blur'));
481-
await advanceTimers(5000);
482+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
482483

483484
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
484485
expect(replay).not.toHaveLastSentReplay();
@@ -497,7 +498,7 @@ describe('Replay', () => {
497498

498499
const NEW_TEST_EVENT = {
499500
data: { name: 'test' },
500-
timestamp: BASE_TIMESTAMP + MAX_SESSION_LIFE + 5000 + 20,
501+
timestamp: BASE_TIMESTAMP + MAX_SESSION_LIFE + DEFAULT_FLUSH_MIN_DELAY + 20,
501502
type: 3,
502503
};
503504

@@ -508,9 +509,9 @@ describe('Replay', () => {
508509
await new Promise(process.nextTick);
509510

510511
expect(replay).not.toHaveSameSession(initialSession);
511-
await advanceTimers(5000);
512+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
512513

513-
const newTimestamp = BASE_TIMESTAMP + MAX_SESSION_LIFE + 5000 + 20; // I don't know where this 20ms comes from
514+
const newTimestamp = BASE_TIMESTAMP + MAX_SESSION_LIFE + DEFAULT_FLUSH_MIN_DELAY + 20; // I don't know where this 20ms comes from
514515
const breadcrumbTimestamp = newTimestamp;
515516

516517
jest.runAllTimers();
@@ -590,13 +591,13 @@ describe('Replay', () => {
590591
throw new Error('Something bad happened');
591592
});
592593
mockRecord._emitter(TEST_EVENT);
593-
await advanceTimers(5000);
594+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
594595

595596
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
596597
mockTransportSend.mockImplementationOnce(() => {
597598
throw new Error('Something bad happened');
598599
});
599-
await advanceTimers(5000);
600+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
600601

601602
// next tick should retry and succeed
602603
mockConsole.mockRestore();
@@ -624,7 +625,7 @@ describe('Replay', () => {
624625
expect(replay.session?.segmentId).toBe(1);
625626

626627
// next tick should do nothing
627-
await advanceTimers(5000);
628+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
628629
expect(replay).not.toHaveLastSentReplay();
629630
});
630631

@@ -647,12 +648,12 @@ describe('Replay', () => {
647648
});
648649
mockRecord._emitter(TEST_EVENT);
649650

650-
await advanceTimers(5000);
651+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
651652

652653
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
653654
expect(replay.sendReplayRequest).toHaveBeenCalledTimes(1);
654655

655-
await advanceTimers(5000);
656+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
656657
expect(replay.sendReplayRequest).toHaveBeenCalledTimes(2);
657658

658659
await advanceTimers(10000);
@@ -758,7 +759,7 @@ describe('Replay', () => {
758759
});
759760
mockRecord._emitter(TEST_EVENT);
760761

761-
await advanceTimers(5000);
762+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
762763

763764
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
764765

@@ -770,7 +771,7 @@ describe('Replay', () => {
770771
mockSendReplayRequest.mockImplementationOnce(() => {
771772
throw new Error('Something bad happened');
772773
});
773-
await advanceTimers(5000);
774+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
774775
expect(replay.sendReplayRequest).toHaveBeenCalledTimes(2);
775776

776777
// next tick should retry and fail
@@ -912,11 +913,11 @@ describe('Replay', () => {
912913
const TEST_EVENT = { data: {}, timestamp: BASE_TIMESTAMP, type: 2 };
913914
mockRecord._emitter(TEST_EVENT);
914915

915-
await advanceTimers(5000);
916+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
916917
expect(replay.flush).toHaveBeenCalledTimes(1);
917918

918919
// Make sure there's nothing queued up after
919-
await advanceTimers(5000);
920+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
920921
expect(replay.flush).toHaveBeenCalledTimes(1);
921922
});
922923
});

0 commit comments

Comments
 (0)