summaryrefslogtreecommitdiff
path: root/test/common/ongc.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2018-08-21 13:38:59 -0700
committerJames M Snell <jasnell@gmail.com>2018-08-24 10:32:09 -0700
commitd495e40bf7c75c689df75b09ae1bf9a92f3eb584 (patch)
treee1616a74cf3ebb3062332fffe7d7c50766a825cb /test/common/ongc.js
parent3579ec4171734fea65e52d385594a35a40bcde8f (diff)
downloadandroid-node-v8-d495e40bf7c75c689df75b09ae1bf9a92f3eb584.tar.gz
android-node-v8-d495e40bf7c75c689df75b09ae1bf9a92f3eb584.tar.bz2
android-node-v8-d495e40bf7c75c689df75b09ae1bf9a92f3eb584.zip
test: move common.onGC to individual module
Incrementally making `require('../common')` less of a monolith. Move the `common.onGC()` utility to a separate standalone module that is only imported when it's actually needed. PR-URL: https://github.com/nodejs/node/pull/22446 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'test/common/ongc.js')
-rw-r--r--test/common/ongc.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/common/ongc.js b/test/common/ongc.js
new file mode 100644
index 0000000000..d8e27beb8f
--- /dev/null
+++ b/test/common/ongc.js
@@ -0,0 +1,32 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const gcTrackerMap = new WeakMap();
+const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER';
+
+function onGC(obj, gcListener) {
+ const async_hooks = require('async_hooks');
+
+ const onGcAsyncHook = async_hooks.createHook({
+ init: common.mustCallAtLeast(function(id, type) {
+ if (this.trackedId === undefined) {
+ assert.strictEqual(type, gcTrackerTag);
+ this.trackedId = id;
+ }
+ }),
+ destroy(id) {
+ assert.notStrictEqual(this.trackedId, -1);
+ if (id === this.trackedId) {
+ this.gcListener.ongc();
+ onGcAsyncHook.disable();
+ }
+ }
+ }).enable();
+ onGcAsyncHook.gcListener = gcListener;
+
+ gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag));
+ obj = null;
+}
+
+module.exports = onGC;