Skip to content

Commit a5f9ece

Browse files
committed
test(io): add pool flush metric coverage
Added IO tests for pgrst_db_pool_flushes_total to validate: - single flush increment on schema cache reload - no extra flushes during schema cache retry loops - metric exposure in /metrics output Also hardened wait_until_status_code by initializing response before retrying, preventing UnboundLocalError when all attempts raise connection exceptions.
1 parent f6882b1 commit a5f9ece

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

test/io/postgrest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def wait_until_exit(postgrest):
187187
def wait_until_status_code(url, max_seconds, status_code):
188188
"Wait for the given HTTP endpoint to return a status code"
189189
session = requests_unixsocket.Session()
190+
response = None
190191

191192
for _ in range(max_seconds * 10):
192193
try:

test/io/test_io.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
sleep_until_postgrest_full_reload,
1919
sleep_until_postgrest_scache_reload,
2020
wait_until_exit,
21+
wait_until_status_code,
2122
)
2223

2324

@@ -105,6 +106,88 @@ def sleep():
105106
t.join()
106107

107108

109+
def test_pool_flushes_metric(defaultenv):
110+
"Should increase pool flushes metric when the pool is flushed"
111+
112+
with run(env=defaultenv, port=freeport()) as postgrest:
113+
response = postgrest.admin.get("/metrics")
114+
assert response.status_code == 200
115+
before = float(
116+
re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group(
117+
1
118+
)
119+
)
120+
121+
postgrest.process.send_signal(signal.SIGUSR1)
122+
sleep_until_postgrest_scache_reload()
123+
124+
response = postgrest.admin.get("/metrics")
125+
assert response.status_code == 200
126+
after = float(
127+
re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group(
128+
1
129+
)
130+
)
131+
assert after == before + 1
132+
133+
134+
def test_pool_flushes_metric_with_schema_cache_retries(defaultenv, metapostgrest):
135+
"Should flush the pool exactly once even when schema cache reload retries"
136+
137+
role = "timeout_authenticator"
138+
app_name = "pool-flush-retry"
139+
env = {
140+
**defaultenv,
141+
"PGUSER": role,
142+
"PGAPPNAME": app_name,
143+
"PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP": "50",
144+
}
145+
146+
with run(env=env, port=freeport()) as postgrest:
147+
response = postgrest.admin.get("/metrics")
148+
assert response.status_code == 200
149+
before = float(
150+
re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group(
151+
1
152+
)
153+
)
154+
155+
try:
156+
# Force schema cache reload failures to trigger retries.
157+
set_statement_timeout(metapostgrest, role, 20)
158+
postgrest.process.send_signal(signal.SIGUSR1)
159+
160+
postgrest.wait_until_scache_starts_loading(max_seconds=2)
161+
162+
# Give retry loop time to run and verify it doesn't flush the pool.
163+
time.sleep(1)
164+
165+
response = postgrest.admin.get("/metrics")
166+
assert response.status_code == 200
167+
during_retries = float(
168+
re.search(
169+
r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text
170+
).group(1)
171+
)
172+
assert during_retries == before
173+
174+
reset_statement_timeout(metapostgrest, role)
175+
# Ensure next retry establishes fresh sessions with the reset timeout.
176+
metapostgrest.session.get(f"/rpc/terminate_pgrst?appname={app_name}")
177+
wait_until_status_code(postgrest.admin.baseurl + "/ready", 12, 200)
178+
finally:
179+
reset_statement_timeout(metapostgrest, role)
180+
181+
response = postgrest.admin.get("/metrics")
182+
assert response.status_code == 200
183+
after = float(
184+
re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group(
185+
1
186+
)
187+
)
188+
assert after == before + 1
189+
190+
108191
def test_random_port_bound(defaultenv):
109192
"PostgREST should bind to a random port when PGRST_SERVER_PORT is 0."
110193

@@ -1458,6 +1541,7 @@ def test_admin_metrics(defaultenv):
14581541
assert "pgrst_db_pool_waiting" in response.text
14591542
assert "pgrst_db_pool_available" in response.text
14601543
assert "pgrst_db_pool_timeouts_total" in response.text
1544+
assert "pgrst_db_pool_flushes_total" in response.text
14611545

14621546

14631547
def test_schema_cache_startup_load_with_in_db_config(defaultenv, metapostgrest):

0 commit comments

Comments
 (0)