Skip to content

test(replay): Add tests for session expiration #7253

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 7, 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
22 changes: 22 additions & 0 deletions packages/integration-tests/suites/replay/sessionExpiry/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: 2000, // this is usually 5min, but we want to test this with shorter times
maxSessionLife: 3600000, // default: 60min
};
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>
98 changes: 98 additions & 0 deletions packages/integration-tests/suites/replay/sessionExpiry/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { expect } from '@playwright/test';

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

// Session should expire after 2s - keep in sync with init.js
const SESSION_TIMEOUT = 2000;

sentryTest('handles an expired session RUN', 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 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');

// We wait for another segment 0
const reqPromise2 = waitForReplayRequest(page, 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 fullSnapshots1 = getFullRecordingSnapshots(req1);
expect(fullSnapshots1.length).toEqual(0);

const incrementalSnapshots1 = getIncrementalRecordingSnapshots(req1);
// The number of incremental snapshots depends on the browser
expect(incrementalSnapshots1.length).toBeGreaterThanOrEqual(4);

expect(incrementalSnapshots1).toEqual(
expect.arrayContaining([
{
source: 1,
positions: [
{
id: 9,
timeOffset: expect.any(Number),
x: expect.any(Number),
y: expect.any(Number),
},
],
},
]),
);
Comment on lines +63 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure what this snapshot element represents - should we add a comment what we're checking here?
Also related but no action required: Any specific reason why we're checking this explicitly over using toMatchSnapshot? (not saying this needs to change)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason was that I didn't want to test the exact x/y values as I figured they may be flaky - and the toMatchSnapshot stuff does string comparison, basically 😬

I'll add a comment - not sure if this actually helps us at all, my thought was to test that something reasonable is sent, but since it is sadly not stable between browsers, it's pretty diluted now 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I also noticed that incremental snapshots are quite browser-specific... In the errors tests, I opted to check for "length of the incr. snapshot > SomeLowerBoundIObservedDuringTestFlakesMinusOne" 😅

RE flakes: My "replay across multiple pages" test snapshot-tests incr. snapshots and so far I haven't noticed flakes (🤞). Perhaps it's still worth a shot? Although, I'm only testing on chromium in this test...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jup, I suspect the reason it works well there is that it's chromium only 😅


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

await new Promise(resolve => setTimeout(resolve, SESSION_TIMEOUT));

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,109 @@
{
"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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"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
}
}
Loading