Skip to content

Commit

Permalink
actually fix memory leak test failing spuriously
Browse files Browse the repository at this point in the history
There is no way to make the process heap tracking reliable in CI.  Just
use v8 if available, otherwise skip the test.
  • Loading branch information
isaacs committed Apr 1, 2022
1 parent f3ffe05 commit d834068
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions test/avoid-memory-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ try {
v8 = require('v8')
} catch (er) {}

var previousHeapStats
if (!v8 || !v8.getHeapStatistics || typeof v8.getHeapStatistics().number_of_detached_contexts !== 'number') {
t.plan(0, 'no reliable context tracking available')
process.exit(0)
}

if (typeof global.gc !== 'function') {
t.plan(0, '--expose_gc not enabled')
process.exit(0)
}

function checkHeap (t) {
var v8stats = v8 ? v8.getHeapStatistics() : {}
var stats = process.memoryUsage()
if (typeof v8stats.number_of_detached_contexts === 'number')
t.equal(v8stats.number_of_detached_contexts, 0, 'no detached contexts')
else {
const memoryUsage = stats.heapUsed - previousHeapStats.heapUsed
const memoryUsageMB = Math.round(memoryUsage / Math.pow(1024, 2))
t.ok(memoryUsageMB < 10, 'expect less than 10MB difference, '
+ memoryUsageMB + 'MB difference found.');
}
var v8stats = v8.getHeapStatistics()
t.equal(v8stats.number_of_detached_contexts, 0, 'no detached contexts')
}

t.test('no memory leak when loading multiple times', function(t) {
t.plan(1);
importFresh(process.cwd() + '/graceful-fs.js') // node 0.10-5 were getting: Cannot find module '../'
previousHeapStats = process.memoryUsage()
// simulate project with 4000 tests
var i = 0;
function importFreshGracefulFs() {
importFresh(process.cwd() + '/graceful-fs.js');
if (i < 4000) {
i++;
process.nextTick(() => importFreshGracefulFs());
process.nextTick(importFreshGracefulFs)
} else {
global.gc()
checkHeap(t);
Expand Down

0 comments on commit d834068

Please sign in to comment.