@@ -75,7 +75,43 @@ async function runAndBreathe(fn, repeat, waitTime = 20) {
7575  } 
7676} 
7777
78+ // This requires --expose-internals 
79+ async  function  checkIfCollectableByCounting ( fn ,  klass ,  count ,  waitTime  =  20 )  { 
80+   const  {  internalBinding }  =  require ( 'internal/test/binding' ) ; 
81+   const  {  countObjectsWithPrototype }  =  internalBinding ( 'heap_utils' ) ; 
82+   const  {  prototype,  name }  =  klass ; 
83+   let  initialCount  =  countObjectsWithPrototype ( prototype ) ; 
84+   console . log ( `Initial count of ${ name } ${ initialCount }  ) ; 
85+   let  totalCreated  =  0 ; 
86+   for  ( let  i  =  0 ;  i  <  count ;  ++ i )  { 
87+     const  created  =  await  fn ( i ) ; 
88+     totalCreated  +=  created ; 
89+     console . log ( `#${ i } ${ created } ${ name } ${ totalCreated }  ) ; 
90+     await  wait ( waitTime ) ;   // give GC some breathing room. 
91+     const  currentCount  =  countObjectsWithPrototype ( prototype ) ; 
92+     const  collected  =  totalCreated  -  ( currentCount  -  initialCount ) ; 
93+     console . log ( `#${ i } ${ currentCount } ${ name } ${ collected }  ) ; 
94+     if  ( collected  >  0  )  { 
95+       console . log ( `Detected ${ collected } ${ name }  ) ; 
96+       return ; 
97+     } 
98+   } 
99+ 
100+   await  wait ( waitTime ) ;   // give GC some breathing room. 
101+   const  currentCount  =  countObjectsWithPrototype ( prototype ) ; 
102+   const  collected  =  totalCreated  -  ( currentCount  -  initialCount ) ; 
103+   console . log ( `Last count: counted ${ currentCount } ${ name } ${ collected }  ) ; 
104+   // Some objects with the prototype can be collected. 
105+   if  ( collected  >  0 )  { 
106+     console . log ( `Detected ${ collected } ${ name }  ) ; 
107+     return ; 
108+   } 
109+ 
110+   throw  new  Error ( `${ name }  ) ; 
111+ } 
112+ 
78113module . exports  =  { 
79114  checkIfCollectable, 
80115  runAndBreathe, 
116+   checkIfCollectableByCounting, 
81117} ; 
0 commit comments