Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IO tests more stable #3497

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wolfgangwalther marked this conversation as resolved.
Show resolved Hide resolved

let tabsWViewsPks = addViewPrimaryKeys tabs keyDeps
rels = addInverseRels $ addM2MRels tabsWViewsPks $ addViewM2OAndO2ORels keyDeps m2oRels
Expand Down
26 changes: 18 additions & 8 deletions test/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,13 @@ def test_statement_timeout(defaultenv, metapostgrest):
data = response.json()
assert data["message"] == "canceling statement due to statement timeout"

reset_statement_timeout(metapostgrest, role)


def test_change_statement_timeout(defaultenv, metapostgrest):
"Statement timeout changes take effect immediately"

role = "timeout_authenticator"
reset_statement_timeout(metapostgrest, role)

env = {
**defaultenv,
Expand Down Expand Up @@ -544,6 +545,8 @@ def test_change_statement_timeout(defaultenv, metapostgrest):
response = postgrest.session.get("/rpc/sleep?seconds=1")
assert response.status_code == 204

reset_statement_timeout(metapostgrest, role)


def test_pool_size(defaultenv, metapostgrest):
"Verify that PGRST_DB_POOL setting allows the correct number of parallel requests"
Expand Down Expand Up @@ -606,7 +609,6 @@ def test_change_statement_timeout_held_connection(defaultenv, metapostgrest):
"Statement timeout changes take effect immediately, even with a request outliving the reconfiguration"

role = "timeout_authenticator"
reset_statement_timeout(metapostgrest, role)

env = {
**defaultenv,
Expand Down Expand Up @@ -651,6 +653,8 @@ def sleep(i=i):
for t in threads:
t.join()

reset_statement_timeout(metapostgrest, role)


def test_admin_config(defaultenv):
"Should get a success response from the admin server containing current configuration"
Expand Down Expand Up @@ -701,28 +705,32 @@ def test_admin_ready_includes_schema_cache_state(defaultenv, metapostgrest):
"Should get a failed response from the admin server ready endpoint when the schema cache is not loaded"

role = "timeout_authenticator"
reset_statement_timeout(metapostgrest, role)

env = {
**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)


def test_admin_not_found(defaultenv):
"Should get a not found from a undefined endpoint on the admin server"
Expand Down Expand Up @@ -1388,6 +1396,8 @@ def test_db_error_logging_to_stderr(level, defaultenv, metapostgrest):
assert " 500 " in output[0]
assert "canceling statement due to statement timeout" in output[1]

reset_statement_timeout(metapostgrest, role)


def test_function_setting_statement_timeout_fails(defaultenv):
"statement that takes three seconds to execute should fail with one second timeout"
Expand Down
Loading