@@ -1012,28 +1012,182 @@ describe("EnvironmentSupervisor", () => {
10121012 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
10131013 ) ;
10141014
1015- it . effect ( "keeps a long-resume socket that delivers a frame during a slow host check" , ( ) =>
1015+ it . effect (
1016+ "keeps a long-resume socket that delivers a frame during a slow host check and closes the replacement" ,
1017+ ( ) =>
1018+ Effect . gen ( function * ( ) {
1019+ const frames = yield * Ref . make ( 0 ) ;
1020+ const replacementRequested = yield * Deferred . make < void > ( ) ;
1021+ const harness = yield * makeHarness ( {
1022+ prepare : ( _attempt , target ) => Effect . succeed ( preparedFor ( target ) ) ,
1023+ initialConfig : ( ) => Effect . succeed ( PROBE_CAPABLE_CONFIG ) ,
1024+ probe : ( attempt ) => ( attempt === 1 ? Effect . sleep ( "2500 millis" ) : Effect . void ) ,
1025+ ready : ( attempt ) =>
1026+ attempt === 2
1027+ ? Deferred . succeed ( replacementRequested , undefined ) . pipe ( Effect . andThen ( Effect . never ) )
1028+ : Effect . void ,
1029+ inboundFrames : Ref . get ( frames ) ,
1030+ checkHost : ( ) => Effect . sleep ( "2 seconds" ) . pipe ( Effect . as ( "answered" ) ) ,
1031+ } ) ;
1032+ const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
1033+ initiallyDesired : true ,
1034+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1035+
1036+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1037+ yield * harness . wake ( "application-active-long" ) ;
1038+ yield * TestClock . adjust ( "700 millis" ) ;
1039+ expect ( yield * Deferred . isDone ( replacementRequested ) ) . toBe ( true ) ;
1040+ yield * Ref . update ( frames , ( count ) => count + 1 ) ;
1041+ // The frame check polls every 100 ms, well before the probe answers.
1042+ yield * TestClock . adjust ( "100 millis" ) ;
1043+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1044+ yield * TestClock . adjust ( "5 seconds" ) ;
1045+
1046+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 2 ) ;
1047+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1048+ expect ( ( yield * SubscriptionRef . get ( supervisor . state ) ) . generation ) . toBe ( 1 ) ;
1049+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1050+ ) ;
1051+
1052+ const makeDeadRemoteSocketHarness = Effect . fn ( "test.makeDeadRemoteSocketHarness" ) ( function * (
1053+ replacementReady : (
1054+ requested : Deferred . Deferred < void > ,
1055+ ) => Effect . Effect < void , ConnectionAttemptError > ,
1056+ ) {
1057+ const replacementRequested = yield * Deferred . make < void > ( ) ;
1058+ const harness = yield * makeHarness ( {
1059+ prepare : ( _attempt , target ) => Effect . succeed ( preparedFor ( target ) ) ,
1060+ initialConfig : ( ) => Effect . succeed ( PROBE_CAPABLE_CONFIG ) ,
1061+ probe : ( attempt ) => ( attempt === 1 ? Effect . never : Effect . void ) ,
1062+ ready : ( attempt ) =>
1063+ attempt === 2
1064+ ? Deferred . succeed ( replacementRequested , undefined ) . pipe (
1065+ Effect . andThen ( replacementReady ( replacementRequested ) ) ,
1066+ )
1067+ : Effect . void ,
1068+ // A dead pooled connection hangs the host check along with the socket.
1069+ checkHost : ( ) => Effect . never ,
1070+ } ) ;
1071+ const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
1072+ initiallyDesired : true ,
1073+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1074+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1075+ return { harness, supervisor, replacementRequested } ;
1076+ } ) ;
1077+
1078+ it . effect (
1079+ "swaps to a replacement that is ready before the probe of a dead remote socket gives up" ,
1080+ ( ) =>
1081+ Effect . gen ( function * ( ) {
1082+ const ready = yield * Deferred . make < void > ( ) ;
1083+ const { harness, supervisor, replacementRequested } = yield * makeDeadRemoteSocketHarness (
1084+ ( ) => Deferred . await ( ready ) ,
1085+ ) ;
1086+
1087+ yield * harness . wake ( "application-active-long" ) ;
1088+ yield * TestClock . adjust ( "499 millis" ) ;
1089+ expect ( yield * Deferred . isDone ( replacementRequested ) ) . toBe ( false ) ;
1090+ yield * TestClock . adjust ( "1 millis" ) ;
1091+ expect ( yield * Deferred . isDone ( replacementRequested ) ) . toBe ( true ) ;
1092+ // The old session keeps serving until the replacement is ready.
1093+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1094+ phase : "connected" ,
1095+ generation : 1 ,
1096+ } ) ;
1097+
1098+ yield * Deferred . succeed ( ready , undefined ) ;
1099+ yield * awaitState (
1100+ supervisor . state ,
1101+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1102+ ) ;
1103+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
1104+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 2 ) ;
1105+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1106+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1107+ ) ;
1108+
1109+ it . effect ( "adopts a replacement that is still connecting when the probe gives up" , ( ) =>
1110+ Effect . gen ( function * ( ) {
1111+ const ready = yield * Deferred . make < void > ( ) ;
1112+ const { harness, supervisor, replacementRequested } = yield * makeDeadRemoteSocketHarness ( ( ) =>
1113+ Deferred . await ( ready ) ,
1114+ ) ;
1115+
1116+ yield * harness . wake ( "application-active-long" ) ;
1117+ yield * TestClock . adjust ( "1500 millis" ) ;
1118+ expect ( yield * Deferred . isDone ( replacementRequested ) ) . toBe ( true ) ;
1119+ yield * Deferred . succeed ( ready , undefined ) ;
1120+ yield * awaitState (
1121+ supervisor . state ,
1122+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1123+ ) ;
1124+
1125+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
1126+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 2 ) ;
1127+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1128+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1129+ ) ;
1130+
1131+ it . effect ( "leaves a dead remote socket to its probe when the replacement fails" , ( ) =>
1132+ Effect . gen ( function * ( ) {
1133+ const { harness, supervisor } = yield * makeDeadRemoteSocketHarness ( ( ) =>
1134+ Effect . fail ( transient ( ) ) ,
1135+ ) ;
1136+
1137+ yield * harness . wake ( "application-active-long" ) ;
1138+ yield * TestClock . adjust ( "1499 millis" ) ;
1139+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1140+ phase : "connected" ,
1141+ generation : 1 ,
1142+ } ) ;
1143+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1144+
1145+ yield * TestClock . adjust ( "1 millis" ) ;
1146+ yield * awaitState (
1147+ supervisor . state ,
1148+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1149+ ) ;
1150+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 3 ) ;
1151+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 2 ) ;
1152+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1153+ ) ;
1154+
1155+ it . effect . each ( [
1156+ { name : "LAN" , entry : LAN_ENTRY } ,
1157+ {
1158+ name : "loopback" ,
1159+ entry : {
1160+ ...TARGET_ENTRY ,
1161+ target : new PrimaryConnectionTarget ( {
1162+ ...TARGET ,
1163+ httpBaseUrl : "http://127.0.0.1:3773" ,
1164+ wsBaseUrl : "ws://127.0.0.1:3773" ,
1165+ } ) ,
1166+ } ,
1167+ } ,
1168+ ] ) ( "waits for the probe of a dead $name socket after a long resume" , ( { entry } ) =>
10161169 Effect . gen ( function * ( ) {
1017- const frames = yield * Ref . make ( 0 ) ;
10181170 const harness = yield * makeHarness ( {
10191171 prepare : ( _attempt , target ) => Effect . succeed ( preparedFor ( target ) ) ,
10201172 initialConfig : ( ) => Effect . succeed ( PROBE_CAPABLE_CONFIG ) ,
1021- probe : ( attempt ) => ( attempt === 1 ? Effect . sleep ( "2500 millis" ) : Effect . void ) ,
1022- inboundFrames : Ref . get ( frames ) ,
1023- checkHost : ( ) => Effect . sleep ( "2 seconds" ) . pipe ( Effect . as ( "answered" ) ) ,
1173+ probe : ( attempt ) => ( attempt === 1 ? Effect . never : Effect . void ) ,
1174+ checkHost : ( ) => Effect . succeed ( "silent" ) ,
10241175 } ) ;
1025- const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
1176+ const supervisor = yield * EnvironmentSupervisor . make ( entry , {
10261177 initiallyDesired : true ,
10271178 } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
10281179
10291180 yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
10301181 yield * harness . wake ( "application-active-long" ) ;
1031- yield * TestClock . adjust ( "700 millis" ) ;
1032- yield * Ref . update ( frames , ( count ) => count + 1 ) ;
1033- yield * TestClock . adjust ( "5 seconds" ) ;
1182+ yield * TestClock . adjust ( "1499 millis" ) ;
1183+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 1 ) ;
10341184
1035- expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
1036- expect ( ( yield * SubscriptionRef . get ( supervisor . state ) ) . generation ) . toBe ( 1 ) ;
1185+ yield * TestClock . adjust ( "1 millis" ) ;
1186+ yield * awaitState (
1187+ supervisor . state ,
1188+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1189+ ) ;
1190+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
10371191 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
10381192 ) ;
10391193
0 commit comments