Skip to content

test(replay): Add test for session max age handling #7362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ sentryTest('should stop recording after receiving an error response', async ({ g

const replay = await getReplaySnapshot(page);

// @ts-ignore private API
expect(replay._isEnabled).toBe(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ sentryTest('handles an inactive session', async ({ getLocalTestPath, page }) =>

// nothing happened because no activity/inactivity was detected
const replay = await getReplaySnapshot(page);
// @ts-ignore private api
expect(replay._isEnabled).toEqual(true);
// @ts-ignore private api
expect(replay._isPaused).toEqual(false);

// Now we trigger a blur event, which should move the session to paused mode
Expand All @@ -63,19 +61,15 @@ sentryTest('handles an inactive session', async ({ getLocalTestPath, page }) =>
});

const replay2 = await getReplaySnapshot(page);
// @ts-ignore private api
expect(replay2._isEnabled).toEqual(true);
// @ts-ignore private api
expect(replay2._isPaused).toEqual(true);

// Trigger an action, should re-start the recording
await page.click('#button2');
const req1 = await reqPromise1;

const replay3 = await getReplaySnapshot(page);
// @ts-ignore private api
expect(replay3._isEnabled).toEqual(true);
// @ts-ignore private api
expect(replay3._isPaused).toEqual(false);

const replayEvent1 = getReplayEvent(req1);
Expand Down
22 changes: 22 additions & 0 deletions packages/integration-tests/suites/replay/sessionMaxAge/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window.Replay = new Sentry.Replay({
flushMinDelay: 500,
flushMaxDelay: 500,
});

Sentry.init({
dsn: 'https://[email protected]/1337',
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [window.Replay],
});

window.Replay._replay.timeouts = {
sessionIdle: 300000, // default: 5min
maxSessionLife: 4000, // this is usually 60min, but we want to test this with shorter times
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<button onclick="console.log('Test log 1')" id="button1">Click me</button>
<button onclick="console.log('Test log 2')" id="button2">Click me</button>
</body>
</html>
89 changes: 89 additions & 0 deletions packages/integration-tests/suites/replay/sessionMaxAge/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../utils/fixtures';
import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates';
import {
getFullRecordingSnapshots,
getReplayEvent,
getReplaySnapshot,
normalize,
shouldSkipReplayTest,
waitForReplayRequest,
} from '../../../utils/replayHelpers';

// Session should be max. 4s long
const SESSION_MAX_AGE = 4000;

/*
The main difference between this and sessionExpiry test, is that here we wait for the overall time (4s)
in multiple steps (2s, 2s) instead of waiting for the whole time at once (4s).
*/
sentryTest('handles session that exceeds max age', async ({ getLocalTestPath, page }) => {
if (shouldSkipReplayTest()) {
sentryTest.skip();
}

const reqPromise0 = waitForReplayRequest(page, 0);
const reqPromise1 = waitForReplayRequest(page, 1);

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestPath({ testDir: __dirname });

await page.goto(url);

const replay0 = await getReplaySnapshot(page);
// We use the `initialTimestamp` of the replay to do any time based calculations
const startTimestamp = replay0._context.initialTimestamp;

const req0 = await reqPromise0;

const replayEvent0 = getReplayEvent(req0);
expect(replayEvent0).toEqual(getExpectedReplayEvent({}));

const fullSnapshots0 = getFullRecordingSnapshots(req0);
expect(fullSnapshots0.length).toEqual(1);
const stringifiedSnapshot = normalize(fullSnapshots0[0]);
expect(stringifiedSnapshot).toMatchSnapshot('snapshot-0.json');

// Wait again for a new segment 0 (=new session)
const reqPromise2 = waitForReplayRequest(page, 0);

// Wait for an incremental snapshot
// Wait half of the session max age (after initial flush), but account for potentially slow runners
const timePassed1 = Date.now() - startTimestamp;
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE / 2 - timePassed1, 0)));
await page.click('#button1');

const req1 = await reqPromise1;
const replayEvent1 = getReplayEvent(req1);

expect(replayEvent1).toEqual(getExpectedReplayEvent({ replay_start_timestamp: undefined, segment_id: 1, urls: [] }));

const replay1 = await getReplaySnapshot(page);
const oldSessionId = replay1.session?.id;

// Wait for session to expire
const timePassed2 = Date.now() - startTimestamp;
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE - timePassed2, 0)));
await page.click('#button2');

