|
1 | | -// Flags: --expose-gc |
| 1 | +// Flags: --expose-internals --expose-internals --max-old-space-size=16 |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | // This test ensures that diagnostic channel references aren't leaked. |
5 | 5 |
|
6 | 6 | require('../common'); |
7 | | -const { ok } = require('assert'); |
8 | | - |
9 | | -const { subscribe, unsubscribe } = require('diagnostics_channel'); |
| 7 | +const assert = require('assert'); |
10 | 8 |
|
| 9 | +const { subscribe, unsubscribe, Channel } = require('diagnostics_channel'); |
| 10 | +const { internalBinding } = require('internal/test/binding'); |
| 11 | +const { countObjectsWithPrototype } = internalBinding('heap_utils'); |
11 | 12 | function noop() {} |
12 | 13 |
|
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); |
| 14 | +const initialCount = countObjectsWithPrototype(Channel.prototype); |
| 15 | +console.log('Initial count of diagnostics channels', initialCount); |
| 16 | +const outer = 1024; |
| 17 | +const inner = 16; |
| 18 | +let finished = 0; |
| 19 | + |
| 20 | +for (let i = 0; i < outer; i++) { |
| 21 | + setImmediate(() => { |
| 22 | + for (let j = 0; j < inner; j++) { |
| 23 | + const key = String(i * inner + j); |
| 24 | + subscribe(key, noop); |
| 25 | + unsubscribe(key, noop); |
| 26 | + } |
| 27 | + if (++finished === outer) { |
| 28 | + setImmediate(check); |
| 29 | + } |
| 30 | + }); |
18 | 31 | } |
19 | 32 |
|
20 | | -global.gc(); |
21 | | - |
22 | | -const heapUsedAfter = process.memoryUsage().heapUsed; |
23 | | - |
24 | | -ok(heapUsedBefore >= heapUsedAfter); |
| 33 | +function check() { |
| 34 | + const afterCount = countObjectsWithPrototype(Channel.prototype); |
| 35 | + console.log('Ending count of diagnostics channels', afterCount); |
| 36 | + const remaining = afterCount - initialCount; |
| 37 | + console.log('Remaining count of diagnostics channels', remaining); |
| 38 | + console.log('Number of channels created', inner * outer); |
| 39 | + assert(remaining < inner * outer); |
| 40 | +} |
0 commit comments