Skip to content

Commit

Permalink
test: DRY scache/config sleep in io tests
Browse files Browse the repository at this point in the history
Also increase schema cache sleep to 0.2 as 0.1 tends to fail in CI.
  • Loading branch information
steve-chavez committed Oct 20, 2023
1 parent 54786a6 commit eb238ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
15 changes: 15 additions & 0 deletions test/io/postgrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
from config import *


def sleep_until_postgrest_scache_reload():
"Sleep until schema cache reload"
time.sleep(0.2)


def sleep_until_postgrest_config_reload():
"Sleep until config reload"
time.sleep(0.1)


def sleep_until_postgrest_full_reload():
"Sleep until schema cache plus config reload"
time.sleep(0.2)


class PostgrestTimedOut(Exception):
"Connecting to PostgREST endpoint timed out."

Expand Down
32 changes: 16 additions & 16 deletions test/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_app_settings_flush_pool(defaultenv):

# SIGUSR1 causes the postgres connection pool to be flushed
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

uri = "/rpc/get_guc_value?name=app.settings.external_api_secret"
response = postgrest.session.get(uri)
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_app_settings_reload(tmp_path, defaultenv):
# reload
postgrest.process.send_signal(signal.SIGUSR2)

time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.get(uri)
assert response.text == '"Jane"'
Expand All @@ -237,7 +237,7 @@ def test_jwt_secret_reload(tmp_path, defaultenv):
# reload config
postgrest.process.send_signal(signal.SIGUSR2)

time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.get("/authors_only", headers=headers)
assert response.status_code == 200
Expand Down Expand Up @@ -267,14 +267,14 @@ def test_jwt_secret_external_file_reload(tmp_path, defaultenv):

# SIGUSR1 doesn't reload external files, at least when db-config=false
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

response = postgrest.session.get("/authors_only", headers=headers)
assert response.status_code == 401

# reload config and external file with SIGUSR2
postgrest.process.send_signal(signal.SIGUSR2)
time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.get("/authors_only", headers=headers)
assert response.status_code == 200
Expand All @@ -285,7 +285,7 @@ def test_jwt_secret_external_file_reload(tmp_path, defaultenv):
# reload config and external file with NOTIFY
response = postgrest.session.post("/rpc/reload_pgrst_config")
assert response.status_code == 204
time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.get("/authors_only", headers=headers)
assert response.status_code == 401
Expand All @@ -308,11 +308,11 @@ def test_db_schema_reload(tmp_path, defaultenv):

# reload config
postgrest.process.send_signal(signal.SIGUSR2)
time.sleep(0.1)
sleep_until_postgrest_config_reload()

# reload schema cache to verify that the config reload actually happened
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

response = postgrest.session.get("/rpc/get_guc_value?name=search_path")
assert response.text == '"\\"v1\\", \\"public\\""'
Expand All @@ -332,7 +332,7 @@ def test_db_schema_notify_reload(defaultenv):
"/rpc/change_db_schema_and_full_reload", data={"schemas": "v1"}
)

time.sleep(0.2)
sleep_until_postgrest_full_reload()

response = postgrest.session.get("/rpc/get_guc_value?name=search_path")
assert response.text == '"\\"v1\\", \\"public\\""'
Expand Down Expand Up @@ -360,7 +360,7 @@ def test_max_rows_reload(defaultenv):
# reload config
postgrest.process.send_signal(signal.SIGUSR2)

time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.head("/projects")
assert response.status_code == 200
Expand Down Expand Up @@ -390,7 +390,7 @@ def test_max_rows_notify_reload(defaultenv):
"/rpc/change_max_rows_config", data={"val": 1, "notify": True}
)

time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.head("/projects")
assert response.status_code == 200
Expand Down Expand Up @@ -501,7 +501,7 @@ def test_change_statement_timeout(defaultenv, metapostgrest):

# trigger schema refresh
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

response = postgrest.session.get("/rpc/sleep?seconds=1")
assert response.status_code == 500
Expand All @@ -512,7 +512,7 @@ def test_change_statement_timeout(defaultenv, metapostgrest):

# trigger role setting refresh
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

response = postgrest.session.get("/rpc/sleep?seconds=1")
assert response.status_code == 204
Expand Down Expand Up @@ -663,7 +663,7 @@ def test_admin_ready_includes_schema_cache_state(defaultenv, metapostgrest):

# force a reconnection so the new role setting is picked up
postgrest.process.send_signal(signal.SIGUSR1)
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

response = postgrest.admin.get("/ready")
assert response.status_code == 503
Expand Down Expand Up @@ -830,7 +830,7 @@ def test_notify_reloading_catalog_cache(defaultenv):
# change it to a bigint
response = postgrest.session.post("/rpc/drop_change_cats")
assert response.status_code == 204
time.sleep(0.1)
sleep_until_postgrest_scache_reload()

# next request should succeed with a bigint value
response = postgrest.session.get("/cats?id=eq.1")
Expand Down Expand Up @@ -858,7 +858,7 @@ def test_role_settings(defaultenv):

response = postgrest.session.get("/rpc/reload_pgrst_config")
assert response.status_code == 204
time.sleep(0.1)
sleep_until_postgrest_config_reload()

response = postgrest.session.get("/rpc/get_guc_value?name=statement_timeout")
assert response.text == '"5s"'
Expand Down

0 comments on commit eb238ad

Please sign in to comment.