File tree 3 files changed +36
-14
lines changed
3 files changed +36
-14
lines changed Original file line number Diff line number Diff line change @@ -5,16 +5,27 @@ const { WeakReference } = internalBinding('util');
5
5
const {
6
6
setDeserializeMainFunction
7
7
} = require ( 'v8' ) . startupSnapshot
8
- const assert = require ( 'assert' ) ;
9
8
10
9
let obj = { hello : 'world' } ;
11
10
const ref = new WeakReference ( obj ) ;
11
+ let gcCount = 0 ;
12
+ let maxGC = 10 ;
12
13
13
- setDeserializeMainFunction ( ( ) => {
14
- obj = null ;
14
+ function run ( ) {
15
15
globalThis . gc ( ) ;
16
-
17
16
setImmediate ( ( ) => {
18
- assert . strictEqual ( ref . get ( ) , undefined ) ;
17
+ gcCount ++ ;
18
+ if ( ref . get ( ) === undefined ) {
19
+ return ;
20
+ } else if ( gcCount < maxGC ) {
21
+ run ( ) ;
22
+ } else {
23
+ throw new Error ( `Reference is still around after ${ maxGC } GC` ) ;
24
+ }
19
25
} ) ;
26
+ }
27
+
28
+ setDeserializeMainFunction ( ( ) => {
29
+ obj = null ;
30
+ run ( ) ;
20
31
} ) ;
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ const isEnumerable = Function.call.bind(Object.prototype.propertyIsEnumerable);
13
13
// See: https://github.com/nodejs/node/issues/23862
14
14
15
15
let d = domain . create ( ) ;
16
+ let resourceGCed = false ; let domainGCed = false ; let
17
+ emitterGCed = false ;
16
18
d . run ( ( ) => {
17
19
const resource = new async_hooks . AsyncResource ( 'TestResource' ) ;
18
20
const emitter = new EventEmitter ( ) ;
@@ -30,10 +32,17 @@ d.run(() => {
30
32
// emitter → resource → async id ⇒ domain → emitter.
31
33
// Make sure that all of these objects are released:
32
34
33
- onGC ( resource , { ongc : common . mustCall ( ) } ) ;
34
- onGC ( d , { ongc : common . mustCall ( ) } ) ;
35
- onGC ( emitter , { ongc : common . mustCall ( ) } ) ;
35
+ onGC ( resource , { ongc : common . mustCall ( ( ) => { resourceGCed = true ; } ) } ) ;
36
+ onGC ( d , { ongc : common . mustCall ( ( ) => { domainGCed = true ; } ) } ) ;
37
+ onGC ( emitter , { ongc : common . mustCall ( ( ) => { emitterGCed = true ; } ) } ) ;
36
38
} ) ;
37
39
38
40
d = null ;
39
- global . gc ( ) ;
41
+
42
+ async function main ( ) {
43
+ await common . gcUntil (
44
+ 'All objects garbage collected' ,
45
+ ( ) => resourceGCed && domainGCed && emitterGCed ) ;
46
+ }
47
+
48
+ main ( ) ;
Original file line number Diff line number Diff line change 1
1
// Flags: --expose-internals --expose-gc
2
2
'use strict' ;
3
- require ( '../common' ) ;
3
+ const common = require ( '../common' ) ;
4
4
const assert = require ( 'assert' ) ;
5
5
const { internalBinding } = require ( 'internal/test/binding' ) ;
6
6
const { WeakReference } = internalBinding ( 'util' ) ;
@@ -9,9 +9,11 @@ let obj = { hello: 'world' };
9
9
const ref = new WeakReference ( obj ) ;
10
10
assert . strictEqual ( ref . get ( ) , obj ) ;
11
11
12
- setImmediate ( ( ) => {
12
+ async function main ( ) {
13
13
obj = null ;
14
- global . gc ( ) ;
14
+ await common . gcUntil (
15
+ 'Reference is garbage collected' ,
16
+ ( ) => ref . get ( ) === undefined ) ;
17
+ }
15
18
16
- assert . strictEqual ( ref . get ( ) , undefined ) ;
17
- } ) ;
19
+ main ( ) ;
You can’t perform that action at this time.
0 commit comments