Skip to content

Commit ddc09a7

Browse files
committed
test(replay): Add test for session max age handling
1 parent 9bf840a commit ddc09a7

File tree

12 files changed

+999
-1
lines changed

12 files changed

+999
-1
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ jobs:
481481
needs: [job_get_metadata, job_build]
482482
if: needs.job_get_metadata.outputs.changed_browser_integration == 'true' || github.event_name != 'pull_request'
483483
runs-on: ubuntu-20.04
484-
timeout-minutes: 15
484+
timeout-minutes: 40
485485
strategy:
486486
fail-fast: false
487487
matrix:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
window.Replay = new Sentry.Replay({
5+
flushMinDelay: 500,
6+
flushMaxDelay: 500,
7+
});
8+
9+
Sentry.init({
10+
dsn: 'https://[email protected]/1337',
11+
sampleRate: 0,
12+
replaysSessionSampleRate: 1.0,
13+
replaysOnErrorSampleRate: 0.0,
14+
debug: true,
15+
16+
integrations: [window.Replay],
17+
});
18+
19+
window.Replay._replay.timeouts = {
20+
sessionIdle: 300000, // default: 5min
21+
maxSessionLife: 4000, // this is usually 60min, but we want to test this with shorter times
22+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button onclick="console.log('Test log 1')" id="button1">Click me</button>
8+
<button onclick="console.log('Test log 2')" id="button2">Click me</button>
9+
</body>
10+
</html>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../utils/fixtures';
4+
import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates';
5+
import {
6+
getFullRecordingSnapshots,
7+
getReplayEvent,
8+
getReplaySnapshot,
9+
normalize,
10+
shouldSkipReplayTest,
11+
waitForReplayRequest,
12+
} from '../../../utils/replayHelpers';
13+
14+
// Session should be max. 4s long
15+
const SESSION_MAX_AGE = 4000;
16+
17+
/*
18+
The main difference between this and sessionExpiry test, is that here we wait for the overall time (4s)
19+
in multiple steps (2s, 2s) instead of waiting for the whole time at once (4s).
20+
*/
21+
for (let i = 0; i < 100; i++) {
22+
sentryTest(`handles session that exceeds max age RUN ${i}`, async ({ getLocalTestPath, page }) => {
23+
if (shouldSkipReplayTest()) {
24+
sentryTest.skip();
25+
}
26+
27+
const reqPromise0 = waitForReplayRequest(page, 0);
28+
const reqPromise1 = waitForReplayRequest(page, 1);
29+
30+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
31+
return route.fulfill({
32+
status: 200,
33+
contentType: 'application/json',
34+
body: JSON.stringify({ id: 'test-id' }),
35+
});
36+
});
37+
38+
const url = await getLocalTestPath({ testDir: __dirname });
39+
40+
await page.goto(url);
41+
42+
const replay0 = await getReplaySnapshot(page);
43+
// We use the `initialTimestamp` of the replay to do any time based calculations
44+
// @ts-ignore this is fine
45+
const startTimestamp = replay0._context.initialTimestamp;
46+
47+
const req0 = await reqPromise0;
48+
49+
const replayEvent0 = getReplayEvent(req0);
50+
expect(replayEvent0).toEqual(getExpectedReplayEvent({}));
51+
52+
const fullSnapshots0 = getFullRecordingSnapshots(req0);
53+
expect(fullSnapshots0.length).toEqual(1);
54+
const stringifiedSnapshot = normalize(fullSnapshots0[0]);
55+
expect(stringifiedSnapshot).toMatchSnapshot('snapshot-0.json');
56+
57+
// Wait again for a new segment 0 (=new session)
58+
const reqPromise2 = waitForReplayRequest(page, 0);
59+
60+
// Wait for an incremental snapshot
61+
// Wait half of the session max age (after initial flush), but account for potentially slow runners
62+
const timePassed1 = Date.now() - startTimestamp;
63+
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE / 2 - timePassed1, 0)));
64+
await page.click('#button1');
65+
66+
const req1 = await reqPromise1;
67+
const replayEvent1 = getReplayEvent(req1);
68+
69+
expect(replayEvent1).toEqual(
70+
getExpectedReplayEvent({ replay_start_timestamp: undefined, segment_id: 1, urls: [] }),
71+
);
72+
73+
const replay1 = await getReplaySnapshot(page);
74+
const oldSessionId = replay1.session?.id;
75+
76+
// Wait for session to expire
77+
const timePassed2 = Date.now() - startTimestamp;
78+
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE - timePassed2, 0)));
79+
await page.click('#button2');
80+
81+
const req2 = await reqPromise2;
82+
const replay2 = await getReplaySnapshot(page);
83+
84+
expect(replay2.session?.id).not.toEqual(oldSessionId);
85+
86+
const replayEvent2 = getReplayEvent(req2);
87+
expect(replayEvent2).toEqual(getExpectedReplayEvent({}));
88+
89+
const fullSnapshots2 = getFullRecordingSnapshots(req2);
90+
expect(fullSnapshots2.length).toEqual(1);
91+
const stringifiedSnapshot2 = normalize(fullSnapshots2[0]);
92+
expect(stringifiedSnapshot2).toMatchSnapshot('snapshot-2.json');
93+
});
94+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"node": {
3+
"type": 0,
4+
"childNodes": [
5+
{
6+
"type": 1,
7+
"name": "html",
8+
"publicId": "",
9+
"systemId": "",
10+
"id": 2
11+
},
12+
{
13+
"type": 2,
14+
"tagName": "html",
15+
"attributes": {},
16+
"childNodes": [
17+
{
18+
"type": 2,
19+
"tagName": "head",
20+
"attributes": {},
21+
"childNodes": [
22+
{
23+
"type": 2,
24+
"tagName": "meta",
25+
"attributes": {
26+
"charset": "utf-8"
27+
},
28+
"childNodes": [],
29+
"id": 5
30+
}
31+
],
32+
"id": 4
33+
},
34+
{
35+
"type": 3,
36+
"textContent": "\n ",
37+
"id": 6
38+
},
39+
{
40+
"type": 2,
41+
"tagName": "body",
42+
"attributes": {},
43+
"childNodes": [
44+
{
45+
"type": 3,
46+
"textContent": "\n ",
47+
"id": 8
48+
},
49+
{
50+
"type": 2,
51+
"tagName": "button",
52+
"attributes": {
53+
"onclick": "console.log('Test log 1')",
54+
"id": "button1"
55+
},
56+
"childNodes": [
57+
{
58+
"type": 3,
59+
"textContent": "***** **",
60+
"id": 10
61+
}
62+
],
63+
"id": 9
64+
},
65+
{
66+
"type": 3,
67+
"textContent": "\n ",
68+
"id": 11
69+
},
70+
{
71+
"type": 2,
72+
"tagName": "button",
73+
"attributes": {
74+
"onclick": "console.log('Test log 2')",
75+
"id": "button2"
76+
},
77+
"childNodes": [
78+
{
79+
"type": 3,
80+
"textContent": "***** **",
81+
"id": 13
82+
}
83+
],
84+
"id": 12
85+
},
86+
{
87+
"type": 3,
88+
"textContent": "\n ",
89+
"id": 14
90+
},
91+
{
92+
"type": 3,
93+
"textContent": "\n\n",
94+
"id": 15
95+
}
96+
],
97+
"id": 7
98+
}
99+
],
100+
"id": 3
101+
}
102+
],
103+
"id": 1
104+
},
105+
"initialOffset": {
106+
"left": 0,
107+
"top": 0
108+
}
109+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"node": {
3+
"type": 0,
4+
"childNodes": [
5+
{
6+
"type": 1,
7+
"name": "html",
8+
"publicId": "",
9+
"systemId": "",
10+
"id": 2
11+
},
12+
{
13+
"type": 2,
14+
"tagName": "html",
15+
"attributes": {},
16+
"childNodes": [
17+
{
18+
"type": 2,
19+
"tagName": "head",
20+
"attributes": {},
21+
"childNodes": [
22+
{
23+
"type": 2,
24+
"tagName": "meta",
25+
"attributes": {
26+
"charset": "utf-8"
27+
},
28+
"childNodes": [],
29+
"id": 5
30+
}
31+
],
32+
"id": 4
33+
},
34+
{
35+
"type": 3,
36+
"textContent": "\n ",
37+
"id": 6
38+
},
39+
{
40+
"type": 2,
41+
"tagName": "body",
42+
"attributes": {},
43+
"childNodes": [
44+
{
45+
"type": 3,
46+
"textContent": "\n ",
47+
"id": 8
48+
},
49+
{
50+
"type": 2,
51+
"tagName": "button",
52+
"attributes": {
53+
"onclick": "console.log('Test log 1')",
54+
"id": "button1"
55+
},
56+
"childNodes": [
57+
{
58+
"type": 3,
59+
"textContent": "***** **",
60+
"id": 10
61+
}
62+
],
63+
"id": 9
64+
},
65+
{
66+
"type": 3,
67+
"textContent": "\n ",
68+
"id": 11
69+
},
70+
{
71+
"type": 2,
72+
"tagName": "button",
73+
"attributes": {
74+
"onclick": "console.log('Test log 2')",
75+
"id": "button2"
76+
},
77+
"childNodes": [
78+
{
79+
"type": 3,
80+
"textContent": "***** **",
81+
"id": 13
82+
}
83+
],
84+
"id": 12
85+
},
86+
{
87+
"type": 3,
88+
"textContent": "\n ",
89+
"id": 14
90+
},
91+
{
92+
"type": 3,
93+
"textContent": "\n\n",
94+
"id": 15
95+
}
96+
],
97+
"id": 7
98+
}
99+
],
100+
"id": 3
101+
}
102+
],
103+
"id": 1
104+
},
105+
"initialOffset": {
106+
"left": 0,
107+
"top": 0
108+
}
109+
}

0 commit comments

Comments
 (0)