Skip to content

Commit 3160419

Browse files
authored
test(node): Add memory leak test for LocalVariables (#10080)
This PR adds a test for the `LocalVariable` integration that causes 20k caught exceptions and checks that the memory usage does not go over 100MB.
1 parent e7552e3 commit 3160419

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint-disable no-unused-vars */
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
includeLocalVariables: true,
7+
beforeSend: _ => {
8+
return null;
9+
},
10+
// Stop the rate limiting from kicking in
11+
integrations: [new Sentry.Integrations.LocalVariables({ maxExceptionsPerSecond: 10000000 })],
12+
});
13+
14+
class Some {
15+
two(name) {
16+
throw new Error('Enough!');
17+
}
18+
}
19+
20+
function one(name) {
21+
const arr = [1, '2', null];
22+
const obj = {
23+
name,
24+
num: 5,
25+
};
26+
27+
const ty = new Some();
28+
29+
ty.two(name);
30+
}
31+
32+
// Every millisecond cause a caught exception
33+
setInterval(() => {
34+
try {
35+
one('some name');
36+
} catch (e) {
37+
//
38+
}
39+
}, 1);
40+
41+
// Every second send a memory usage update to parent process
42+
setInterval(() => {
43+
process.send({ memUsage: process.memoryUsage() });
44+
}, 1000);

dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,29 @@ conditionalTest({ min: 18 })('LocalVariables integration', () => {
109109
done();
110110
});
111111
});
112+
113+
test('Should not leak memory', done => {
114+
const testScriptPath = path.resolve(__dirname, 'local-variables-memory-test.js');
115+
116+
const child = childProcess.spawn('node', [testScriptPath], {
117+
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
118+
});
119+
120+
let reportedCount = 0;
121+
122+
child.on('message', msg => {
123+
reportedCount++;
124+
const rssMb = msg.memUsage.rss / 1024 / 1024;
125+
// We shouldn't use more than 100MB of memory
126+
expect(rssMb).toBeLessThan(100);
127+
});
128+
129+
// Wait for 20 seconds
130+
setTimeout(() => {
131+
// Ensure we've had memory usage reported at least 15 times
132+
expect(reportedCount).toBeGreaterThan(15);
133+
child.kill();
134+
done();
135+
}, 20000);
136+
});
112137
});

0 commit comments

Comments
 (0)