@@ -8,12 +8,12 @@ import { describe, expect, it } from "@effect/vitest";
88import * as Cause from "effect/Cause" ;
99import * as Deferred from "effect/Deferred" ;
1010import * as Effect from "effect/Effect" ;
11- import * as Exit from "effect/Exit" ;
1211import * as Fiber from "effect/Fiber" ;
1312import * as Option from "effect/Option" ;
1413import * as Queue from "effect/Queue" ;
1514import * as Ref from "effect/Ref" ;
1615import * as Stream from "effect/Stream" ;
16+ import * as TestClock from "effect/testing/TestClock" ;
1717import * as SubscriptionRef from "effect/SubscriptionRef" ;
1818import { RpcClientError } from "effect/rpc" ;
1919
@@ -347,33 +347,43 @@ describe("environment RPC", () => {
347347 ) ;
348348
349349 it . effect . each ( [ "input" , "stream" ] as const ) (
350- "does not classify %s subscription defects as expected failures " ,
350+ "reports %s subscription defects and resubscribes on the next session " ,
351351 ( where ) =>
352352 Effect . gen ( function * ( ) {
353353 const defect = new Error ( "subscription invariant failed" ) ;
354354 let expectedFailureCount = 0 ;
355355 let inputs = 0 ;
356356 let streams = 0 ;
357357 const observedDefects : unknown [ ] = [ ] ;
358- const client = {
358+ const defectObserved = yield * Deferred . make < void > ( ) ;
359+ const resubscribed = yield * Deferred . make < void > ( ) ;
360+ const firstClient = {
359361 [ WS_METHODS . subscribeTerminalMetadata ] : ( ) => {
360362 streams += 1 ;
361363 return where === "stream" ? Stream . die ( defect ) : Stream . never ;
362364 } ,
363365 } as unknown as WsRpcProtocolClient ;
366+ const secondClient = {
367+ [ WS_METHODS . subscribeTerminalMetadata ] : ( ) => {
368+ Deferred . doneUnsafe ( resubscribed , Effect . void ) ;
369+ return Stream . never ;
370+ } ,
371+ } as unknown as WsRpcProtocolClient ;
364372 const { activeSession, supervisor } = yield * makeHarness ( ) ;
365373
366- yield * SubscriptionRef . set ( activeSession , Option . some ( session ( client ) ) ) ;
367- const exit = yield * subscribeDynamicWithSession (
374+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( firstClient ) ) ) ;
375+ const subscriptionFiber = yield * subscribeDynamicWithSession (
368376 WS_METHODS . subscribeTerminalMetadata ,
369377 ( ) =>
370- Effect . sync ( ( ) => {
378+ Effect . suspend ( ( ) => {
371379 inputs += 1 ;
372- } ) . pipe ( Effect . andThen ( where === "input" ? Effect . die ( defect ) : Effect . succeed ( { } ) ) ) ,
380+ return where === "input" && inputs === 1 ? Effect . die ( defect ) : Effect . succeed ( { } ) ;
381+ } ) ,
373382 {
374383 onDefect : ( cause ) =>
375384 Effect . sync ( ( ) => {
376385 observedDefects . push ( Cause . squash ( cause ) ) ;
386+ Deferred . doneUnsafe ( defectObserved , Effect . void ) ;
377387 } ) ,
378388 onExpectedFailure : ( ) =>
379389 Effect . sync ( ( ) => {
@@ -384,18 +394,23 @@ describe("environment RPC", () => {
384394 ) . pipe (
385395 Stream . runDrain ,
386396 Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
387- Effect . exit ,
397+ Effect . forkChild ,
388398 ) ;
399+ yield * Deferred . await ( defectObserved ) ;
389400
390- expect ( Exit . isFailure ( exit ) ) . toBe ( true ) ;
391- if ( Exit . isFailure ( exit ) ) {
392- expect ( Cause . hasDies ( exit . cause ) ) . toBe ( true ) ;
393- expect ( Cause . squash ( exit . cause ) ) . toBe ( defect ) ;
394- }
395401 expect ( inputs ) . toBe ( 1 ) ;
396402 expect ( streams ) . toBe ( where === "input" ? 0 : 1 ) ;
397403 expect ( expectedFailureCount ) . toBe ( 0 ) ;
398404 expect ( observedDefects ) . toEqual ( [ defect ] ) ;
405+
406+ // Let the defect settle before the next session arrives, as it does on a real device.
407+ yield * TestClock . adjust ( "1 second" ) ;
408+ expect ( subscriptionFiber . pollUnsafe ( ) ) . toBeUndefined ( ) ;
409+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( secondClient ) ) ) ;
410+ yield * Deferred . await ( resubscribed ) ;
411+ yield * Fiber . interrupt ( subscriptionFiber ) ;
412+
413+ expect ( inputs ) . toBe ( 2 ) ;
399414 } ) ,
400415 ) ;
401416} ) ;
0 commit comments