@@ -28,62 +28,94 @@ const connectionState = (
2828 ...overrides ,
2929} ) ;
3030
31+ const makeCatalogHarness = Effect . fn ( "TestCatalog.makeHarness" ) ( function * ( ) {
32+ const state = yield * SubscriptionRef . make (
33+ connectionState ( { phase : "connected" , attempt : 1 , generation : 1 } ) ,
34+ ) ;
35+ const session = yield * SubscriptionRef . make ( Option . some ( { } as RpcSession . RpcSession ) ) ;
36+ const supervisor = {
37+ state,
38+ session,
39+ } as unknown as EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ;
40+ const followStream : EnvironmentRegistry . EnvironmentRegistry [ "Service" ] [ "followStream" ] = (
41+ _environmentId ,
42+ stream ,
43+ ) => Stream . provideService ( stream , EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ;
44+ const registry = EnvironmentRegistry . EnvironmentRegistry . of ( {
45+ followStream,
46+ } as unknown as EnvironmentRegistry . EnvironmentRegistry [ "Service" ] ) ;
47+ const catalog = createEnvironmentCatalogAtoms (
48+ Atom . runtime ( Layer . succeed ( EnvironmentRegistry . EnvironmentRegistry , registry ) ) ,
49+ ) ;
50+ const atom = catalog . sessionGenerationAtom ( ENVIRONMENT_ID ) ;
51+ const atoms = AtomRegistry . make ( ) ;
52+ const unmount = atoms . mount ( atom ) ;
53+ yield * Effect . addFinalizer ( ( ) =>
54+ Effect . sync ( ( ) => {
55+ unmount ( ) ;
56+ atoms . dispose ( ) ;
57+ } ) ,
58+ ) ;
59+ yield * Effect . yieldNow ;
60+ /** Moves the supervisor to `next`, with a new session object when one is established. */
61+ const generationAfter = (
62+ next : SupervisorConnectionState ,
63+ nextSession : Option . Option < RpcSession . RpcSession > = Option . none ( ) ,
64+ ) =>
65+ SubscriptionRef . set ( session , nextSession ) . pipe (
66+ Effect . andThen ( SubscriptionRef . set ( state , next ) ) ,
67+ Effect . andThen ( Effect . yieldNow ) ,
68+ Effect . map ( ( ) => atoms . get ( atom ) ) ,
69+ ) ;
70+ return { generation : ( ) => atoms . get ( atom ) , generationAfter } ;
71+ } ) ;
72+
73+ const lastFailure = new ConnectionTransientError ( { reason : "transport" , detail : "Down." } ) ;
74+ const newSession = ( ) => Option . some ( { } as RpcSession . RpcSession ) ;
75+
3176describe ( "environment catalog atoms" , ( ) => {
3277 it . effect ( "changes the session generation only when a new session comes up" , ( ) =>
3378 Effect . scoped (
3479 Effect . gen ( function * ( ) {
35- const state = yield * SubscriptionRef . make (
36- connectionState ( { phase : "connected" , attempt : 1 , generation : 1 } ) ,
37- ) ;
38- const session = yield * SubscriptionRef . make ( Option . some ( { } as RpcSession . RpcSession ) ) ;
39- const supervisor = {
40- state,
41- session,
42- } as unknown as EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ;
43- const followStream : EnvironmentRegistry . EnvironmentRegistry [ "Service" ] [ "followStream" ] = (
44- _environmentId ,
45- stream ,
46- ) => Stream . provideService ( stream , EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ;
47- const registry = EnvironmentRegistry . EnvironmentRegistry . of ( {
48- followStream,
49- } as unknown as EnvironmentRegistry . EnvironmentRegistry [ "Service" ] ) ;
50- const catalog = createEnvironmentCatalogAtoms (
51- Atom . runtime ( Layer . succeed ( EnvironmentRegistry . EnvironmentRegistry , registry ) ) ,
52- ) ;
53- const atom = catalog . sessionGenerationAtom ( ENVIRONMENT_ID ) ;
54- const atoms = AtomRegistry . make ( ) ;
55- const unmount = atoms . mount ( atom ) ;
56- yield * Effect . addFinalizer ( ( ) =>
57- Effect . sync ( ( ) => {
58- unmount ( ) ;
59- atoms . dispose ( ) ;
60- } ) ,
61- ) ;
62- const lastFailure = new ConnectionTransientError ( { reason : "transport" , detail : "Down." } ) ;
63- const generationAfter = ( next : SupervisorConnectionState ) =>
64- SubscriptionRef . set ( state , next ) . pipe (
65- Effect . andThen ( Effect . yieldNow ) ,
66- Effect . map ( ( ) => atoms . get ( atom ) ) ,
67- ) ;
68-
69- yield * Effect . yieldNow ;
70- expect ( atoms . get ( atom ) ) . toBe ( 1 ) ;
80+ const { generation, generationAfter } = yield * makeCatalogHarness ( ) ;
81+ const first = generation ( ) ;
7182 // A retry reports the generation it is attempting before it is established.
7283 expect (
7384 yield * generationAfter (
7485 connectionState ( { phase : "connecting" , stage : "opening" , generation : 2 , lastFailure } ) ,
7586 ) ,
76- ) . toBe ( 1 ) ;
87+ ) . toBe ( first ) ;
7788 expect (
7889 yield * generationAfter (
7990 connectionState ( { phase : "backoff" , generation : 1 , lastFailure, retryAt : 1 } ) ,
8091 ) ,
81- ) . toBe ( 1 ) ;
92+ ) . toBe ( first ) ;
8293 expect (
8394 yield * generationAfter (
8495 connectionState ( { phase : "connected" , attempt : 2 , generation : 2 } ) ,
96+ newSession ( ) ,
97+ ) ,
98+ ) . not . toBe ( first ) ;
99+ } ) ,
100+ ) ,
101+ ) ;
102+
103+ it . effect ( "changes when a replaced supervisor's first session reuses an old generation" , ( ) =>
104+ Effect . scoped (
105+ Effect . gen ( function * ( ) {
106+ const { generation, generationAfter } = yield * makeCatalogHarness ( ) ;
107+ const first = generation ( ) ;
108+ yield * generationAfter (
109+ connectionState ( { phase : "backoff" , generation : 1 , lastFailure, retryAt : 1 } ) ,
110+ ) ;
111+ // Re-pairing or editing a route installs a new supervisor, whose counter starts over.
112+ yield * generationAfter ( connectionState ( { phase : "available" , generation : 0 } ) ) ;
113+ expect (
114+ yield * generationAfter (
115+ connectionState ( { phase : "connected" , attempt : 1 , generation : 1 } ) ,
116+ newSession ( ) ,
85117 ) ,
86- ) . toBe ( 2 ) ;
118+ ) . not . toBe ( first ) ;
87119 } ) ,
88120 ) ,
89121 ) ;
0 commit comments