Skip to content

Commit a8b8d3f

Browse files
jasnelltargos
authored andcommitted
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: #22446 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 6d0c3d1 commit a8b8d3f

11 files changed

+81
-57
lines changed

test/common/README.md

+28-15
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,6 @@ otherwise.
296296
### noWarnCode
297297
See `common.expectWarning()` for usage.
298298

299-
### onGC(target, listener)
300-
* `target` [&lt;Object>]
301-
* `listener` [&lt;Object>]
302-
* `ongc` [&lt;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-
314299
### opensslCli
315300
* [&lt;boolean>]
316301

@@ -759,6 +744,34 @@ via `NODE_TEST_*` environment variables. For example, to configure
759744
`internet.addresses.INET_HOST`, set the environment
760745
variable `NODE_TEST_INET_HOST` to a specified host.
761746

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` [&lt;Object>]
761+
* `listener` [&lt;Object>]
762+
* `ongc` [&lt;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+
762775
## tmpdir Module
763776

764777
The `tmpdir` module supports the use of a temporary directory for testing.

test/common/index.js

-27
Original file line numberDiff line numberDiff line change
@@ -805,30 +805,3 @@ exports.isCPPSymbolsNotMapped = exports.isWindows ||
805805
exports.isAIX ||
806806
exports.isLinuxPPCBE ||
807807
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-
};

test/common/ongc.js

+32
Original file line numberDiff line numberDiff 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;

test/parallel/test-common-gc.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22
// Flags: --expose-gc
33
const common = require('../common');
4+
const onGC = require('../common/ongc');
45

56
{
6-
const gcListener = { ongc: common.mustCall() };
7-
common.onGC({}, gcListener);
7+
onGC({}, { ongc: common.mustCall() });
88
global.gc();
99
}
1010

1111
{
12-
const gcListener = { ongc: common.mustNotCall() };
13-
common.onGC(process, gcListener);
12+
onGC(process, { ongc: common.mustNotCall() });
1413
global.gc();
1514
}

test/parallel/test-gc-http-client-connaborted.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// just like test-gc-http-client.js,
44
// but aborting every connection that comes in.
55

6-
const common = require('../common');
6+
require('../common');
7+
const onGC = require('../common/ongc');
78

89
function serverHandler(req, res) {
910
res.connection.destroy();
@@ -36,7 +37,7 @@ function getall() {
3637
}, cb).on('error', cb);
3738

3839
count++;
39-
common.onGC(req, { ongc });
40+
onGC(req, { ongc });
4041
})();
4142

4243
setImmediate(getall);

test/parallel/test-gc-http-client-onerror.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// just like test-gc-http-client.js,
44
// but with an on('error') handler that does nothing.
55

6-
const common = require('../common');
6+
require('../common');
7+
const onGC = require('../common/ongc');
78

89
function serverHandler(req, res) {
910
req.resume();
@@ -42,7 +43,7 @@ function getall() {
4243
}, cb).on('error', onerror);
4344

4445
count++;
45-
common.onGC(req, { ongc });
46+
onGC(req, { ongc });
4647
})();
4748

4849
setImmediate(getall);

test/parallel/test-gc-http-client-timeout.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// just like test-gc-http-client.js,
44
// but with a timeout set
55

6-
const common = require('../common');
6+
require('../common');
7+
const onGC = require('../common/ongc');
78

89
function serverHandler(req, res) {
910
setTimeout(function() {
@@ -45,7 +46,7 @@ function getall() {
4546
});
4647

4748
count++;
48-
common.onGC(req, { ongc });
49+
onGC(req, { ongc });
4950
})();
5051

5152
setImmediate(getall);

test/parallel/test-gc-http-client.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// just a simple http server and client.
44

55
const common = require('../common');
6+
const onGC = require('../common/ongc');
67

78
function serverHandler(req, res) {
89
res.writeHead(200, { 'Content-Type': 'text/plain' });
@@ -34,7 +35,7 @@ function getall() {
3435
}, cb);
3536

3637
count++;
37-
common.onGC(req, { ongc });
38+
onGC(req, { ongc });
3839

3940
setImmediate(getall);
4041
}

test/parallel/test-gc-net-timeout.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// just like test-gc-http-client-timeout.js,
44
// but using a net server/client instead
55

6-
const common = require('../common');
6+
require('../common');
7+
const onGC = require('../common/ongc');
78

89
function serverHandler(sock) {
910
sock.setTimeout(120000);
@@ -44,7 +45,7 @@ function getall() {
4445
});
4546

4647
count++;
47-
common.onGC(req, { ongc });
48+
onGC(req, { ongc });
4849

4950
setImmediate(getall);
5051
}

test/parallel/test-net-connect-memleak.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// Flags: --expose-gc
2424

2525
const common = require('../common');
26+
const onGC = require('../common/ongc');
2627
const assert = require('assert');
2728
const net = require('net');
2829

@@ -36,7 +37,7 @@ const gcListener = { ongc() { collected = true; } };
3637

3738
{
3839
const gcObject = {};
39-
common.onGC(gcObject, gcListener);
40+
onGC(gcObject, gcListener);
4041

4142
const sock = net.createConnection(
4243
server.address().port,

test/parallel/test-tls-connect-memleak.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const common = require('../common');
2626
if (!common.hasCrypto)
2727
common.skip('missing crypto');
2828

29+
const onGC = require('../common/ongc');
2930
const assert = require('assert');
3031
const tls = require('tls');
3132
const fixtures = require('../common/fixtures');
@@ -43,7 +44,7 @@ const gcListener = { ongc() { collected = true; } };
4344

4445
{
4546
const gcObject = {};
46-
common.onGC(gcObject, gcListener);
47+
onGC(gcObject, gcListener);
4748

4849
const sock = tls.connect(
4950
server.address().port,

0 commit comments

Comments
 (0)