const req2 = await reqPromise2;
const replay2 = await getReplaySnapshot(page);

expect(replay2.session?.id).not.toEqual(oldSessionId);

const replayEvent2 = getReplayEvent(req2);
expect(replayEvent2).toEqual(getExpectedReplayEvent({}));

const fullSnapshots2 = getFullRecordingSnapshots(req2);
expect(fullSnapshots2.length).toEqual(1);
const stringifiedSnapshot2 = normalize(fullSnapshots2[0]);
expect(stringifiedSnapshot2).toMatchSnapshot('snapshot-2.json');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"type": 2,
"data": {
"node": {
"type": 0,
"childNodes": [
{
"type": 1,
"name": "html",
"publicId": "",
"systemId": "",
"id": 2
},
{
"type": 2,
"tagName": "html",
"attributes": {},
"childNodes": [
{
"type": 2,
"tagName": "head",
"attributes": {},
"childNodes": [
{
"type": 2,
"tagName": "meta",
"attributes": {
"charset": "utf-8"
},
"childNodes": [],
"id": 5
}
],
"id": 4
},
{
"type": 3,
"textContent": "\n ",
"id": 6
},
{
"type": 2,
"tagName": "body",
"attributes": {},
"childNodes": [
{
"type": 3,
"textContent": "\n ",
"id": 8
},
{
"type": 2,
"tagName": "button",
"attributes": {
"onclick": "console.log('Test log 1')",
"id": "button1"
},
"childNodes": [
{
"type": 3,
"textContent": "***** **",
"id": 10
}
],
"id": 9
},
{
"type": 3,
"textContent": "\n ",
"id": 11
},
{
"type": 2,
"tagName": "button",
"attributes": {
"onclick": "console.log('Test log 2')",
"id": "button2"
},
"childNodes": [
{
"type": 3,
"textContent": "***** **",
"id": 13
}
],
"id": 12
},
{
"type": 3,
"textContent": "\n ",
"id": 14
},
{
"type": 3,
"textContent": "\n\n",
"id": 15
}
],
"id": 7
}
],
"id": 3
}
],
"id": 1
},
"initialOffset": {
"left": 0,
"top": 0
}
},
"timestamp": [timestamp]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"type": 2,
"data": {
"node": {
"type": 0,
"childNodes": [
{
"type": 1,
"name": "html",
"publicId": "",
"systemId": "",
"id": 2
},
{
"type": 2,
"tagName": "html",
"attributes": {},
"childNodes": [
{
"type": 2,
"tagName": "head",
"attributes": {},
"childNodes": [
{
"type": 2,
"tagName": "meta",
"attributes": {
"charset": "utf-8"
},
"childNodes": [],
"id": 5
}
],
"id": 4
},
{
"type": 3,
"textContent": "\n ",
"id": 6
},
{
"type": 2,
"tagName": "body",
"attributes": {},
"childNodes": [
{
"type": 3,
"textContent": "\n ",
"id": 8
},
{
"type": 2,
"tagName": "button",
"attributes": {
"onclick": "console.log('Test log 1')",
"id": "button1"
},
"childNodes": [
{
"type": 3,
"textContent": "***** **",
"id": 10
}
],
"id": 9
},
{
"type": 3,
"textContent": "\n ",
"id": 11
},
{
"type": 2,
"tagName": "button",
"attributes": {
"onclick": "console.log('Test log 2')",
"id": "button2"
},
"childNodes": [
{
"type": 3,
"textContent": "***** **",
"id": 13
}
],
"id": 12
},
{
"type": 3,
"textContent": "\n ",
"id": 14
},
{
"type": 3,
"textContent": "\n\n",
"id": 15
}
],
"id": 7
}
],
"id": 3
}
],
"id": 1
},
"initialOffset": {
"left": 0,
"top": 0
}
},
"timestamp": [timestamp]
}
Loading