| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6d0c3d1 commit a8b8d3f
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -296,21 +296,6 @@ otherwise. | |||
| 296 | 296 | ### noWarnCode | |
| 297 | 297 | See `common.expectWarning()` for usage. | |
| 298 | 298 | ||
| 299 | - ### onGC(target, listener) | ||
| 300 | - * `target` [<Object>] | ||
| 301 | - * `listener` [<Object>] | ||
| 302 | - * `ongc` [<Function>] | ||
| 303 | - | ||
| 304 | - Installs a GC listener for the collection of `target`. | ||
| 305 | - | ||
| 306 | - This uses `async_hooks` for GC tracking. This means that it enables | ||
| 307 | - `async_hooks` tracking, which may affect the test functionality. It also | ||
| 308 | - means that between a `global.gc()` call and the listener being invoked | ||
| 309 | - a full `setImmediate()` invocation passes. | ||
| 310 | - | ||
| 311 | - `listener` is an object to make it easier to use a closure; the target object | ||
| 312 | - should not be in scope when `listener.ongc()` is created. | ||
| 313 | - | ||
| 314 | 299 | ### opensslCli | |
| 315 | 300 | * [<boolean>] | |
| 316 | 301 | ||
@@ -759,6 +744,34 @@ via `NODE_TEST_*` environment variables. For example, to configure | |||
| 759 | 744 | `internet.addresses.INET_HOST`, set the environment | |
| 760 | 745 | variable `NODE_TEST_INET_HOST` to a specified host. | |
| 761 | 746 | ||
| 747 | + ## ongc Module | ||
| 748 | + | ||
| 749 | + The `ongc` module allows a garbage collection listener to be installed. The | ||
| 750 | + module exports a single `onGC()` function. | ||
| 751 | + | ||
| 752 | + ```js | ||
| 753 | + require('../common'); | ||
| 754 | + const onGC = require('../common/ongc'); | ||
| 755 | + | ||
| 756 | + onGC({}, { ongc() { console.log('collected'); } }); | ||
| 757 | + ``` | ||
| 758 | + | ||
| 759 | + ### onGC(target, listener) | ||
| 760 | + * `target` [<Object>] | ||
| 761 | + * `listener` [<Object>] | ||
| 762 | + * `ongc` [<Function>] | ||
| 763 | + | ||
| 764 | + Installs a GC listener for the collection of `target`. | ||
| 765 | + | ||
| 766 | + This uses `async_hooks` for GC tracking. This means that it enables | ||
| 767 | + `async_hooks` tracking, which may affect the test functionality. It also | ||
| 768 | + means that between a `global.gc()` call and the listener being invoked | ||
| 769 | + a full `setImmediate()` invocation passes. | ||
| 770 | + | ||
| 771 | + `listener` is an object to make it easier to use a closure; the target object | ||
| 772 | + should not be in scope when `listener.ongc()` is created. | ||
| 773 | + | ||
| 774 | + | ||
| 762 | 775 | ## tmpdir Module | |
| 763 | 776 | ||
| 764 | 777 | The `tmpdir` module supports the use of a temporary directory for testing. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -805,30 +805,3 @@ exports.isCPPSymbolsNotMapped = exports.isWindows || | |||
| 805 | 805 | exports.isAIX || | |
| 806 | 806 | exports.isLinuxPPCBE || | |
| 807 | 807 | exports.isFreeBSD; | |
| 808 | - | ||
| 809 | - const gcTrackerMap = new WeakMap(); | ||
| 810 | - const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER'; | ||
| 811 | - | ||
| 812 | - exports.onGC = function(obj, gcListener) { | ||
| 813 | - const async_hooks = require('async_hooks'); | ||
| 814 | - | ||
| 815 | - const onGcAsyncHook = async_hooks.createHook({ | ||
| 816 | - init: exports.mustCallAtLeast(function(id, type, trigger, resource) { | ||
| 817 | - if (this.trackedId === undefined) { | ||
| 818 | - assert.strictEqual(type, gcTrackerTag); | ||
| 819 | - this.trackedId = id; | ||
| 820 | - } | ||
| 821 | - }), | ||
| 822 | - destroy(id) { | ||
| 823 | - assert.notStrictEqual(this.trackedId, -1); | ||
| 824 | - if (id === this.trackedId) { | ||
| 825 | - this.gcListener.ongc(); | ||
| 826 | - onGcAsyncHook.disable(); | ||
| 827 | - } | ||
| 828 | - } | ||
| 829 | - }).enable(); | ||
| 830 | - onGcAsyncHook.gcListener = gcListener; | ||
| 831 | - | ||
| 832 | - gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag)); | ||
| 833 | - obj = null; | ||
| 834 | - }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const gcTrackerMap = new WeakMap(); | ||
| 6 | + const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER'; | ||
| 7 | + | ||
| 8 | + function onGC(obj, gcListener) { | ||
| 9 | + const async_hooks = require('async_hooks'); | ||
| 10 | + | ||
| 11 | + const onGcAsyncHook = async_hooks.createHook({ | ||
| 12 | + init: common.mustCallAtLeast(function(id, type) { | ||
| 13 | + if (this.trackedId === undefined) { | ||
| 14 | + assert.strictEqual(type, gcTrackerTag); | ||
| 15 | + this.trackedId = id; | ||
| 16 | + } | ||
| 17 | + }), | ||
| 18 | + destroy(id) { | ||
| 19 | + assert.notStrictEqual(this.trackedId, -1); | ||
| 20 | + if (id === this.trackedId) { | ||
| 21 | + this.gcListener.ongc(); | ||
| 22 | + onGcAsyncHook.disable(); | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + }).enable(); | ||
| 26 | + onGcAsyncHook.gcListener = gcListener; | ||
| 27 | + | ||
| 28 | + gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag)); | ||
| 29 | + obj = null; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + module.exports = onGC; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,14 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | // Flags: --expose-gc | |
| 3 | 3 | const common = require('../common'); | |
| 4 | + const onGC = require('../common/ongc'); | ||
| 4 | 5 | ||
| 5 | 6 | { | |
| 6 | - const gcListener = { ongc: common.mustCall() }; | ||
| 7 | - common.onGC({}, gcListener); | ||
| 7 | + onGC({}, { ongc: common.mustCall() }); | ||
| 8 | 8 | global.gc(); | |
| 9 | 9 | } | |
| 10 | 10 | ||
| 11 | 11 | { | |
| 12 | - const gcListener = { ongc: common.mustNotCall() }; | ||
| 13 | - common.onGC(process, gcListener); | ||
| 12 | + onGC(process, { ongc: common.mustNotCall() }); | ||
| 14 | 13 | global.gc(); | |
| 15 | 14 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,8 @@ | |||
| 3 | 3 | // just like test-gc-http-client.js, | |
| 4 | 4 | // but aborting every connection that comes in. | |
| 5 | 5 | ||
| 6 | - const common = require('../common'); | ||
| 6 | + require('../common'); | ||
| 7 | + const onGC = require('../common/ongc'); | ||
| 7 | 8 | ||
| 8 | 9 | function serverHandler(req, res) { | |
| 9 | 10 | res.connection.destroy(); | |
@@ -36,7 +37,7 @@ function getall() { | |||
| 36 | 37 | }, cb).on('error', cb); | |
| 37 | 38 | ||
| 38 | 39 | count++; | |
| 39 | - common.onGC(req, { ongc }); | ||
| 40 | + onGC(req, { ongc }); | ||
| 40 | 41 | })(); | |
| 41 | 42 | ||
| 42 | 43 | setImmediate(getall); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,8 @@ | |||
| 3 | 3 | // just like test-gc-http-client.js, | |
| 4 | 4 | // but with an on('error') handler that does nothing. | |
| 5 | 5 | ||
| 6 | - const common = require('../common'); | ||
| 6 | + require('../common'); | ||
| 7 | + const onGC = require('../common/ongc'); | ||
| 7 | 8 | ||
| 8 | 9 | function serverHandler(req, res) { | |
| 9 | 10 | req.resume(); | |
@@ -42,7 +43,7 @@ function getall() { | |||
| 42 | 43 | }, cb).on('error', onerror); | |
| 43 | 44 | ||
| 44 | 45 | count++; | |
| 45 | - common.onGC(req, { ongc }); | ||
| 46 | + onGC(req, { ongc }); | ||
| 46 | 47 | })(); | |
| 47 | 48 | ||
| 48 | 49 | setImmediate(getall); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,8 @@ | |||
| 3 | 3 | // just like test-gc-http-client.js, | |
| 4 | 4 | // but with a timeout set | |
| 5 | 5 | ||
| 6 | - const common = require('../common'); | ||
| 6 | + require('../common'); | ||
| 7 | + const onGC = require('../common/ongc'); | ||
| 7 | 8 | ||
| 8 | 9 | function serverHandler(req, res) { | |
| 9 | 10 | setTimeout(function() { | |
@@ -45,7 +46,7 @@ function getall() { | |||
| 45 | 46 | }); | |
| 46 | 47 | ||
| 47 | 48 | count++; | |
| 48 | - common.onGC(req, { ongc }); | ||
| 49 | + onGC(req, { ongc }); | ||
| 49 | 50 | })(); | |
| 50 | 51 | ||
| 51 | 52 | setImmediate(getall); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | // just a simple http server and client. | |
| 4 | 4 | ||
| 5 | 5 | const common = require('../common'); | |
| 6 | + const onGC = require('../common/ongc'); | ||
| 6 | 7 | ||
| 7 | 8 | function serverHandler(req, res) { | |
| 8 | 9 | res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
@@ -34,7 +35,7 @@ function getall() { | |||
| 34 | 35 | }, cb); | |
| 35 | 36 | ||
| 36 | 37 | count++; | |
| 37 | - common.onGC(req, { ongc }); | ||
| 38 | + onGC(req, { ongc }); | ||
| 38 | 39 | ||
| 39 | 40 | setImmediate(getall); | |
| 40 | 41 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,8 @@ | |||
| 3 | 3 | // just like test-gc-http-client-timeout.js, | |
| 4 | 4 | // but using a net server/client instead | |
| 5 | 5 | ||
| 6 | - const common = require('../common'); | ||
| 6 | + require('../common'); | ||
| 7 | + const onGC = require('../common/ongc'); | ||
| 7 | 8 | ||
| 8 | 9 | function serverHandler(sock) { | |
| 9 | 10 | sock.setTimeout(120000); | |
@@ -44,7 +45,7 @@ function getall() { | |||
| 44 | 45 | }); | |
| 45 | 46 | ||
| 46 | 47 | count++; | |
| 47 | - common.onGC(req, { ongc }); | ||
| 48 | + onGC(req, { ongc }); | ||
| 48 | 49 | ||
| 49 | 50 | setImmediate(getall); | |
| 50 | 51 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ | |||
| 23 | 23 | // Flags: --expose-gc | |
| 24 | 24 | ||
| 25 | 25 | const common = require('../common'); | |
| 26 | + const onGC = require('../common/ongc'); | ||
| 26 | 27 | const assert = require('assert'); | |
| 27 | 28 | const net = require('net'); | |
| 28 | 29 | ||
@@ -36,7 +37,7 @@ const gcListener = { ongc() { collected = true; } }; | |||
| 36 | 37 | ||
| 37 | 38 | { | |
| 38 | 39 | const gcObject = {}; | |
| 39 | - common.onGC(gcObject, gcListener); | ||
| 40 | + onGC(gcObject, gcListener); | ||
| 40 | 41 | ||
| 41 | 42 | const sock = net.createConnection( | |
| 42 | 43 | server.address().port, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments