@@ -7,15 +7,17 @@ import {
77} from "@t3tools/contracts" ;
88import { describe , expect , it } from "@effect/vitest" ;
99import * as Cause from "effect/Cause" ;
10+ import * as Clock from "effect/Clock" ;
1011import * as Deferred from "effect/Deferred" ;
1112import * as Effect from "effect/Effect" ;
1213import * as Fiber from "effect/Fiber" ;
1314import * as Option from "effect/Option" ;
1415import * as Queue from "effect/Queue" ;
1516import * as Ref from "effect/Ref" ;
17+ import * as Schema from "effect/Schema" ;
1618import * as Stream from "effect/Stream" ;
17- import * as TestClock from "effect/testing/TestClock" ;
1819import * as SubscriptionRef from "effect/SubscriptionRef" ;
20+ import * as TestClock from "effect/testing/TestClock" ;
1921import { RpcClientError } from "effect/rpc" ;
2022
2123import {
@@ -45,6 +47,8 @@ const INSTALL_DOWNLOADING: RelayClientInstallProgressEvent = {
4547 stage : "downloading" ,
4648} ;
4749
50+ const decodeNumber = Schema . decodeUnknownEffect ( Schema . Number ) ;
51+
4852function session ( client : WsRpcProtocolClient ) : RpcSession . RpcSession {
4953 return {
5054 client,
@@ -265,13 +269,145 @@ describe("environment RPC", () => {
265269 while ( subscriptions . length < 2 ) {
266270 yield * Queue . take ( subscriptionChanges ) ;
267271 }
272+ // The pending same-session retry must not resubscribe the replaced session.
273+ yield * TestClock . adjust ( "1 minute" ) ;
268274 yield * Fiber . interrupt ( subscriptionFiber ) ;
269275
270276 expect ( subscriptions ) . toEqual ( [ "first" , "second" ] ) ;
271277 expect ( yield * Ref . get ( retryCount ) ) . toBe ( 0 ) ;
272278 } ) ,
273279 ) ;
274280
281+ // The session stays healthy (`closed` never resolves), as when one stream chunk fails to decode,
282+ // so the supervisor will not replace it and only resubscribing restores delivery.
283+ const resumesOnTheSameSession = Effect . fn ( "TestEnvironmentRpc.resumesOnTheSameSession" ) (
284+ function * ( streamFailure : Stream . Stream < never , unknown > ) {
285+ const resumedValue = { source : "resumed" } as unknown as ServerLifecycleStreamEvent ;
286+ const firstSubscribed = yield * Deferred . make < void > ( ) ;
287+ let subscriptions = 0 ;
288+ const client = {
289+ [ WS_METHODS . subscribeServerLifecycle ] : ( ) => {
290+ subscriptions += 1 ;
291+ return subscriptions === 1
292+ ? Stream . fromEffect ( Deferred . succeed ( firstSubscribed , undefined ) ) . pipe (
293+ Stream . drain ,
294+ Stream . concat ( streamFailure ) ,
295+ )
296+ : Stream . make ( resumedValue ) . pipe ( Stream . concat ( Stream . never ) ) ;
297+ } ,
298+ } as unknown as WsRpcProtocolClient ;
299+ const { activeSession, retryCount, supervisor } = yield * makeHarness ( ) ;
300+
301+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( client ) ) ) ;
302+ const resultFiber = yield * subscribe ( WS_METHODS . subscribeServerLifecycle , { } ) . pipe (
303+ Stream . runHead ,
304+ Effect . timeoutOption ( "2 seconds" ) ,
305+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
306+ Effect . forkChild ,
307+ ) ;
308+ yield * Deferred . await ( firstSubscribed ) ;
309+ yield * TestClock . adjust ( "2 seconds" ) ;
310+
311+ expect (
312+ yield * Fiber . join ( resultFiber ) ,
313+ "durable subscription stopped delivering after its stream ended on a healthy session" ,
314+ ) . toEqual ( Option . some ( Option . some ( resumedValue ) ) ) ;
315+ expect ( subscriptions ) . toBe ( 2 ) ;
316+ expect ( yield * Ref . get ( retryCount ) ) . toBe ( 0 ) ;
317+ } ,
318+ ) ;
319+
320+ it . effect ( "resubscribes on the same session after a transport failure" , ( ) =>
321+ resumesOnTheSameSession (
322+ Stream . fail (
323+ new RpcClientError . RpcClientError ( {
324+ reason : new RpcClientError . RpcClientDefect ( {
325+ message : "stream failed" ,
326+ cause : new Error ( "stream failed" ) ,
327+ } ) ,
328+ } ) ,
329+ ) ,
330+ ) ,
331+ ) ;
332+
333+ it . effect ( "resubscribes on the same session after a stream chunk fails to decode" , ( ) =>
334+ // Mirrors RpcClient, which decodes each chunk with `Effect.orDie`.
335+ resumesOnTheSameSession (
336+ Stream . fromEffect ( decodeNumber ( "not a number" ) . pipe ( Effect . orDie ) ) . pipe ( Stream . drain ) ,
337+ ) ,
338+ ) ;
339+
340+ it . effect (
341+ "backs off a same-session stream that keeps failing to decode after its first value" ,
342+ ( ) =>
343+ Effect . gen ( function * ( ) {
344+ // Like the shell stream: a valid value (metadata) arrives, then a chunk fails to decode.
345+ const value = { source : "metadata" } as unknown as ServerLifecycleStreamEvent ;
346+ const decodeDefect = decodeNumber ( "not a number" ) . pipe ( Effect . orDie ) ;
347+ const subscriptions = yield * Queue . unbounded < {
348+ readonly session : "first" | "second" ;
349+ readonly at : number ;
350+ } > ( ) ;
351+ let secondSubscriptions = 0 ;
352+ const failingClient = ( name : "first" | "second" ) =>
353+ ( {
354+ [ WS_METHODS . subscribeServerLifecycle ] : ( ) =>
355+ Stream . unwrap (
356+ Effect . gen ( function * ( ) {
357+ yield * Queue . offer ( subscriptions , {
358+ session : name ,
359+ at : yield * Clock . currentTimeMillis ,
360+ } ) ;
361+ if ( name === "second" && ++ secondSubscriptions > 2 ) return Stream . never ;
362+ return Stream . make ( value ) . pipe (
363+ Stream . concat ( Stream . fromEffect ( decodeDefect ) . pipe ( Stream . drain ) ) ,
364+ ) ;
365+ } ) ,
366+ ) ,
367+ } ) as unknown as WsRpcProtocolClient ;
368+ const { activeSession, retryCount, supervisor } = yield * makeHarness ( ) ;
369+
370+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( failingClient ( "first" ) ) ) ) ;
371+ const subscriptionFiber = yield * subscribe (
372+ WS_METHODS . subscribeServerLifecycle ,
373+ { } ,
374+ {
375+ onExpectedFailure : ( ) => Effect . void ,
376+ retryExpectedFailureAfter : "250 millis" ,
377+ } ,
378+ ) . pipe (
379+ Stream . runDrain ,
380+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
381+ Effect . forkChild ,
382+ ) ;
383+
384+ const expectedFirstSessionDelays = [ 250 , 500 , 1_000 , 2_000 , 4_000 , 8_000 , 16_000 , 30_000 ] ;
385+ const firstSessionDelays : number [ ] = [ ] ;
386+ let previous = yield * Queue . take ( subscriptions ) ;
387+ for ( const delay of expectedFirstSessionDelays ) {
388+ yield * TestClock . adjust ( delay ) ;
389+ const next = yield * Queue . take ( subscriptions ) ;
390+ firstSessionDelays . push ( next . at - previous . at ) ;
391+ previous = next ;
392+ }
393+
394+ // A replacement session starts a fresh backoff.
395+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( failingClient ( "second" ) ) ) ) ;
396+ const replaced = yield * Queue . take ( subscriptions ) ;
397+ yield * TestClock . adjust ( 250 ) ;
398+ const retried = yield * Queue . take ( subscriptions ) ;
399+ yield * Fiber . interrupt ( subscriptionFiber ) ;
400+
401+ expect (
402+ firstSessionDelays ,
403+ "same-session decode retries reset to the base delay after every first value" ,
404+ ) . toEqual ( expectedFirstSessionDelays ) ;
405+ expect ( [ replaced . session , retried . session ] ) . toEqual ( [ "second" , "second" ] ) ;
406+ expect ( retried . at - replaced . at ) . toBe ( 250 ) ;
407+ expect ( yield * Ref . get ( retryCount ) ) . toBe ( 0 ) ;
408+ } ) ,
409+ ) ;
410+
275411 it . effect ( "surfaces domain subscription failures without reconnecting" , ( ) =>
276412 Effect . gen ( function * ( ) {
277413 const domainError = new Error ( "terminal subscription rejected" ) ;
0 commit comments