Skip to content

Commit

Permalink
test: Prevent test_admin_ready_includes_schema_cache_state from timin…
Browse files Browse the repository at this point in the history
…g out

By increasing the delays in this test by factor 400x, postgrest will not
swamp pg with connection retries after the failed schema cache anymore.

This would happen because there is no backoff included after fatal
errors. Once it does, the io tests hang indefinitely in CI.
  • Loading branch information
wolfgangwalther committed May 9, 2024
1 parent e96e16f commit 747c78f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type SqlQuery = ByteString

querySchemaCache :: AppConfig -> SQL.Transaction SchemaCache
querySchemaCache AppConfig{..} = do
_ <-
let sleepCall = SQL.Statement "select pg_sleep($1 / 1000.0)" (param HE.int4) HD.noResult prepared in
whenJust configInternalSCSleep (`SQL.statement` sleepCall) -- only used for testing
SQL.sql "set local schema ''" -- This voids the search path. The following queries need this for getting the fully qualified name(schema.name) of every db object
pgVer <- SQL.statement mempty $ pgVersionStatement prepared
tabs <- SQL.statement schemas $ allTables pgVer prepared
Expand All @@ -155,9 +158,6 @@ querySchemaCache AppConfig{..} = do
reps <- SQL.statement schemas $ dataRepresentations prepared
mHdlers <- SQL.statement schemas $ mediaHandlers pgVer prepared
tzones <- SQL.statement mempty $ timezones prepared
_ <-
let sleepCall = SQL.Statement "select pg_sleep($1)" (param HE.int4) HD.noResult prepared in
whenJust configInternalSCSleep (`SQL.statement` sleepCall) -- only used for testing

let tabsWViewsPks = addViewPrimaryKeys tabs keyDeps
rels = addInverseRels $ addM2MRels tabsWViewsPks $ addViewM2OAndO2ORels keyDeps m2oRels
Expand Down
13 changes: 8 additions & 5 deletions test/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,20 +710,23 @@ def test_admin_ready_includes_schema_cache_state(defaultenv, metapostgrest):
**defaultenv,
"PGUSER": role,
"PGRST_DB_ANON_ROLE": role,
"PGRST_INTERNAL_SCHEMA_CACHE_SLEEP": "500",
}

with run(env=env) as postgrest:
# make it impossible to load the schema cache, by setting statement timeout to 1ms
set_statement_timeout(metapostgrest, role, 1)
# The schema cache query takes at least 500ms, due do PGRST_INTERNAL_SCHEMA_CACHE_SLEEP above.
# Make it impossible to load the schema cache, by setting statement timeout to 400ms.
set_statement_timeout(metapostgrest, role, 400)

# force a reconnection so the new role setting is picked up
postgrest.process.send_signal(signal.SIGUSR1)
sleep_until_postgrest_scache_reload()
# wait 600ms to finish schema cache reload attempt
time.sleep(0.6)

response = postgrest.admin.get("/ready")
response = postgrest.admin.get("/ready", timeout=1)
assert response.status_code == 503

response = postgrest.session.get("/projects")
response = postgrest.session.get("/projects", timeout=1)
assert response.status_code == 503

reset_statement_timeout(metapostgrest, role)
Expand Down

0 comments on commit 747c78f

Please sign in to comment.