|
| 1 | +import { MemoryProfiler } from '@sentry-internal/test-utils'; |
| 2 | +import { expect, test } from '@playwright/test'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Memory leak tests for Cloudflare Workers SDK. |
| 7 | + * |
| 8 | + * These tests verify that the CloudflareClient.dispose() mechanism properly |
| 9 | + * cleans up resources to prevent memory leaks. |
| 10 | + * |
| 11 | + * The test connects directly to the wrangler dev server's V8 inspector via CDP |
| 12 | + * (Chrome DevTools Protocol) on ws://127.0.0.1:9230/ws to take heap snapshots |
| 13 | + * of the actual worker isolate. |
| 14 | + * |
| 15 | + * @see https://developers.cloudflare.com/workers/observability/dev-tools/memory-usage/ |
| 16 | + */ |
| 17 | + |
| 18 | +// Wrangler dev exposes inspector on this port (configured in playwright.config.ts) |
| 19 | +const INSPECTOR_PORT = 9230; |
| 20 | + |
| 21 | +/** |
| 22 | + * CDP-based heap snapshot test for Cloudflare Workers. |
| 23 | + * |
| 24 | + * This test connects directly to the wrangler dev inspector at ws://127.0.0.1:9230/ws |
| 25 | + * to profile the actual worker's V8 isolate memory, not a browser. |
| 26 | + * |
| 27 | + * The wrangler dev server must be running with --inspector-port 9230. |
| 28 | + * This is configured in playwright.config.ts. |
| 29 | + */ |
| 30 | +test.describe('Worker V8 isolate memory tests', () => { |
| 31 | + // Heap snapshot serialization for the worker isolate can take 30s+ on top of the requests. |
| 32 | + test.setTimeout(120_000); |
| 33 | + |
| 34 | + test('worker memory is reclaimed after GC', async ({ baseURL }) => { |
| 35 | + const profiler = new MemoryProfiler({ port: INSPECTOR_PORT, debug: true }); |
| 36 | + |
| 37 | + await profiler.connect(); |
| 38 | + await profiler.startProfiling(); |
| 39 | + |
| 40 | + const numRequests = 50; |
| 41 | + |
| 42 | + for (let i = 0; i < numRequests; i++) { |
| 43 | + const res = await fetch(baseURL!); |
| 44 | + expect(res.status).toBe(200); |
| 45 | + await res.text(); |
| 46 | + } |
| 47 | + |
| 48 | + const result = await profiler.stopProfiling(); |
| 49 | + |
| 50 | + expect(result.growthKB).toBeLessThan(50); |
| 51 | + |
| 52 | + await profiler.close(); |
| 53 | + }); |
| 54 | +}); |
0 commit comments