Skip to content

Commit cd153d0

Browse files
Add test case to demonstrate memory-leak
1 parent 6c6149a commit cd153d0

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"description": "Bare bones Promises/A+ implementation",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha --timeout 200 --slow 99999",
7+
"test": "mocha --timeout 200 --slow 99999 && npm run test-memory-leak",
88
"test-resolve": "mocha test/resolver-tests.js -R spec --timeout 200 --slow 999999",
9-
"test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999"
9+
"test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999",
10+
"test-memory-leak": "node --expose-gc test/memory-leak.js"
1011
},
1112
"repository": {
1213
"type": "git",

test/memory-leak.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
var assert = require('assert')
4+
var Promise = require('../')
5+
6+
var i = 0
7+
var sampleA, sampleB
8+
9+
function next() {
10+
return new Promise(function (resolve) {
11+
i++
12+
/*
13+
if (i % 100000 === 0) {
14+
global.gc()
15+
console.dir(process.memoryUsage())
16+
}
17+
*/
18+
if (i === 100000 && typeof global.gc === 'function') {
19+
global.gc()
20+
sampleA = process.memoryUsage()
21+
}
22+
if (i > 100000 * 5) {
23+
if (typeof global.gc === 'function') {
24+
global.gc()
25+
sampleB = process.memoryUsage()
26+
console.dir(sampleA)
27+
console.dir(sampleB)
28+
assert(sampleA.rss * 1.2 > sampleB.rss, 'RSS should not grow by more than 20%')
29+
assert(sampleA.heapTotal * 1.2 > sampleB.heapTotal, 'heapTotal should not grow by more than 20%')
30+
assert(sampleA.heapUsed * 1.2 > sampleB.heapUsed, 'heapUsed should not grow by more than 20%')
31+
}
32+
} else {
33+
setImmediate(resolve)
34+
}
35+
}).then(next)
36+
}
37+
38+
if (typeof global.gc !== 'function') {
39+
console.warn('You must run with --expose-gc to test for memory leak.')
40+
}
41+
next().done()

0 commit comments

Comments
 (0)