@@ -100,6 +100,54 @@ defmodule Realtime.Tenants.ConnectTest do
100
100
# This one will succeed
101
101
{ :ok , _pid } = Connect . lookup_or_start_connection ( tenant . external_id )
102
102
end
103
+
104
+ test "too many db connections" , % { tenant: tenant } do
105
+ extension = % {
106
+ "type" => "postgres_cdc_rls" ,
107
+ "settings" => % {
108
+ "db_host" => "127.0.0.1" ,
109
+ "db_name" => "postgres" ,
110
+ "db_user" => "supabase_admin" ,
111
+ "db_password" => "postgres" ,
112
+ "poll_interval" => 100 ,
113
+ "poll_max_changes" => 100 ,
114
+ "poll_max_record_bytes" => 1_048_576 ,
115
+ "region" => "us-east-1" ,
116
+ "ssl_enforced" => false ,
117
+ "db_pool" => 100 ,
118
+ "subcriber_pool_size" => 100 ,
119
+ "subs_pool_size" => 100
120
+ }
121
+ }
122
+
123
+ { :ok , tenant } = update_extension ( tenant , extension )
124
+
125
+ parent = self ( )
126
+
127
+ # Let's slow down Connect starting
128
+ expect ( Database , :check_tenant_connection , fn t ->
129
+ :timer . sleep ( 1000 )
130
+ call_original ( Database , :check_tenant_connection , [ t ] )
131
+ end )
132
+
133
+ connect = fn -> send ( parent , Connect . lookup_or_start_connection ( tenant . external_id ) ) end
134
+
135
+ # Start an early connect
136
+ spawn ( connect )
137
+ :timer . sleep ( 100 )
138
+
139
+ # Start others
140
+ spawn ( connect )
141
+ spawn ( connect )
142
+
143
+ # This one should block and wait for the first Connect
144
+ { :error , :tenant_db_too_many_connections } = Connect . lookup_or_start_connection ( tenant . external_id )
145
+
146
+ assert_receive { :error , :tenant_db_too_many_connections }
147
+ assert_receive { :error , :tenant_db_too_many_connections }
148
+ assert_receive { :error , :tenant_db_too_many_connections }
149
+ refute_receive _any
150
+ end
103
151
end
104
152
105
153
describe "region rebalancing" do
@@ -263,6 +311,34 @@ defmodule Realtime.Tenants.ConnectTest do
263
311
assert { :error , :tenant_suspended } = Connect . lookup_or_start_connection ( tenant . external_id )
264
312
end
265
313
314
+ test "tenant not able to connect if database has not enough connections" , % {
315
+ tenant: tenant
316
+ } do
317
+ extension = % {
318
+ "type" => "postgres_cdc_rls" ,
319
+ "settings" => % {
320
+ "db_host" => "127.0.0.1" ,
321
+ "db_name" => "postgres" ,
322
+ "db_user" => "supabase_admin" ,
323
+ "db_password" => "postgres" ,
324
+ "poll_interval" => 100 ,
325
+ "poll_max_changes" => 100 ,
326
+ "poll_max_record_bytes" => 1_048_576 ,
327
+ "region" => "us-east-1" ,
328
+ "ssl_enforced" => false ,
329
+ "db_pool" => 100 ,
330
+ "subcriber_pool_size" => 100 ,
331
+ "subs_pool_size" => 100
332
+ }
333
+ }
334
+
335
+ { :ok , tenant } = update_extension ( tenant , extension )
336
+
337
+ assert capture_log ( fn ->
338
+ assert { :error , :tenant_db_too_many_connections } = Connect . lookup_or_start_connection ( tenant . external_id )
339
+ end ) =~ ~r/ Only \d + available connections\. At least \d + connections are required/
340
+ end
341
+
266
342
test "handles tenant suspension and unsuspension in a reactive way" , % { tenant: tenant } do
267
343
assert { :ok , db_conn } = Connect . lookup_or_start_connection ( tenant . external_id )
268
344
assert Connect . ready? ( tenant . external_id )
@@ -459,30 +535,6 @@ defmodule Realtime.Tenants.ConnectTest do
459
535
test "if tenant does not exist, does nothing" do
460
536
assert :ok = Connect . shutdown ( "none" )
461
537
end
462
-
463
- test "tenant not able to connect if database has not enough connections" , % { tenant: tenant } do
464
- extension = % {
465
- "type" => "postgres_cdc_rls" ,
466
- "settings" => % {
467
- "db_host" => "127.0.0.1" ,
468
- "db_name" => "postgres" ,
469
- "db_user" => "supabase_admin" ,
470
- "db_password" => "postgres" ,
471
- "poll_interval" => 100 ,
472
- "poll_max_changes" => 100 ,
473
- "poll_max_record_bytes" => 1_048_576 ,
474
- "region" => "us-east-1" ,
475
- "ssl_enforced" => false ,
476
- "db_pool" => 100 ,
477
- "subcriber_pool_size" => 100 ,
478
- "subs_pool_size" => 100
479
- }
480
- }
481
-
482
- { :ok , tenant } = update_extension ( tenant , extension )
483
-
484
- assert { :error , :tenant_db_too_many_connections } = Connect . lookup_or_start_connection ( tenant . external_id )
485
- end
486
538
end
487
539
488
540
describe "registers into local registry" do
0 commit comments