@@ -3,7 +3,8 @@ import * as Context from "effect/Context";
33import * as Effect from "effect/Effect" ;
44import * as Layer from "effect/Layer" ;
55import * as Semaphore from "effect/Semaphore" ;
6- import { PgDialect } from "drizzle-orm/pg-core" ;
6+ import { PGlite } from "@electric-sql/pglite" ;
7+ import { drizzle } from "drizzle-orm/pglite" ;
78
89import * as RelayDb from "../db.ts" ;
910import { relayManagedEndpointAllocations } from "../persistence/schema.ts" ;
@@ -14,7 +15,7 @@ class FakeTransaction extends Context.Service<FakeTransaction, { holdsLock: bool
1415) { }
1516
1617// Models Postgres just enough: the advisory lock only works inside a transaction and is held until it ends.
17- function makeFakeDb ( reserved : ReadonlyArray < unknown > , countConditions : Array < unknown > = [ ] ) {
18+ function makeFakeDb ( reserved : ReadonlyArray < unknown > ) {
1819 const userLock = Semaphore . makeUnsafe ( 1 ) ;
1920 return {
2021 $client : {
@@ -40,11 +41,10 @@ function makeFakeDb(reserved: ReadonlyArray<unknown>, countConditions: Array<unk
4041 } ) ,
4142 select : ( ) => ( {
4243 from : ( table : unknown ) => ( {
43- where : ( condition : unknown ) => {
44+ where : ( ) => {
4445 if ( table !== relayManagedEndpointAllocations ) {
4546 return { limit : ( ) => Effect . succeed ( [ ] ) } ;
4647 }
47- countConditions . push ( condition ) ;
4848 return Effect . yieldNow . pipe ( Effect . as ( [ { activeTunnels : reserved . length } ] ) ) ;
4949 } ,
5050 } ) ,
@@ -87,29 +87,140 @@ describe("ManagedTunnelLimits", () => {
8787 ) ;
8888 } ) ;
8989
90- it . effect ( "pending reservations stop counting once released or withdrawn" , ( ) => {
91- const countConditions : Array < unknown > = [ ] ;
92-
93- return Effect . gen ( function * ( ) {
94- const limits = yield * ManagedTunnelLimits . ManagedTunnelLimits ;
95- yield * limits . withCapacity ( { userId : "user-1" , environmentId : "environment-1" } , Effect . void ) ;
96-
97- expect ( countConditions ) . toHaveLength ( 1 ) ;
98- const query = new PgDialect ( ) . sqlToQuery ( countConditions [ 0 ] as never ) ;
99- // claimRelease writes updatedAt = tunnelReleasedAt; a later reserve moves updatedAt past it.
100- expect ( query . sql ) . toContain (
101- '"relay_managed_endpoint_allocations"."updated_at" > "relay_managed_endpoint_allocations"."tunnel_released_at"' ,
90+ it . effect ( "counts each allocation by its tunnel and link state" , ( ) =>
91+ Effect . gen ( function * ( ) {
92+ const pg = yield * Effect . acquireRelease (
93+ Effect . promise ( ( ) => PGlite . create ( ) ) ,
94+ ( client ) => Effect . promise ( ( ) => client . close ( ) ) ,
10295 ) ;
103- // A link revoked or made publish-only after the last reservation no longer holds a slot.
104- expect ( query . sql ) . toContain (
105- '"relay_environment_links"."updated_at" >= "relay_managed_endpoint_allocations"."updated_at"' ,
96+ yield * Effect . promise ( ( ) =>
97+ pg . exec ( `
98+ create table relay_managed_endpoint_allocations (
99+ user_id text not null, environment_id text not null, tunnel_id text,
100+ tunnel_released_at text, updated_at text not null
101+ );
102+ create table relay_environment_links (
103+ user_id text not null, environment_id text not null, revoked_at text,
104+ managed_tunnels_enabled boolean not null, updated_at text not null
105+ );
106+ ` ) ,
106107 ) ;
107- } ) . pipe (
108- Effect . provide (
109- ManagedTunnelLimits . layer . pipe (
110- Layer . provide ( Layer . succeed ( RelayDb . RelayDb , makeFakeDb ( [ ] , countConditions ) ) ) ,
108+ const at = ( minutes : number ) => `2026-10-27T12:${ String ( minutes ) . padStart ( 2 , "0" ) } :00.000Z` ;
109+ const cases : ReadonlyArray < {
110+ readonly environmentId : string ;
111+ readonly counted : boolean ;
112+ readonly allocation : {
113+ readonly tunnelId ?: string ;
114+ readonly releasedAt ?: number ;
115+ readonly updatedAt : number ;
116+ } ;
117+ readonly link ?: {
118+ readonly revoked ?: boolean ;
119+ readonly publishOnly ?: boolean ;
120+ readonly updatedAt : number ;
121+ } ;
122+ } > = [
123+ { environmentId : "pending" , counted : true , allocation : { updatedAt : 50 } } ,
124+ { environmentId : "abandoned-reservation" , counted : false , allocation : { updatedAt : 10 } } ,
125+ {
126+ environmentId : "live" ,
127+ counted : true ,
128+ allocation : { tunnelId : "t" , updatedAt : 10 } ,
129+ link : { updatedAt : 12 } ,
130+ } ,
131+ {
132+ environmentId : "released" ,
133+ counted : false ,
134+ allocation : { tunnelId : "t" , releasedAt : 20 , updatedAt : 20 } ,
135+ } ,
136+ {
137+ environmentId : "re-reserved-after-release" ,
138+ counted : true ,
139+ allocation : { tunnelId : "t" , releasedAt : 20 , updatedAt : 50 } ,
140+ } ,
141+ {
142+ environmentId : "revoked" ,
143+ counted : false ,
144+ allocation : { tunnelId : "t" , updatedAt : 10 } ,
145+ link : { revoked : true , updatedAt : 20 } ,
146+ } ,
147+ {
148+ environmentId : "revoked-pending" ,
149+ counted : false ,
150+ allocation : { updatedAt : 50 } ,
151+ link : { revoked : true , updatedAt : 55 } ,
152+ } ,
153+ {
154+ environmentId : "publish-only" ,
155+ counted : false ,
156+ allocation : { tunnelId : "t" , updatedAt : 10 } ,
157+ link : { publishOnly : true , updatedAt : 20 } ,
158+ } ,
159+ // Reprovisioned after a revoke, but the Worker died before rewriting the link.
160+ {
161+ environmentId : "reprovision-before-link" ,
162+ counted : true ,
163+ allocation : { tunnelId : "t" , updatedAt : 20 } ,
164+ link : { revoked : true , updatedAt : 10 } ,
165+ } ,
166+ {
167+ environmentId : "reserved-before-link-rewrite" ,
168+ counted : true ,
169+ allocation : { updatedAt : 50 } ,
170+ link : { revoked : true , updatedAt : 48 } ,
171+ } ,
172+ { environmentId : "self" , counted : false , allocation : { tunnelId : "t" , updatedAt : 10 } } ,
173+ ] ;
174+ for ( const entry of cases ) {
175+ yield * Effect . promise ( ( ) =>
176+ pg . query (
177+ "insert into relay_managed_endpoint_allocations values ('user-1', $1, $2, $3, $4)" ,
178+ [
179+ entry . environmentId ,
180+ entry . allocation . tunnelId ?? null ,
181+ entry . allocation . releasedAt === undefined ? null : at ( entry . allocation . releasedAt ) ,
182+ at ( entry . allocation . updatedAt ) ,
183+ ] ,
184+ ) ,
185+ ) ;
186+ if ( entry . link !== undefined ) {
187+ const link = entry . link ;
188+ yield * Effect . promise ( ( ) =>
189+ pg . query ( "insert into relay_environment_links values ('user-1', $1, $2, $3, $4)" , [
190+ entry . environmentId ,
191+ link . revoked === true ? at ( link . updatedAt ) : null ,
192+ link . publishOnly !== true ,
193+ at ( link . updatedAt ) ,
194+ ] ) ,
195+ ) ;
196+ }
197+ }
198+ yield * Effect . promise ( ( ) =>
199+ pg . query (
200+ "insert into relay_managed_endpoint_allocations values ('user-2', 'other-user', 't', null, $1)" ,
201+ [ at ( 10 ) ] ,
111202 ) ,
112- ) ,
113- ) ;
114- } ) ;
203+ ) ;
204+
205+ const counted = yield * Effect . promise ( ( ) =>
206+ drizzle ( { client : pg } )
207+ . select ( { environmentId : relayManagedEndpointAllocations . environmentId } )
208+ . from ( relayManagedEndpointAllocations )
209+ . where (
210+ ManagedTunnelLimits . countedAllocations ( {
211+ userId : "user-1" ,
212+ environmentId : "self" ,
213+ pendingCutoff : at ( 45 ) ,
214+ } ) ,
215+ ) ,
216+ ) ;
217+
218+ expect ( counted . map ( ( row ) => row . environmentId ) . toSorted ( ) ) . toEqual (
219+ cases
220+ . filter ( ( entry ) => entry . counted )
221+ . map ( ( entry ) => entry . environmentId )
222+ . toSorted ( ) ,
223+ ) ;
224+ } ) ,
225+ ) ;
115226} ) ;
0 commit comments