|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | + |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { getCustomRecordingEvents, shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers'; |
| 5 | + |
| 6 | +const COUNT = 250; |
| 7 | +const THROTTLE_LIMIT = 300; |
| 8 | + |
| 9 | +sentryTest( |
| 10 | + 'throttles breadcrumbs when many requests are made at the same time', |
| 11 | + async ({ getLocalTestUrl, page, forceFlushReplay }) => { |
| 12 | + if (shouldSkipReplayTest()) { |
| 13 | + sentryTest.skip(); |
| 14 | + } |
| 15 | + |
| 16 | + const reqPromise0 = waitForReplayRequest(page, 0); |
| 17 | + |
| 18 | + await page.route('https://dsn.ingest.sentry.io/**/*', route => { |
| 19 | + return route.fulfill({ |
| 20 | + status: 200, |
| 21 | + contentType: 'application/json', |
| 22 | + body: JSON.stringify({ id: 'test-id' }), |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + let scriptsLoaded = 0; |
| 27 | + let fetchLoaded = 0; |
| 28 | + |
| 29 | + await page.route('**/virtual-assets/script-**', route => { |
| 30 | + scriptsLoaded++; |
| 31 | + return route.fulfill({ |
| 32 | + status: 200, |
| 33 | + contentType: 'text/javascript', |
| 34 | + body: `const aha = ${'xx'.repeat(20_000)};`, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + await page.route('**/virtual-assets/fetch-**', route => { |
| 39 | + fetchLoaded++; |
| 40 | + return route.fulfill({ |
| 41 | + status: 200, |
| 42 | + contentType: 'application/json', |
| 43 | + body: JSON.stringify({ fetchResponse: 'aa'.repeat(20_000) }), |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 48 | + |
| 49 | + await page.goto(url); |
| 50 | + await reqPromise0; |
| 51 | + |
| 52 | + const reqPromise1 = waitForReplayRequest( |
| 53 | + page, |
| 54 | + (_event, res) => { |
| 55 | + const { performanceSpans } = getCustomRecordingEvents(res); |
| 56 | + |
| 57 | + return performanceSpans.some(span => span.op === 'resource.script'); |
| 58 | + }, |
| 59 | + 5_000, |
| 60 | + ); |
| 61 | + |
| 62 | + await page.click('[data-network]'); |
| 63 | + await page.click('[data-fetch]'); |
| 64 | + |
| 65 | + await page.waitForFunction('window.__isLoaded()'); |
| 66 | + await forceFlushReplay(); |
| 67 | + |
| 68 | + const { performanceSpans, breadcrumbs } = getCustomRecordingEvents(await reqPromise1); |
| 69 | + |
| 70 | + // All assets have been _loaded_ |
| 71 | + expect(scriptsLoaded).toBe(COUNT); |
| 72 | + expect(fetchLoaded).toBe(COUNT); |
| 73 | + |
| 74 | + // But only some have been captured by replay |
| 75 | + // We check for <= THROTTLE_LIMIT, as there have been some captured before, which take up some of the throttle limit |
| 76 | + expect(performanceSpans.length).toBeLessThanOrEqual(THROTTLE_LIMIT); |
| 77 | + expect(performanceSpans.length).toBeGreaterThan(THROTTLE_LIMIT - 10); |
| 78 | + |
| 79 | + expect(breadcrumbs.filter(({ category }) => category === 'replay.throttled').length).toBe(1); |
| 80 | + |
| 81 | + // Now we wait for 6s (5s + some wiggle room), and make some requests again |
| 82 | + await page.waitForTimeout(6_000); |
| 83 | + await forceFlushReplay(); |
| 84 | + |
| 85 | + const reqPromise2 = waitForReplayRequest( |
| 86 | + page, |
| 87 | + (_event, res) => { |
| 88 | + const { performanceSpans } = getCustomRecordingEvents(res); |
| 89 | + |
| 90 | + return performanceSpans.some(span => span.op === 'resource.script'); |
| 91 | + }, |
| 92 | + 5_000, |
| 93 | + ); |
| 94 | + |
| 95 | + await page.click('[data-network]'); |
| 96 | + await page.click('[data-fetch]'); |
| 97 | + |
| 98 | + await forceFlushReplay(); |
| 99 | + |
| 100 | + const { performanceSpans: performanceSpans2, breadcrumbs: breadcrumbs2 } = getCustomRecordingEvents( |
| 101 | + await reqPromise2, |
| 102 | + ); |
| 103 | + |
| 104 | + // All assets have been _loaded_ |
| 105 | + expect(scriptsLoaded).toBe(COUNT * 2); |
| 106 | + expect(fetchLoaded).toBe(COUNT * 2); |
| 107 | + |
| 108 | + // But only some have been captured by replay |
| 109 | + // We check for <= THROTTLE_LIMIT, as there have been some captured before, which take up some of the throttle limit |
| 110 | + expect(performanceSpans2.length).toBeLessThanOrEqual(THROTTLE_LIMIT); |
| 111 | + expect(performanceSpans2.length).toBeGreaterThan(THROTTLE_LIMIT - 10); |
| 112 | + |
| 113 | + expect(breadcrumbs2.filter(({ category }) => category === 'replay.throttled').length).toBe(1); |
| 114 | + }, |
| 115 | +); |
0 commit comments