* Installs a garbage collection listener for the specified object. * Uses async_hooks for GC tracking, which may affect test functionality. * A full setImmediate() invocation passes between a global.gc() call and the listener being invoked. * @param {object} obj - The target object to track for g
(obj, gcListener)
| 17 | * @param {Function} gcListener.ongc - The function to call when the target object is garbage collected. |
| 18 | */ |
| 19 | function onGC(obj, gcListener) { |
| 20 | const async_hooks = require('async_hooks'); |
| 21 | |
| 22 | const onGcAsyncHook = async_hooks.createHook({ |
| 23 | init: common.mustCallAtLeast(function(id, type) { |
| 24 | if (this.trackedId === undefined) { |
| 25 | assert.strictEqual(type, gcTrackerTag); |
| 26 | this.trackedId = id; |
| 27 | } |
| 28 | }), |
| 29 | destroy(id) { |
| 30 | assert.notStrictEqual(this.trackedId, -1); |
| 31 | if (id === this.trackedId) { |
| 32 | this.gcListener.ongc(); |
| 33 | onGcAsyncHook.disable(); |
| 34 | } |
| 35 | }, |
| 36 | }).enable(); |
| 37 | onGcAsyncHook.gcListener = gcListener; |
| 38 | |
| 39 | gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag)); |
| 40 | obj = null; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Repeatedly triggers garbage collection until a specified condition is met or a maximum number of attempts is reached. |
searching dependent graphs…