11import * as NodeV8 from "node:v8" ;
2- import * as NodeVM from "node:vm" ;
32import { assert , it } from "@effect/vitest" ;
43import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
54import * as NodeServices from "@effect/platform-node/NodeServices" ;
@@ -16,9 +15,6 @@ import * as KeyValueStore from "effect/persistence/KeyValueStore";
1615import * as TestClock from "effect/testing/TestClock" ;
1716import * as PullRequestReadCache from "./PullRequestReadCache.ts" ;
1817
19- NodeV8 . setFlagsFromString ( "--expose-gc" ) ;
20- const collectGarbage = NodeVM . runInNewContext ( "gc" ) as ( ) => void ;
21-
2218const cacheLayer = ( directory : string ) =>
2319 PullRequestReadCache . make . pipe ( Effect . provide ( KeyValueStore . layerFileSystem ( directory ) ) ) ;
2420
@@ -203,13 +199,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
203199 it . effect ( "does not keep a caller's data alive after its reads and invalidations" , ( ) =>
204200 Effect . gen ( function * ( ) {
205201 const cache = yield * PullRequestReadCache . make ;
206- const sweeps : Array < WeakRef < object > > = [ ] ;
202+ class SweepData extends Map < string , number > { }
207203 // Like a sync sweep: an Effect.fn made per run that closes over that run's data.
208204 const sweep = Effect . fn ( "sweep" ) ( function * ( index : number ) {
209- const groups = new Map < string , number > (
210- Array . from ( { length : 1_000 } , ( _ , id ) => [ `group-${ id } ` , id ] ) ,
211- ) ;
212- sweeps . push ( new WeakRef ( groups ) ) ;
205+ const groups = new SweepData ( Array . from ( { length : 1_000 } , ( _ , id ) => [ `group-${ id } ` , id ] ) ) ;
213206 const read = Effect . fn ( "read" ) ( function * ( pr : string ) {
214207 const size = groups . size ;
215208 yield * cache . invalidate ( pr ) ;
@@ -219,10 +212,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
219212 yield * Effect . forEach ( [ "pr-1" , "pr-2" ] , read , { discard : true } ) ;
220213 } ) ;
221214 for ( let index = 0 ; index < 5 ; index ++ ) yield * sweep ( index ) ;
222- // WeakRef targets stay alive until the current job ends, so yield before collecting.
223- yield * Effect . yieldNow ;
224- collectGarbage ( ) ;
225- assert . strictEqual ( sweeps . filter ( ( sweepData ) => sweepData . deref ( ) !== undefined ) . length , 0 ) ;
215+ // Counts after a full GC. A WeakRef would keep its target alive until the job ends.
216+ assert . strictEqual ( NodeV8 . queryObjects ( SweepData , { format : "count" } ) , 0 ) ;
226217 } ) . pipe ( Effect . provide ( KeyValueStore . layerMemory ) ) ,
227218 ) ;
228219
0 commit comments