@@ -22,6 +22,7 @@ import {
2222} from "../connection/model.ts" ;
2323import * as EnvironmentRegistry from "../connection/registry.ts" ;
2424import * as EnvironmentSupervisor from "../connection/supervisor.ts" ;
25+ import { EnvironmentRpcUnavailableError } from "../rpc/client.ts" ;
2526import type * as RpcSession from "../rpc/session.ts" ;
2627import {
2728 environmentRpcKey ,
@@ -46,6 +47,8 @@ const QUERY_ENVIRONMENT = new PrimaryConnectionTarget({
4647 wsBaseUrl : "wss://query.example.test" ,
4748} ) ;
4849
50+ const QUERY_RPC_SESSION = { } as RpcSession . RpcSession ;
51+
4952class TestQueryError extends Schema . TaggedErrorClass < TestQueryError > ( ) ( "TestQueryError" , {
5053 message : Schema . String ,
5154} ) { }
@@ -78,19 +81,25 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
7881 execute : Effect . Effect < A , E > ,
7982) {
8083 const supervisorState = yield * SubscriptionRef . make ( queryConnectionState ( ) ) ;
84+ const supervisorSession = yield * SubscriptionRef . make ( Option . some ( QUERY_RPC_SESSION ) ) ;
8185 const supervisor = EnvironmentSupervisor . EnvironmentSupervisor . of ( {
8286 target : QUERY_ENVIRONMENT ,
8387 state : supervisorState ,
84- session : yield * SubscriptionRef . make < Option . Option < RpcSession . RpcSession > > ( Option . none ( ) ) ,
88+ session : supervisorSession ,
8589 prepared : yield * SubscriptionRef . make < Option . Option < PreparedConnection > > ( Option . none ( ) ) ,
8690 connect : Effect . void ,
8791 disconnect : Effect . void ,
8892 retryNow : Effect . void ,
8993 } satisfies EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ) ;
9094 const run : EnvironmentRegistry . EnvironmentRegistry [ "Service" ] [ "run" ] = ( _environmentId , effect ) =>
9195 Effect . provideService ( effect , EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ;
96+ const followStream : EnvironmentRegistry . EnvironmentRegistry [ "Service" ] [ "followStream" ] = (
97+ _environmentId ,
98+ stream ,
99+ ) => Stream . provideService ( stream , EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ;
92100 const environmentRegistry = EnvironmentRegistry . EnvironmentRegistry . of ( {
93101 run,
102+ followStream,
94103 stateChanges : ( ) => SubscriptionRef . changes ( supervisorState ) ,
95104 } as unknown as EnvironmentRegistry . EnvironmentRegistry [ "Service" ] ) ;
96105 const runtime = Atom . runtime (
@@ -104,6 +113,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
104113
105114 return {
106115 atom : family ( { environmentId : QUERY_ENVIRONMENT . environmentId , input : undefined } ) ,
116+ supervisorSession,
107117 supervisorState,
108118 } ;
109119} ) ;
@@ -268,17 +278,21 @@ describe("environment query lifecycle", () => {
268278 Effect . scoped (
269279 Effect . gen ( function * ( ) {
270280 const firstStarted = Latch . makeUnsafe ( ) ;
271- const interruptFirst = Latch . makeUnsafe ( ) ;
281+ const failFirst = Latch . makeUnsafe ( ) ;
272282 const firstSettled = Latch . makeUnsafe ( ) ;
283+ const unavailable = new EnvironmentRpcUnavailableError ( {
284+ environmentId : QUERY_ENVIRONMENT . environmentId ,
285+ message : "Query environment is not connected." ,
286+ } ) ;
273287 let executions = 0 ;
274288 const execute = Effect . suspend ( ( ) => {
275289 executions += 1 ;
276290 if ( executions > 1 ) {
277291 return Effect . succeed ( "recovered" ) ;
278292 }
279293 firstStarted . openUnsafe ( ) ;
280- return interruptFirst . await . pipe (
281- Effect . andThen ( Effect . interrupt ) ,
294+ return failFirst . await . pipe (
295+ Effect . andThen ( Effect . fail ( unavailable ) ) ,
282296 Effect . ensuring (
283297 Effect . sync ( ( ) => {
284298 firstSettled . openUnsafe ( ) ;
@@ -304,6 +318,14 @@ describe("environment query lifecycle", () => {
304318 ) ;
305319
306320 yield * firstStarted . await ;
321+ yield * SubscriptionRef . set ( harness . supervisorSession , Option . none ( ) ) ;
322+ yield * Effect . yieldNow ;
323+ failFirst . openUnsafe ( ) ;
324+ yield * firstSettled . await ;
325+ yield * Effect . yieldNow ;
326+
327+ expect ( observed . some ( AsyncResult . isFailure ) ) . toBe ( false ) ;
328+
307329 yield * SubscriptionRef . set (
308330 harness . supervisorState ,
309331 queryConnectionState ( { phase : "connecting" , stage : "preparing" } ) ,
@@ -322,12 +344,8 @@ describe("environment query lifecycle", () => {
322344 } ) ,
323345 ) ;
324346 yield * Effect . yieldNow ;
325- interruptFirst . openUnsafe ( ) ;
326- yield * firstSettled . await ;
327- yield * Effect . yieldNow ;
328-
329- expect ( observed . some ( AsyncResult . isFailure ) ) . toBe ( false ) ;
330347
348+ yield * SubscriptionRef . set ( harness . supervisorSession , Option . some ( QUERY_RPC_SESSION ) ) ;
331349 yield * SubscriptionRef . set (
332350 harness . supervisorState ,
333351 queryConnectionState ( { generation : 2 } ) ,
0 commit comments