44 DesktopTelemetryControlMessage ,
55} from "@t3tools/contracts" ;
66import { assert , describe , it } from "@effect/vitest" ;
7- import * as Cause from "effect/Cause" ;
87import * as Deferred from "effect/Deferred" ;
98import * as Duration from "effect/Duration" ;
109import * as Effect from "effect/Effect" ;
@@ -1507,18 +1506,17 @@ describe("DesktopBackendManager", () => {
15071506 ) ,
15081507 ) ;
15091508
1510- it . effect ( "stopAllPoolInstances stops all backends with timeout " , ( ) =>
1509+ it . effect ( "stopAllPoolInstances bounds the quit finalizer when backends hang " , ( ) =>
15111510 Effect . scoped (
15121511 Effect . gen ( function * ( ) {
1513- // Regression test: validates that stopAllPoolInstances applies a
1514- // timeout to each stop call, bounding the wait time even when
1515- // backends hang during teardown. This directly tests the actual
1516- // implementation used by the quit path .
1517- const stopped : string [ ] = [ ] ;
1518- const teardownStarted = yield * Deferred . make < void > ( ) ;
1512+ // Each backend's process-scope finalizer reports when it starts and
1513+ // when it finishes, keyed by instance name, so the test can prove
1514+ // both backends reached each milestone instead of inferring it from
1515+ // a shared flag or a clock advance .
1516+ const teardownStarted = yield * Queue . unbounded < string > ( ) ;
1517+ const teardownFinished = yield * Queue . unbounded < string > ( ) ;
15191518 const allowTeardown = yield * Deferred . make < void > ( ) ;
15201519
1521- // Create two instances to simulate pool.list returning multiple backends
15221520 const makeInstance = ( name : string ) =>
15231521 makeTestInstance ( {
15241522 spawnerLayer : Layer . succeed (
@@ -1528,19 +1526,13 @@ describe("DesktopBackendManager", () => {
15281526 const scope = yield * Scope . Scope ;
15291527 yield * Scope . addFinalizer (
15301528 scope ,
1531- Deferred . succeed ( teardownStarted , undefined ) . pipe (
1529+ Queue . offer ( teardownStarted , name ) . pipe (
15321530 Effect . andThen ( Deferred . await ( allowTeardown ) ) ,
1533- Effect . andThen (
1534- Effect . sync ( ( ) => {
1535- stopped . push ( name ) ;
1536- } ) ,
1537- ) ,
1531+ Effect . andThen ( Queue . offer ( teardownFinished , name ) ) ,
15381532 Effect . asVoid ,
15391533 ) ,
15401534 ) ;
1541- return makeProcess ( {
1542- exitCode : Effect . never ,
1543- } ) ;
1535+ return makeProcess ( { exitCode : Effect . never } ) ;
15441536 } ) ,
15451537 ) ,
15461538 ) ,
@@ -1553,45 +1545,35 @@ describe("DesktopBackendManager", () => {
15531545 yield * instance1 . start ;
15541546 yield * instance2 . start ;
15551547
1556- // Create a mock pool with the instances we just started
15571548 const mockPool = Layer . succeed ( DesktopBackendPool . DesktopBackendPool , {
15581549 list : Effect . succeed ( [ instance1 , instance2 ] ) ,
1559- get : ( id : DesktopBackendPool . BackendInstanceId ) => Effect . succeed ( Option . none ( ) ) ,
1560- primary : Effect . failCause ( Cause . die ( new Error ( "primary not implemented" ) ) ) ,
1561- register : ( ) =>
1562- Effect . failCause (
1563- Cause . die (
1564- new DesktopBackendPool . DesktopBackendPoolInstanceAlreadyRegisteredError ( {
1565- id : "test" ,
1566- } ) ,
1567- ) ,
1568- ) ,
1569- unregister : ( ) =>
1570- Effect . failCause (
1571- Cause . die ( new DesktopBackendPool . DesktopBackendPoolCannotUnregisterPrimaryError ( ) ) ,
1572- ) ,
1550+ get : ( ) => Effect . succeed ( Option . none ( ) ) ,
1551+ primary : Effect . die ( new Error ( "primary not implemented" ) ) ,
1552+ register : ( ) => Effect . die ( new Error ( "register not implemented" ) ) ,
1553+ unregister : ( ) => Effect . die ( new Error ( "unregister not implemented" ) ) ,
15731554 } ) ;
15741555
1575- // Call the actual stopAllPoolInstances function with the mock pool provided as a service
1576- const quitFiber = yield * DesktopApp . stopAllPoolInstances ( ) . pipe (
1577- Effect . provide ( mockPool ) ,
1578- Effect . forkChild ,
1579- ) ;
1556+ // Mirror the quit path: register stopAllPoolInstances as a scope
1557+ // finalizer and let the scope close run it, rather than calling it
1558+ // as an ordinary interruptible effect.
1559+ const quitFiber = yield * Effect . scoped (
1560+ Effect . addFinalizer ( ( ) => DesktopApp . stopAllPoolInstances ( ) ) ,
1561+ ) . pipe ( Effect . provide ( mockPool ) , Effect . forkChild ) ;
15801562
1581- // Wait for teardown to start, then verify quit completes within timeout
1582- yield * Deferred . await ( teardownStarted ) . pipe ( Effect . timeout ( "1 second" ) ) ;
1583- yield * TestClock . adjust ( Duration . seconds ( 5 ) ) ;
1563+ const started = yield * Queue . takeN ( teardownStarted , 2 ) ;
1564+ assert . deepEqual ( started . toSorted ( ) , [ "instance1" , "instance2" ] ) ;
15841565
1585- // stopAllPoolInstances should complete after timeout, even though teardown is hanging
1586- yield * Fiber . join ( quitFiber ) . pipe ( Effect . timeout ( "1 second" ) ) ;
1587- assert . equal ( stopped . length , 0 , "teardown should not have completed yet" ) ;
1566+ // Both backends are now hung in teardown. Advancing past the 5s
1567+ // budget must let the quit finalizer return without them.
1568+ yield * TestClock . adjust ( Duration . seconds ( 5 ) ) ;
1569+ yield * Fiber . join ( quitFiber ) ;
1570+ assert . equal ( yield * Queue . size ( teardownFinished ) , 0 ) ;
15881571
1589- // Let teardown finish
1572+ // The timed-out closes keep running in the background and finish
1573+ // once the backends unblock.
15901574 yield * Deferred . succeed ( allowTeardown , undefined ) ;
1591- yield * TestClock . adjust ( Duration . millis ( 100 ) ) ;
1592-
1593- // Now both should have stopped
1594- assert . equal ( stopped . length , 2 , "both instances should have stopped" ) ;
1575+ const finished = yield * Queue . takeN ( teardownFinished , 2 ) ;
1576+ assert . deepEqual ( finished . toSorted ( ) , [ "instance1" , "instance2" ] ) ;
15951577 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
15961578 ) ,
15971579 ) ;
0 commit comments