forked from eduardbme/node-memwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
64 lines (59 loc) · 1.74 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const
should = require('should'),
memwatch = require('./');
describe('the library', function() {
it('should export a couple functions', function(done) {
should.exist(memwatch.gc);
should.exist(memwatch.on);
should.exist(memwatch.once);
should.exist(memwatch.removeAllListeners);
should.exist(memwatch.HeapDiff);
done();
});
});
describe('calling .gc()', function() {
it('should cause a stats() event to be emitted', function(done) {
memwatch.once('stats', function(s) {
s.should.be.object;
done();
});
memwatch.gc();
});
});
describe('HeapDiff', function() {
it('should detect allocations', function(done) {
function LeakingClass() {};
var arr = [];
var hd = new memwatch.HeapDiff();
for (var i = 0; i < 100; i++) arr.push(new LeakingClass());
var diff = hd.end();
(Array.isArray(diff.change.details)).should.be.ok;
diff.change.details.should.be.an.instanceOf(Array);
// find the LeakingClass elem
var leakingReport;
diff.change.details.forEach(function(d) {
if (d.what === 'LeakingClass')
leakingReport = d;
});
should.exist(leakingReport);
((leakingReport['+'] - leakingReport['-']) > 0).should.be.ok;
done();
});
});
describe('HeapDiff', function() {
it('double end should throw', function(done) {
var hd = new memwatch.HeapDiff();
var arr = [];
(function() { hd.end(); }).should.not.throw();
(function() { hd.end(); }).should.throw();
done();
});
});
describe('improper HeapDiff allocation', function() {
it('should throw an exception', function(done) {
// equivalent to "new require('memwatch').HeapDiff()"
// see issue #30
(function() { new (memwatch.HeapDiff()); }).should.throw();
done();
});
});