Skip to content

Commit 470cc36

Browse files
committed
fixes & tests
1 parent fdcd72b commit 470cc36

File tree

5 files changed

+21
-101
lines changed

5 files changed

+21
-101
lines changed

packages/replay/src/util/addEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ export function addEventSync(replay: ReplayContainer, event: RecordingEvent, isC
3535
*
3636
* `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
3737
*/
38-
export async function addEvent(
38+
export function addEvent(
3939
replay: ReplayContainer,
4040
event: RecordingEvent,
4141
isCheckout?: boolean,
4242
): Promise<AddEventResult | null> {
4343
if (!shouldAddEvent(replay, event)) {
44-
return null;
44+
return Promise.resolve(null);
4545
}
4646

4747
return _addEvent(replay, event, isCheckout);
@@ -90,7 +90,6 @@ async function _addEvent(
9090

9191
function shouldAddEvent(replay: ReplayContainer, event: RecordingEvent): boolean {
9292
if (!replay.eventBuffer || replay.isPaused() || !replay.isEnabled()) {
93-
// This implies that `_isEnabled` is false
9493
return false;
9594
}
9695

packages/replay/src/util/handleRecordingEmit.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { EventType } from '@sentry-internal/rrweb';
22
import { logger } from '@sentry/utils';
33

44
import { saveSession } from '../session/saveSession';
5-
import type { AddEventResult, RecordingEvent, ReplayContainer, ReplayOptionFrameEvent } from '../types';
6-
import { addEvent, addEventSync } from './addEvent';
5+
import type { RecordingEvent, ReplayContainer, ReplayOptionFrameEvent } from '../types';
6+
import { addEventSync } from './addEvent';
77
import { logInfo } from './log';
88

99
type RecordingEmitCallback = (event: RecordingEvent, isCheckout?: boolean) => void;
@@ -59,7 +59,7 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
5959
//
6060
// `isCheckout` is always true, but want to be explicit that it should
6161
// only be added for checkouts
62-
void addSettingsEvent(replay, isCheckout);
62+
addSettingsEvent(replay, isCheckout);
6363

6464
// If there is a previousSessionId after a full snapshot occurs, then
6565
// the replay session was started due to session expiration. The new session
@@ -133,11 +133,11 @@ export function createOptionsEvent(replay: ReplayContainer): ReplayOptionFrameEv
133133
* Add a "meta" event that contains a simplified view on current configuration
134134
* options. This should only be included on the first segment of a recording.
135135
*/
136-
function addSettingsEvent(replay: ReplayContainer, isCheckout?: boolean): Promise<AddEventResult | null> {
136+
function addSettingsEvent(replay: ReplayContainer, isCheckout?: boolean): void {
137137
// Only need to add this event when sending the first segment
138138
if (!isCheckout || !replay.session || replay.session.segmentId !== 0) {
139-
return Promise.resolve(null);
139+
return;
140140
}
141141

142-
return addEvent(replay, createOptionsEvent(replay), false);
142+
addEventSync(replay, createOptionsEvent(replay), false);
143143
}

packages/replay/test/integration/errorSampleRate.test.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -816,73 +816,6 @@ describe('Integration | errorSampleRate', () => {
816816
expect(replay).not.toHaveLastSentReplay();
817817
});
818818

819-
it('does not stop replay based on earliest event in buffer', async () => {
820-
jest.setSystemTime(BASE_TIMESTAMP);
821-
822-
const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP - 60000 });
823-
mockRecord._emitter(TEST_EVENT);
824-
825-
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
826-
expect(replay).not.toHaveLastSentReplay();
827-
828-
jest.runAllTimers();
829-
await new Promise(process.nextTick);
830-
831-
expect(replay).not.toHaveLastSentReplay();
832-
captureException(new Error('testing'));
833-
834-
await waitForBufferFlush();
835-
836-
expect(replay).toHaveLastSentReplay();
837-
838-
// Flush from calling `stopRecording`
839-
await waitForFlush();
840-
841-
// Now wait after session expires - should stop recording
842-
mockRecord.takeFullSnapshot.mockClear();
843-
(getCurrentHub().getClient()!.getTransport()!.send as unknown as jest.SpyInstance<any>).mockClear();
844-
845-
expect(replay).not.toHaveLastSentReplay();
846-
847-
const TICKS = 80;
848-
849-
// We advance time so that we are on the border of expiring, taking into
850-
// account that TEST_EVENT timestamp is 60000 ms before BASE_TIMESTAMP. The
851-
// 3 DEFAULT_FLUSH_MIN_DELAY is to account for the `waitForFlush` that has
852-
// happened, and for the next two that will happen. The first following
853-
// `waitForFlush` does not expire session, but the following one will.
854-
jest.advanceTimersByTime(SESSION_IDLE_EXPIRE_DURATION - 60000 - 3 * DEFAULT_FLUSH_MIN_DELAY - TICKS);
855-
await new Promise(process.nextTick);
856-
857-
mockRecord._emitter(TEST_EVENT);
858-
expect(replay).not.toHaveLastSentReplay();
859-
await waitForFlush();
860-
861-
expect(replay).not.toHaveLastSentReplay();
862-
expect(mockRecord.takeFullSnapshot).toHaveBeenCalledTimes(0);
863-
expect(replay.isEnabled()).toBe(true);
864-
865-
mockRecord._emitter(TEST_EVENT);
866-
expect(replay).not.toHaveLastSentReplay();
867-
await waitForFlush();
868-
869-
expect(replay).not.toHaveLastSentReplay();
870-
expect(mockRecord.takeFullSnapshot).toHaveBeenCalledTimes(0);
871-
expect(replay.isEnabled()).toBe(true);
872-
873-
// It's hard to test, but if we advance the below time less 1 ms, it should
874-
// be enabled, but we can't trigger a session check via flush without
875-
// incurring another DEFAULT_FLUSH_MIN_DELAY timeout.
876-
jest.advanceTimersByTime(60000 - DEFAULT_FLUSH_MIN_DELAY);
877-
mockRecord._emitter(TEST_EVENT);
878-
expect(replay).not.toHaveLastSentReplay();
879-
await waitForFlush();
880-
881-
expect(replay).not.toHaveLastSentReplay();
882-
expect(mockRecord.takeFullSnapshot).toHaveBeenCalledTimes(0);
883-
expect(replay.isEnabled()).toBe(false);
884-
});
885-
886819
it('handles very long active buffer session', async () => {
887820
const stepDuration = 10_000;
888821
const steps = 5_000;

packages/replay/test/integration/flush.test.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ describe('Integration | flush', () => {
403403
it('logs warning if adding event that is after maxReplayDuration', async () => {
404404
replay.getOptions()._experiments.traceInternals = true;
405405

406+
const spyLogger = jest.spyOn(SentryUtils.logger, 'info');
407+
406408
sessionStorage.clear();
407409
clearSession(replay);
408410
replay['_initializeSessionForSampling']();
@@ -422,32 +424,18 @@ describe('Integration | flush', () => {
422424
// no checkout!
423425
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
424426

425-
expect(mockFlush).toHaveBeenCalledTimes(1);
426-
expect(mockSendReplay).toHaveBeenCalledTimes(1);
427-
428-
const replayData = mockSendReplay.mock.calls[0][0];
427+
// No flush is scheduled is aborted because event is after maxReplayDuration
428+
expect(mockFlush).toHaveBeenCalledTimes(0);
429+
expect(mockSendReplay).toHaveBeenCalledTimes(0);
429430

430-
expect(JSON.parse(replayData.recordingData)).toEqual([
431-
{
432-
type: 5,
433-
timestamp: BASE_TIMESTAMP,
434-
data: {
435-
tag: 'breadcrumb',
436-
payload: {
437-
timestamp: BASE_TIMESTAMP / 1000,
438-
type: 'default',
439-
category: 'console',
440-
data: { logger: 'replay' },
441-
level: 'info',
442-
message: `[Replay] Skipping event with timestamp ${
443-
BASE_TIMESTAMP + MAX_REPLAY_DURATION + 100
444-
} because it is after maxReplayDuration`,
445-
},
446-
},
447-
},
448-
]);
431+
expect(spyLogger).toHaveBeenLastCalledWith(
432+
`[Replay] Skipping event with timestamp ${
433+
BASE_TIMESTAMP + MAX_REPLAY_DURATION + 100
434+
} because it is after maxReplayDuration`,
435+
);
449436

450437
replay.getOptions()._experiments.traceInternals = false;
438+
spyLogger.mockRestore();
451439
});
452440

453441
/**

packages/replay/test/unit/util/handleRecordingEmit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('Unit | util | handleRecordingEmit', () => {
1616

1717
beforeEach(function () {
1818
jest.setSystemTime(BASE_TIMESTAMP);
19-
addEventMock = jest.spyOn(SentryAddEvent, 'addEvent').mockImplementation(async () => {
20-
// Do nothing
19+
addEventMock = jest.spyOn(SentryAddEvent, 'addEventSync').mockImplementation(() => {
20+
return true;
2121
});
2222
});
2323

0 commit comments

Comments
 (0)