@@ -7,12 +7,16 @@ import {
77} from "@t3tools/contracts" ;
88import { describe , expect , it } from "@effect/vitest" ;
99import * as Cause from "effect/Cause" ;
10+ import * as Duration from "effect/Duration" ;
1011import * as Effect from "effect/Effect" ;
1112import * as Exit from "effect/Exit" ;
13+ import * as Fiber from "effect/Fiber" ;
1214import * as Option from "effect/Option" ;
1315import * as Queue from "effect/Queue" ;
16+ import * as Ref from "effect/Ref" ;
1417import * as Stream from "effect/Stream" ;
1518import * as SubscriptionRef from "effect/SubscriptionRef" ;
19+ import * as TestClock from "effect/testing/TestClock" ;
1620import { RpcClientError } from "effect/unstable/rpc" ;
1721import * as Socket from "effect/unstable/socket/Socket" ;
1822
@@ -30,6 +34,7 @@ import {
3034 makeEnvironmentServerConfigState ,
3135 isLegacyUpdateHandoffLoss ,
3236 matchesServerUpdateReadyEvent ,
37+ nudgeReconnectDuringUpdateRestart ,
3338 projectServerWelcome ,
3439 resolveServerConfigValue ,
3540 resolveServerUpdateProgressResult ,
@@ -71,6 +76,56 @@ function session(client: WsRpcProtocolClient): RpcSession {
7176 } ;
7277}
7378
79+ describe ( "update restart reconnect nudges" , ( ) => {
80+ it . effect ( "retries once per backoff entry instead of only the first" , ( ) =>
81+ Effect . gen ( function * ( ) {
82+ const retries = yield * Ref . make ( 0 ) ;
83+ const states = [
84+ { phase : "backoff" } ,
85+ { phase : "connecting" } ,
86+ { phase : "backoff" } ,
87+ { phase : "backoff" } ,
88+ ] ;
89+
90+ yield * nudgeReconnectDuringUpdateRestart ( {
91+ stateChanges : Stream . fromIterable ( states ) ,
92+ retryNow : Ref . update ( retries , ( count ) => count + 1 ) ,
93+ interval : Duration . zero ,
94+ } ) ;
95+
96+ // Three backoff entries, three nudges. The old one-shot behavior fired
97+ // once and then let the supervisor's ladder stretch to 16-second gaps.
98+ expect ( yield * Ref . get ( retries ) ) . toBe ( 3 ) ;
99+ } ) ,
100+ ) ;
101+
102+ it . effect ( "paces nudges so a fast-failing connection cannot spin" , ( ) =>
103+ Effect . gen ( function * ( ) {
104+ const retries = yield * Ref . make ( 0 ) ;
105+
106+ const fiber = yield * Effect . forkChild (
107+ nudgeReconnectDuringUpdateRestart ( {
108+ stateChanges : Stream . fromIterable ( [ { phase : "backoff" } , { phase : "backoff" } ] ) ,
109+ retryNow : Ref . update ( retries , ( count ) => count + 1 ) ,
110+ } ) ,
111+ { startImmediately : true } ,
112+ ) ;
113+
114+ // Each nudge waits out the interval first, so nothing fires immediately.
115+ yield * TestClock . adjust ( Duration . millis ( 999 ) ) ;
116+ expect ( yield * Ref . get ( retries ) ) . toBe ( 0 ) ;
117+
118+ yield * TestClock . adjust ( Duration . millis ( 1 ) ) ;
119+ expect ( yield * Ref . get ( retries ) ) . toBe ( 1 ) ;
120+
121+ yield * TestClock . adjust ( Duration . seconds ( 1 ) ) ;
122+ expect ( yield * Ref . get ( retries ) ) . toBe ( 2 ) ;
123+
124+ yield * Fiber . join ( fiber ) ;
125+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
126+ ) ;
127+ } ) ;
128+
74129describe ( "server state projection" , ( ) => {
75130 it ( "only treats a legacy transport interruption as an unacknowledged handoff" , ( ) => {
76131 expect ( isLegacyUpdateHandoffLoss ( Cause . interrupt ( 1 ) ) ) . toBe ( true ) ;
0 commit comments