55} from "@t3tools/contracts" ;
66import { describe , expect , it } from "@effect/vitest" ;
77import * as Cause from "effect/Cause" ;
8+ import * as Deferred from "effect/Deferred" ;
89import * as Effect from "effect/Effect" ;
910import * as Exit from "effect/Exit" ;
1011import * as Fiber from "effect/Fiber" ;
@@ -25,7 +26,13 @@ import {
2526import * as EnvironmentSupervisor from "../connection/supervisor.ts" ;
2627import * as RpcSession from "../rpc/session.ts" ;
2728import type { WsRpcProtocolClient } from "../rpc/protocol.ts" ;
28- import { EnvironmentRpcRequestObserver , request , runStream , subscribe } from "./client.ts" ;
29+ import {
30+ EnvironmentRpcRequestObserver ,
31+ request ,
32+ runStream ,
33+ subscribe ,
34+ subscribeDynamic ,
35+ } from "./client.ts" ;
2936
3037const TARGET = new PrimaryConnectionTarget ( {
3138 environmentId : EnvironmentId . make ( "environment-1" ) ,
@@ -53,7 +60,9 @@ function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
5360 } ;
5461}
5562
56- const makeHarness = Effect . fn ( "TestEnvironmentRpc.makeHarness" ) ( function * ( ) {
63+ const makeHarness = Effect . fn ( "TestEnvironmentRpc.makeHarness" ) ( function * ( options ?: {
64+ readonly retryNow ?: Effect . Effect < void > ;
65+ } ) {
5766 const state = yield * SubscriptionRef . make < SupervisorConnectionState > ( AVAILABLE_CONNECTION_STATE ) ;
5867 const activeSession = yield * SubscriptionRef . make < Option . Option < RpcSession . RpcSession > > (
5968 Option . none ( ) ,
@@ -67,7 +76,7 @@ const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () {
6776 prepared,
6877 connect : Effect . void ,
6978 disconnect : Effect . void ,
70- retryNow : Ref . update ( retryCount , ( count ) => count + 1 ) ,
79+ retryNow : options ?. retryNow ?? Ref . update ( retryCount , ( count ) => count + 1 ) ,
7180 } satisfies EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ) ;
7281 return {
7382 activeSession,
@@ -307,6 +316,96 @@ describe("environment RPC", () => {
307316 } ) ,
308317 ) ;
309318
319+ it . effect ( "ignores a transport failure from a session that has already been replaced" , ( ) =>
320+ Effect . gen ( function * ( ) {
321+ const failingClient = {
322+ [ WS_METHODS . subscribeTerminalEvents ] : ( ) =>
323+ Stream . fail (
324+ new RpcClientError . RpcClientError ( {
325+ reason : new RpcClientError . RpcClientDefect ( {
326+ message : "stale socket closed" ,
327+ cause : new Error ( "stale socket closed" ) ,
328+ } ) ,
329+ } ) ,
330+ ) ,
331+ } as unknown as WsRpcProtocolClient ;
332+ const replacementClient = {
333+ [ WS_METHODS . subscribeTerminalEvents ] : ( ) => Stream . never ,
334+ } as unknown as WsRpcProtocolClient ;
335+ const staleSession = session ( failingClient ) ;
336+ const replacementSession = session ( replacementClient ) ;
337+ const { activeSession, retryCount, supervisor } = yield * makeHarness ( ) ;
338+
339+ const subscriptionFiber = yield * subscribeDynamic (
340+ WS_METHODS . subscribeTerminalEvents ,
341+ ( current ) =>
342+ current === staleSession
343+ ? SubscriptionRef . set ( activeSession , Option . some ( replacementSession ) ) . pipe (
344+ Effect . as ( { } ) ,
345+ )
346+ : Effect . succeed ( { } ) ,
347+ ) . pipe (
348+ Stream . runDrain ,
349+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
350+ Effect . forkChild ,
351+ ) ;
352+ yield * SubscriptionRef . set ( activeSession , Option . some ( staleSession ) ) ;
353+ for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
354+ yield * Effect . yieldNow ;
355+ }
356+ yield * Fiber . interrupt ( subscriptionFiber ) ;
357+
358+ expect ( yield * Ref . get ( retryCount ) ) . toBe ( 0 ) ;
359+ } ) ,
360+ ) ;
361+
362+ it . effect ( "allows recovery to retry when a same-session resubscribe interrupts the signal" , ( ) =>
363+ Effect . gen ( function * ( ) {
364+ const attempts = yield * Ref . make ( 0 ) ;
365+ const blockRecovery = yield * Deferred . make < void > ( ) ;
366+ const resubscribe = yield * Queue . unbounded < void > ( ) ;
367+ const failingClient = {
368+ [ WS_METHODS . subscribeTerminalEvents ] : ( ) =>
369+ Stream . fail (
370+ new RpcClientError . RpcClientError ( {
371+ reason : new RpcClientError . RpcClientDefect ( {
372+ message : "socket closed" ,
373+ cause : new Error ( "socket closed" ) ,
374+ } ) ,
375+ } ) ,
376+ ) ,
377+ } as unknown as WsRpcProtocolClient ;
378+ const { activeSession, supervisor } = yield * makeHarness ( {
379+ retryNow : Ref . update ( attempts , ( count ) => count + 1 ) . pipe (
380+ Effect . andThen ( Deferred . await ( blockRecovery ) ) ,
381+ ) ,
382+ } ) ;
383+
384+ const subscriptionFiber = yield * subscribe (
385+ WS_METHODS . subscribeTerminalEvents ,
386+ { } ,
387+ {
388+ resubscribe : Stream . fromQueue ( resubscribe ) ,
389+ } ,
390+ ) . pipe (
391+ Stream . runDrain ,
392+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
393+ Effect . forkChild ,
394+ ) ;
395+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( failingClient ) ) ) ;
396+ for ( let attempt = 0 ; attempt < 100 && ( yield * Ref . get ( attempts ) ) < 1 ; attempt += 1 ) {
397+ yield * Effect . yieldNow ;
398+ }
399+ yield * Queue . offer ( resubscribe , undefined ) ;
400+ for ( let attempt = 0 ; attempt < 100 && ( yield * Ref . get ( attempts ) ) < 2 ; attempt += 1 ) {
401+ yield * Effect . yieldNow ;
402+ }
403+ yield * Fiber . interrupt ( subscriptionFiber ) ;
404+
405+ expect ( yield * Ref . get ( attempts ) ) . toBe ( 2 ) ;
406+ } ) ,
407+ ) ;
408+
310409 it . effect ( "surfaces domain subscription failures without reconnecting" , ( ) =>
311410 Effect . gen ( function * ( ) {
312411 const domainError = new Error ( "terminal subscription rejected" ) ;
0 commit comments