GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • test
    • common
      • README.md
      • index.js
    • parallel
      • test-common-gc.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/common/README.md‎

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 numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ otherwise.
322322
### noWarnCode
323323
See `common.expectWarning()` for usage.
324324

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+
325340
### opensslCli
326341
* [<boolean>]
327342

Collapse file

‎test/common/index.js‎

Copy file name to clipboardExpand all lines: test/common/index.js
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,30 @@ exports.isCPPSymbolsNotMapped = exports.isWindows ||
883883
exports.isAIX ||
884884
exports.isLinuxPPCBE ||
885885
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

‎test/parallel/test-common-gc.js‎

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.