@@ -10,6 +10,7 @@ import * as Effect from "effect/Effect";
1010import * as Fiber from "effect/Fiber" ;
1111import * as Layer from "effect/Layer" ;
1212import * as Option from "effect/Option" ;
13+ import * as Queue from "effect/Queue" ;
1314import * as Random from "effect/Random" ;
1415import * as Ref from "effect/Ref" ;
1516import * as Stream from "effect/Stream" ;
@@ -1324,6 +1325,103 @@ describe("EnvironmentSupervisor", () => {
13241325 } ) ,
13251326 ) ;
13261327
1328+ it . effect (
1329+ "keeps a live session when the automatic follow-up to a slow foreground probe answers" ,
1330+ ( ) =>
1331+ Effect . gen ( function * ( ) {
1332+ const probeCount = yield * Ref . make ( 0 ) ;
1333+ const probeStarted = yield * Queue . unbounded < number > ( ) ;
1334+ const harness = yield * makeHarness ( {
1335+ probe : ( ) =>
1336+ Ref . updateAndGet ( probeCount , ( count ) => count + 1 ) . pipe (
1337+ Effect . tap ( ( call ) => Queue . offer ( probeStarted , call ) ) ,
1338+ Effect . flatMap ( ( call ) => ( call === 1 ? Effect . never : Effect . void ) ) ,
1339+ ) ,
1340+ } ) ;
1341+ const supervisor = yield * EnvironmentSupervisor . make ( TARGET_ENTRY , {
1342+ initiallyDesired : true ,
1343+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1344+
1345+ yield * awaitState (
1346+ supervisor . state ,
1347+ ( state ) => state . phase === "connected" && state . generation === 1 ,
1348+ ) ;
1349+ yield * harness . wake ( "application-active" ) ;
1350+ expect ( yield * Queue . take ( probeStarted ) ) . toBe ( 1 ) ;
1351+ // A busy server misses the foreground probe deadline once while the
1352+ // socket stays open; the supervisor asks again on its own.
1353+ yield * TestClock . adjust ( "15 seconds" ) ;
1354+ expect ( yield * Queue . take ( probeStarted ) ) . toBe ( 2 ) ;
1355+ yield * TestClock . adjust ( "30 seconds" ) ;
1356+
1357+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 0 ) ;
1358+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
1359+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1360+ phase : "connected" ,
1361+ generation : 1 ,
1362+ } ) ;
1363+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1364+ ) ;
1365+
1366+ it . effect ( "replaces a session when the automatic follow-up probe also goes unanswered" , ( ) =>
1367+ Effect . gen ( function * ( ) {
1368+ const probeCount = yield * Ref . make ( 0 ) ;
1369+ const harness = yield * makeHarness ( {
1370+ probe : ( ) =>
1371+ Ref . update ( probeCount , ( count ) => count + 1 ) . pipe ( Effect . andThen ( Effect . never ) ) ,
1372+ } ) ;
1373+ const supervisor = yield * EnvironmentSupervisor . make ( TARGET_ENTRY , {
1374+ initiallyDesired : true ,
1375+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1376+
1377+ yield * awaitState (
1378+ supervisor . state ,
1379+ ( state ) => state . phase === "connected" && state . generation === 1 ,
1380+ ) ;
1381+ // One foreground event and no other signal: the follow-up's miss alone
1382+ // replaces the session.
1383+ yield * harness . wake ( "application-active" ) ;
1384+ yield * TestClock . adjust ( "15 seconds" ) ;
1385+ yield * TestClock . adjust ( "15 seconds" ) ;
1386+ yield * awaitState (
1387+ supervisor . state ,
1388+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1389+ ) ;
1390+
1391+ expect ( yield * Ref . get ( probeCount ) ) . toBe ( 2 ) ;
1392+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1393+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1394+ ) ;
1395+
1396+ it . effect ( "replaces a session when a retry lands late in a slow foreground probe" , ( ) =>
1397+ Effect . gen ( function * ( ) {
1398+ const probeStarted = yield * Deferred . make < void > ( ) ;
1399+ const harness = yield * makeHarness ( {
1400+ probe : ( ) => Deferred . succeed ( probeStarted , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ,
1401+ } ) ;
1402+ const supervisor = yield * EnvironmentSupervisor . make ( TARGET_ENTRY , {
1403+ initiallyDesired : true ,
1404+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1405+
1406+ yield * awaitState (
1407+ supervisor . state ,
1408+ ( state ) => state . phase === "connected" && state . generation === 1 ,
1409+ ) ;
1410+ yield * harness . wake ( "application-active" ) ;
1411+ yield * Deferred . await ( probeStarted ) ;
1412+ yield * TestClock . adjust ( "13 seconds" ) ;
1413+ // The retry's own deadline falls after the foreground one, but it still
1414+ // asks for a definite answer, so the first miss is not forgiven.
1415+ yield * supervisor . retryNow ;
1416+ yield * TestClock . adjust ( "5 seconds" ) ;
1417+ yield * awaitState (
1418+ supervisor . state ,
1419+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1420+ ) ;
1421+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1422+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1423+ ) ;
1424+
13271425 it . effect ( "does not churn a healthy session when credentials change" , ( ) =>
13281426 Effect . gen ( function * ( ) {
13291427 const harness = yield * makeHarness ( ) ;
0 commit comments