@@ -1097,40 +1097,183 @@ describe("EnvironmentSupervisor", () => {
10971097 } ) ,
10981098 ) ;
10991099
1100- it . effect ( "renews a relay connection before its DPoP access token expires " , ( ) =>
1100+ it . effect ( "hands off relay authorization after the replacement session is ready " , ( ) =>
11011101 Effect . gen ( function * ( ) {
11021102 const tokenLifetimeMs = DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS * 2 ;
1103+ const replacementStarted = yield * Deferred . make < void > ( ) ;
1104+ const releaseReplacement = yield * Deferred . make < void > ( ) ;
11031105 const harness = yield * makeHarness ( {
11041106 prepare : ( attempt ) =>
1105- Effect . succeed ( {
1106- ...PREPARED_CONNECTION ,
1107- target : RELAY_TARGET ,
1108- httpAuthorization : {
1109- _tag : "Dpop" ,
1110- accessToken : `access-token-${ attempt } ` ,
1111- expiresAtEpochMs : tokenLifetimeMs * attempt ,
1112- } ,
1113- } ) ,
1107+ attempt === 2
1108+ ? Effect . fail ( transient ( "Authorization refresh failed." ) )
1109+ : Effect . succeed ( {
1110+ ...PREPARED_CONNECTION ,
1111+ target : RELAY_TARGET ,
1112+ httpAuthorization : {
1113+ _tag : "Dpop" ,
1114+ accessToken : `access-token-${ attempt } ` ,
1115+ expiresAtEpochMs : tokenLifetimeMs * attempt ,
1116+ } ,
1117+ } ) ,
1118+ ready : ( attempt ) =>
1119+ attempt === 2
1120+ ? Deferred . succeed ( replacementStarted , undefined ) . pipe (
1121+ Effect . andThen ( Deferred . await ( releaseReplacement ) ) ,
1122+ )
1123+ : Effect . void ,
11141124 } ) ;
11151125 const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
11161126 initiallyDesired : true ,
11171127 } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
11181128
11191129 yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1130+ const firstSession = Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ;
1131+ const firstPrepared = Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . prepared ) ) ;
11201132 yield * TestClock . adjust ( DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS - 1 ) ;
11211133 expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
11221134
11231135 yield * TestClock . adjust ( 1 ) ;
1136+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
1137+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
1138+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 0 ) ;
1139+ yield * Effect . yieldNow ;
1140+
1141+ yield * TestClock . adjust ( "2999 millis" ) ;
1142+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
1143+ yield * TestClock . adjust ( "1 milli" ) ;
1144+ yield * Deferred . await ( replacementStarted ) ;
1145+
1146+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 3 ) ;
1147+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 2 ) ;
1148+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 0 ) ;
1149+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1150+ phase : "connected" ,
1151+ generation : 1 ,
1152+ } ) ;
1153+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( firstSession ) ;
1154+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . prepared ) ) ) . toBe (
1155+ firstPrepared ,
1156+ ) ;
1157+
1158+ yield * Deferred . succeed ( releaseReplacement , undefined ) ;
11241159 yield * awaitState (
11251160 supervisor . state ,
11261161 ( state ) => state . phase === "connected" && state . generation === 2 ,
11271162 ) ;
11281163
1129- expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 2 ) ;
11301164 expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1165+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . not . toBe (
1166+ firstSession ,
1167+ ) ;
11311168 expect (
11321169 Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . prepared ) ) . httpAuthorization ,
1133- ) . toMatchObject ( { accessToken : "access-token-2" } ) ;
1170+ ) . toMatchObject ( { accessToken : "access-token-3" } ) ;
1171+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1172+ ) ;
1173+
1174+ it . effect ( "cleans up timed-out replacements and an interrupted retry" , ( ) =>
1175+ Effect . gen ( function * ( ) {
1176+ const tokenLifetimeMs = DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS * 2 ;
1177+ const replacementStarted = yield * Deferred . make < void > ( ) ;
1178+ const retryStarted = yield * Deferred . make < void > ( ) ;
1179+ const harness = yield * makeHarness ( {
1180+ prepare : ( attempt ) =>
1181+ Effect . succeed ( {
1182+ ...PREPARED_CONNECTION ,
1183+ target : RELAY_TARGET ,
1184+ httpAuthorization : {
1185+ _tag : "Dpop" ,
1186+ accessToken : `access-token-${ attempt } ` ,
1187+ expiresAtEpochMs : tokenLifetimeMs * attempt ,
1188+ } ,
1189+ } ) ,
1190+ ready : ( attempt ) => {
1191+ if ( attempt === 2 ) {
1192+ return Deferred . succeed ( replacementStarted , undefined ) . pipe (
1193+ Effect . andThen ( Effect . never ) ,
1194+ ) ;
1195+ }
1196+ if ( attempt === 3 ) {
1197+ return Deferred . succeed ( retryStarted , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ;
1198+ }
1199+ return Effect . void ;
1200+ } ,
1201+ } ) ;
1202+ const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
1203+ initiallyDesired : true ,
1204+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1205+
1206+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1207+ const firstSession = Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ;
1208+ yield * TestClock . adjust ( DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS ) ;
1209+ yield * Deferred . await ( replacementStarted ) ;
1210+
1211+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( firstSession ) ;
1212+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 0 ) ;
1213+
1214+ yield * TestClock . adjust ( "15 seconds" ) ;
1215+ yield * Effect . yieldNow ;
1216+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1217+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( firstSession ) ;
1218+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1219+ phase : "connected" ,
1220+ generation : 1 ,
1221+ } ) ;
1222+
1223+ yield * TestClock . adjust ( "3 seconds" ) ;
1224+ yield * Deferred . await ( retryStarted ) ;
1225+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 3 ) ;
1226+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1227+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( firstSession ) ;
1228+
1229+ yield * supervisor . disconnect ;
1230+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "available" ) ;
1231+
1232+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 3 ) ;
1233+ expect ( Option . isNone ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( true ) ;
1234+ expect ( Option . isNone ( yield * SubscriptionRef . get ( supervisor . prepared ) ) ) . toBe ( true ) ;
1235+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1236+ ) ;
1237+
1238+ it . effect ( "does not keep a relay session past its authorization expiry" , ( ) =>
1239+ Effect . gen ( function * ( ) {
1240+ const tokenLifetimeMs = DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS * 2 ;
1241+ const harness = yield * makeHarness ( {
1242+ prepare : ( attempt ) =>
1243+ attempt === 1
1244+ ? Effect . succeed ( {
1245+ ...PREPARED_CONNECTION ,
1246+ target : RELAY_TARGET ,
1247+ httpAuthorization : {
1248+ _tag : "Dpop" ,
1249+ accessToken : "access-token-1" ,
1250+ expiresAtEpochMs : tokenLifetimeMs ,
1251+ } ,
1252+ } )
1253+ : Effect . fail ( transient ( "Authorization refresh failed." ) ) ,
1254+ } ) ;
1255+ const supervisor = yield * EnvironmentSupervisor . make ( RELAY_ENTRY , {
1256+ initiallyDesired : true ,
1257+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1258+
1259+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1260+ yield * TestClock . adjust ( DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS ) ;
1261+ yield * Effect . yieldNow ;
1262+ for ( const delay of [ 3_000 , 4_000 , 8_000 , 16_000 , 16_000 , 13_000 ] ) {
1263+ yield * TestClock . adjust ( delay ) ;
1264+ yield * Effect . yieldNow ;
1265+ }
1266+
1267+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "backoff" ) ;
1268+
1269+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
1270+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 1 ) ;
1271+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1272+ phase : "backoff" ,
1273+ generation : 1 ,
1274+ } ) ;
1275+ expect ( Option . isNone ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( true ) ;
1276+ expect ( Option . isNone ( yield * SubscriptionRef . get ( supervisor . prepared ) ) ) . toBe ( true ) ;
11341277 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
11351278 ) ;
11361279
0 commit comments