Skip to content

Commit fa94bd0

Browse files
committed
Add tests for client connection counters
1 parent 761a2fa commit fa94bd0

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

tests/ruby/admin_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,45 @@
176176
end
177177
end
178178

179+
context "clients connects and disconnect normally" do
180+
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 2) }
181+
182+
it 'shows the number same number of clients before and after' do
183+
clients_before = clients_connected_to_pool(processes: processes)
184+
threads = []
185+
connections = Array.new(4) { PG::connect("#{pgcat_conn_str}?application_name=one_query") }
186+
connections.each do |c|
187+
threads << Thread.new { c.async_exec("SELECT 1") }
188+
end
189+
clients_between = clients_connected_to_pool(processes: processes)
190+
expect(clients_before).not_to eq(clients_between)
191+
connections.each(&:close)
192+
clients_after = clients_connected_to_pool(processes: processes)
193+
expect(clients_before).to eq(clients_after)
194+
end
195+
end
196+
197+
context "clients connects and disconnect abruptly" do
198+
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 10) }
199+
200+
it 'shows the number same number of clients before and after' do
201+
threads = []
202+
connections = Array.new(2) { PG::connect("#{pgcat_conn_str}?application_name=one_query") }
203+
connections.each do |c|
204+
threads << Thread.new { c.async_exec("SELECT pg_sleep(1.1)") }
205+
end
206+
clients_before = clients_connected_to_pool(processes: processes)
207+
208+
connection_string = "#{pgcat_conn_str}?application_name=one_query"
209+
faulty_client = Process.spawn("psql -Atx #{connection_string} ")
210+
sleep(2)
211+
Process.kill("SIGKILL", faulty_client)
212+
sleep(1)
213+
clients_after = clients_connected_to_pool(processes: processes)
214+
expect(clients_before).to eq(clients_after)
215+
end
216+
end
217+
179218
context "clients overwhelm server pools" do
180219
let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 2) }
181220

tests/ruby/capture

39.7 KB
Binary file not shown.

tests/ruby/spec_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ def with_captured_stdout_stderr
1919
STDOUT.reopen(sout)
2020
STDERR.reopen(serr)
2121
end
22+
23+
def clients_connected_to_pool(pool_index: 0, processes:)
24+
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
25+
results = admin_conn.async_exec("SHOW POOLS")[pool_index]
26+
admin_conn.close
27+
results['cl_idle'].to_i + results['cl_active'].to_i + results['cl_waiting'].to_i
28+
end

0 commit comments

Comments
 (0)