Skip to content

Commit 386e43f

Browse files
committed
test: deflake test-diagnostics-channel-memory-leak
1 parent 86c7783 commit 386e43f

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

test/common/gc.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,43 @@ async function runAndBreathe(fn, repeat, waitTime = 20) {
7575
}
7676
}
7777

78+
// This requires --expose-internals
79+
async function checkIfCollectableByCounting(fn, klass, count, waitTime = 20) {
80+
const { internalBinding } = require('internal/test/binding');
81+
const { countObjectsWithPrototype } = internalBinding('heap_utils');
82+
const { prototype, name } = klass;
83+
let initialCount = countObjectsWithPrototype(prototype);
84+
console.log(`Initial count of ${name}: ${initialCount}`);
85+
let totalCreated = 0;
86+
for (let i = 0; i < count; ++i) {
87+
const created = await fn(i);
88+
totalCreated += created;
89+
console.log(`#${i}: created ${created} ${name}, total ${totalCreated}`);
90+
await wait(waitTime); // give GC some breathing room.
91+
const currentCount = countObjectsWithPrototype(prototype);
92+
const collected = totalCreated - (currentCount - initialCount);
93+
console.log(`#${i}: counted ${currentCount} ${name}, collected ${collected}`);
94+
if (collected > 0 ) {
95+
console.log(`Detected ${collected} collected ${name}, finish early`);
96+
return;
97+
}
98+
}
99+
100+
await wait(waitTime); // give GC some breathing room.
101+
const currentCount = countObjectsWithPrototype(prototype);
102+
const collected = totalCreated - (currentCount - initialCount);
103+
console.log(`Last count: counted ${currentCount} ${name}, collected ${collected}`);
104+
// Some objects with the prototype can be collected.
105+
if (collected > 0) {
106+
console.log(`Detected ${collected} collected ${name}`);
107+
return;
108+
}
109+
110+
throw new Error(`${name} cannot be collected`);
111+
}
112+
78113
module.exports = {
79114
checkIfCollectable,
80115
runAndBreathe,
116+
checkIfCollectableByCounting,
81117
};

test/parallel/parallel.status

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ prefix parallel
55
# sample-test : PASS,FLAKY
66

77
[true] # This section applies to all platforms
8-
# https://github.com/nodejs/node/pull/50327
9-
# Currently there's no reliable way to test it.
10-
test-diagnostics-channel-memory-leak: SKIP
118

129
[$system==win32]
1310
# https://github.com/nodejs/node/issues/41206
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
// Flags: --expose-gc
1+
// Flags: --expose-internals --max-old-space-size=16
22
'use strict';
33

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

6-
require('../common');
7-
const { ok } = require('assert');
6+
const common = require('../common');
87

9-
const { subscribe, unsubscribe } = require('diagnostics_channel');
8+
const { subscribe, unsubscribe, Channel } = require('diagnostics_channel');
9+
const { checkIfCollectableByCounting } = require('../common/gc');
1010

1111
function noop() {}
1212

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);
13+
const outer = 64;
14+
const inner = 256;
15+
checkIfCollectableByCounting((i) => {
16+
for (let j = 0; j < inner; j++) {
17+
const key = String(i * inner + j);
18+
subscribe(key, noop);
19+
unsubscribe(key, noop);
20+
}
21+
return inner;
22+
}, Channel, outer).then(common.mustCall());

0 commit comments

Comments
 (0)