Skip to content

Commit e008b75

Browse files
committed
test: use checkIfCollectable() in diganostics channel leak test
Previously it only checks that the heap memory usage is down after global.gc() returns, which is unreliable because global.gc() does not guarantee that the heap memory usage would go down even when there is no leak (it could go up due to promotion, or it only kicks off marking). Use the more reliable checkIfCollectable() utility instead.
1 parent 0cec822 commit e008b75

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
// Flags: --expose-gc
1+
// Flags: --max-old-space-size=16
22
'use strict';
33

44
// This test ensures that diagnostic channel references aren't leaked.
55

66
require('../common');
7-
const { ok } = require('assert');
8-
9-
const { subscribe, unsubscribe } = require('diagnostics_channel');
7+
const { checkIfCollectable } = require('../common/gc');
8+
const { subscribe, unsubscribe, channel } = require('diagnostics_channel');
109

1110
function noop() {}
1211

13-
const heapUsedBefore = process.memoryUsage().heapUsed;
14-
15-
for (let i = 0; i < 1000; i++) {
16-
subscribe(String(i), noop);
17-
unsubscribe(String(i), noop);
18-
}
19-
20-
global.gc();
21-
22-
const heapUsedAfter = process.memoryUsage().heapUsed;
23-
24-
ok(heapUsedBefore >= heapUsedAfter);
12+
let i = 0;
13+
checkIfCollectable(() => {
14+
const key = new Array(1024 * 1024).fill(i++).join('\n');
15+
subscribe(key, noop);
16+
unsubscribe(key, noop);
17+
return channel(key);
18+
}, 4096, 1024);

0 commit comments

Comments
 (0)