Skip to content

Commit

Permalink
Add test case to demonstrate memory-leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay committed Dec 26, 2014
1 parent 6c6149a commit cd153d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -4,9 +4,10 @@
"description": "Bare bones Promises/A+ implementation",
"main": "index.js",
"scripts": {
"test": "mocha --timeout 200 --slow 99999",
"test": "mocha --timeout 200 --slow 99999 && npm run test-memory-leak",
"test-resolve": "mocha test/resolver-tests.js -R spec --timeout 200 --slow 999999",
"test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999"
"test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999",
"test-memory-leak": "node --expose-gc test/memory-leak.js"
},
"repository": {
"type": "git",
Expand Down
41 changes: 41 additions & 0 deletions test/memory-leak.js
@@ -0,0 +1,41 @@
'use strict';

var assert = require('assert')
var Promise = require('../')

var i = 0
var sampleA, sampleB

function next() {
return new Promise(function (resolve) {
i++
/*
if (i % 100000 === 0) {
global.gc()
console.dir(process.memoryUsage())
}
*/
if (i === 100000 && typeof global.gc === 'function') {
global.gc()
sampleA = process.memoryUsage()
}
if (i > 100000 * 5) {
if (typeof global.gc === 'function') {
global.gc()
sampleB = process.memoryUsage()
console.dir(sampleA)
console.dir(sampleB)
assert(sampleA.rss * 1.2 > sampleB.rss, 'RSS should not grow by more than 20%')
assert(sampleA.heapTotal * 1.2 > sampleB.heapTotal, 'heapTotal should not grow by more than 20%')
assert(sampleA.heapUsed * 1.2 > sampleB.heapUsed, 'heapUsed should not grow by more than 20%')
}
} else {
setImmediate(resolve)
}
}).then(next)
}

if (typeof global.gc !== 'function') {
console.warn('You must run with --expose-gc to test for memory leak.')
}
next().done()

0 comments on commit cd153d0

Please sign in to comment.