File tree Expand file tree Collapse file tree 3 files changed +36
-14
lines changed
Expand file tree Collapse file tree 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');
55const {
66 setDeserializeMainFunction
77} = require ( 'v8' ) . startupSnapshot
8- const assert = require ( 'assert' ) ;
98
109let obj = { hello : 'world' } ;
1110const ref = new WeakReference ( obj ) ;
11+ let gcCount = 0 ;
12+ let maxGC = 10 ;
1213
13- setDeserializeMainFunction ( ( ) => {
14- obj = null ;
14+ function run ( ) {
1515 globalThis . gc ( ) ;
16-
1716 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+ }
1925 } ) ;
26+ }
27+
28+ setDeserializeMainFunction ( ( ) => {
29+ obj = null ;
30+ run ( ) ;
2031} ) ;
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ const isEnumerable = Function.call.bind(Object.prototype.propertyIsEnumerable);
1313// See: https://github.com/nodejs/node/issues/23862
1414
1515let d = domain . create ( ) ;
16+ let resourceGCed = false ; let domainGCed = false ; let
17+ emitterGCed = false ;
1618d . run ( ( ) => {
1719 const resource = new async_hooks . AsyncResource ( 'TestResource' ) ;
1820 const emitter = new EventEmitter ( ) ;
@@ -30,10 +32,17 @@ d.run(() => {
3032 // emitter → resource → async id ⇒ domain → emitter.
3133 // Make sure that all of these objects are released:
3234
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 ; } ) } ) ;
3638} ) ;
3739
3840d = 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 11// Flags: --expose-internals --expose-gc
22'use strict' ;
3- require ( '../common' ) ;
3+ const common = require ( '../common' ) ;
44const assert = require ( 'assert' ) ;
55const { internalBinding } = require ( 'internal/test/binding' ) ;
66const { WeakReference } = internalBinding ( 'util' ) ;
@@ -9,9 +9,11 @@ let obj = { hello: 'world' };
99const ref = new WeakReference ( obj ) ;
1010assert . strictEqual ( ref . get ( ) , obj ) ;
1111
12- setImmediate ( ( ) => {
12+ async function main ( ) {
1313 obj = null ;
14- global . gc ( ) ;
14+ await common . gcUntil (
15+ 'Reference is garbage collected' ,
16+ ( ) => ref . get ( ) === undefined ) ;
17+ }
1518
16- assert . strictEqual ( ref . get ( ) , undefined ) ;
17- } ) ;
19+ main ( ) ;
You can’t perform that action at this time.
0 commit comments