From b246880d2abf93388e9f4358fca95402297c8714 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 5 Jan 2024 17:26:52 +0100 Subject: [PATCH] test(node): Add memory leak test for `LocalVariables` --- .../local-variables-memory-test.js | 44 +++++++++++++++++++ .../suites/public-api/LocalVariables/test.ts | 25 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js diff --git a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js new file mode 100644 index 000000000000..7b227d4d08de --- /dev/null +++ b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js @@ -0,0 +1,44 @@ +/* eslint-disable no-unused-vars */ +const Sentry = require('@sentry/node'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + includeLocalVariables: true, + beforeSend: _ => { + return null; + }, + // Stop the rate limiting from kicking in + integrations: [new Sentry.Integrations.LocalVariables({ maxExceptionsPerSecond: 10000000 })], +}); + +class Some { + two(name) { + throw new Error('Enough!'); + } +} + +function one(name) { + const arr = [1, '2', null]; + const obj = { + name, + num: 5, + }; + + const ty = new Some(); + + ty.two(name); +} + +// Every millisecond cause a caught exception +setInterval(() => { + try { + one('some name'); + } catch (e) { + // + } +}, 1); + +// Every second send a memory usage update to parent process +setInterval(() => { + process.send({ memUsage: process.memoryUsage() }); +}, 1000); diff --git a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts index 0b542c19c629..1c58fd802122 100644 --- a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts +++ b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts @@ -109,4 +109,29 @@ conditionalTest({ min: 18 })('LocalVariables integration', () => { done(); }); }); + + test('Should not leak memory', done => { + const testScriptPath = path.resolve(__dirname, 'local-variables-memory-test.js'); + + const child = childProcess.spawn('node', [testScriptPath], { + stdio: ['inherit', 'inherit', 'inherit', 'ipc'], + }); + + let reportedCount = 0; + + child.on('message', msg => { + reportedCount++; + const rssMb = msg.memUsage.rss / 1024 / 1024; + // We shouldn't use more than 100MB of memory + expect(rssMb).toBeLessThan(100); + }); + + // Wait for 20 seconds + setTimeout(() => { + // Ensure we've had memory usage reported at least 15 times + expect(reportedCount).toBeGreaterThan(15); + child.kill(); + done(); + }, 20000); + }); });