GeezerVison is showing github.com — test: add gc tracking to common API · nodejs/node@b338ff5
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: test/common/README.md +15Lines changed: 15 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -322,6 +322,21 @@ otherwise. |
322 | 322 | ### noWarnCode |
323 | 323 | See `common.expectWarning()` for usage. |
324 | 324 |
|
| 325 | +### onGC(target, listener) |
| 326 | +* `target` [<Object>] |
| 327 | +* `listener` [<Object>] |
| 328 | + * `ongc` [<Function>] |
| 329 | + |
| 330 | +Installs a GC listener for the collection of `target`. |
| 331 | + |
| 332 | +This uses `async_hooks` for GC tracking. This means that it enables |
| 333 | +`async_hooks` tracking, which may affect the test functionality. It also |
| 334 | +means that between a `global.gc()` call and the listener being invoked |
| 335 | +a full `setImmediate()` invocation passes. |
| 336 | + |
| 337 | +`listener` is an object to make it easier to use a closure; the target object |
| 338 | +should not be in scope when `listener.ongc()` is created. |
| 339 | + |
325 | 340 | ### opensslCli |
326 | 341 | * [<boolean>] |
327 | 342 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/common/index.js +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -883,3 +883,30 @@ exports.isCPPSymbolsNotMapped = exports.isWindows || |
883 | 883 | exports.isAIX || |
884 | 884 | exports.isLinuxPPCBE || |
885 | 885 | exports.isFreeBSD; |
| 886 | + |
| 887 | +const gcTrackerMap = new WeakMap(); |
| 888 | +const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER'; |
| 889 | + |
| 890 | +exports.onGC = function(obj, gcListener) { |
| 891 | + const async_hooks = require('async_hooks'); |
| 892 | + |
| 893 | + const onGcAsyncHook = async_hooks.createHook({ |
| 894 | + init: exports.mustCallAtLeast(function(id, type, trigger, resource) { |
| 895 | + if (this.trackedId === undefined) { |
| 896 | + assert.strictEqual(type, gcTrackerTag); |
| 897 | + this.trackedId = id; |
| 898 | + } |
| 899 | + }), |
| 900 | + destroy(id) { |
| 901 | + assert.notStrictEqual(this.trackedId, -1); |
| 902 | + if (id === this.trackedId) { |
| 903 | + this.gcListener.ongc(); |
| 904 | + onGcAsyncHook.disable(); |
| 905 | + } |
| 906 | + } |
| 907 | + }).enable(); |
| 908 | + onGcAsyncHook.gcListener = gcListener; |
| 909 | + |
| 910 | + gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag)); |
| 911 | + obj = null; |
| 912 | +}; |
Collapse file
Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +// Flags: --expose-gc |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +{ |
| 6 | + const gcListener = { ongc: common.mustCall() }; |
| 7 | + common.onGC({}, gcListener); |
| 8 | + global.gc(); |
| 9 | +} |
| 10 | + |
| 11 | +{ |
| 12 | + const gcListener = { ongc: common.mustNotCall() }; |
| 13 | + common.onGC(process, gcListener); |
| 14 | + global.gc(); |
| 15 | +} |