66import subprocess
77import time
88import pytest
9+ import requests
910
1011from config import CONFIGSDIR , FIXTURES , SECRET
1112from util import Thread , jwtauthheader , parse_server_timings_header , relativeSeconds
@@ -1085,33 +1086,6 @@ def test_invalid_rpc_method_log_contains_role(defaultenv):
10851086 )
10861087
10871088
1088- def test_empty_schema_cache_log_contains_jwt_role (defaultenv ):
1089- "Requests are logged with the role when the schema cache is empty on startup"
1090-
1091- env = {
1092- ** defaultenv ,
1093- "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP" : "1000" ,
1094- "PGRST_JWT_SECRET" : SECRET ,
1095- }
1096- headers = jwtauthheader ({"role" : "postgrest_test_author" }, SECRET )
1097-
1098- with run (env = env , wait_for_readiness = False ) as postgrest :
1099- postgrest .wait_until_scache_starts_loading ()
1100-
1101- response = postgrest .session .get ("/authors_only" , headers = headers )
1102- assert response .status_code == 503
1103-
1104- output = drain_stdout (postgrest )
1105-
1106- assert any (
1107- re .match (
1108- r'- - postgrest_test_author \[.+\] "GET /authors_only HTTP/1.1" 503 \d+ "" "python-requests/.+"' ,
1109- line ,
1110- )
1111- for line in output
1112- )
1113-
1114-
11151089def test_no_pool_connection_required_on_bad_http_logic (defaultenv ):
11161090 "no pool connection should be consumed for failing on invalid http logic"
11171091
@@ -1513,14 +1487,19 @@ def test_log_postgrest_host_and_port(host, defaultenv):
15131487 with run (
15141488 env = defaultenv , host = host , port = port , no_startup_stdout = False
15151489 ) as postgrest :
1516- output = postgrest .read_stdout (nlines = 10 )
1490+ output = postgrest .read_stdout (nlines = 11 )
15171491
1492+ # Cannot assume a particular log entry order
1493+ # Listening on a socket happens after schema querying
1494+ # but is concurrent to the schema loading process
1495+ # and migh happen before or after writing of the
1496+ # "Schema cache loaded" log entry
15181497 if is_unix :
1519- re . match ( r' API server listening on " /tmp/.*\.sock"' , output [ 2 ])
1498+ match_log ( output , [ r".* API server listening on .* /tmp/.*\.sock" ])
15201499 elif is_ipv6 (host ):
1521- assert f" API server listening on [ { host } ]: { port } " in output [ 2 ]
1500+ match_log ( output , [ r".* API server listening on \[.+]:\d+" ])
15221501 else : # IPv4
1523- assert f" API server listening on { host } : { port } " in output [ 2 ]
1502+ match_log ( output , [ r".* API server listening on .+:\d+" ])
15241503
15251504
15261505def test_succeed_w_role_having_superuser_settings (defaultenv ):
@@ -1868,17 +1847,24 @@ def test_pgrst_log_503_client_error_to_stderr(defaultenv):
18681847 assert any (log_message in line for line in output )
18691848
18701849
1871- def test_log_error_when_empty_schema_cache_on_startup_to_stderr (defaultenv ):
1872- "Should log the 503 error message when there is an empty schema cache on startup"
1850+ def test_log_error_when_schema_cache_load_error_on_startup_to_stderr (defaultenv ):
1851+ "Should log the 503 error message when there is an error loading schema cache on startup"
18731852
18741853 env = {
18751854 ** defaultenv ,
1876- "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP" : "300" ,
1855+ "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP" : "1000" ,
1856+ "PGRST_DB_SCHEMAS" : "non_existent_schema_aaaa" ,
18771857 }
18781858
18791859 with run (env = env , wait_for_readiness = False ) as postgrest :
18801860 postgrest .wait_until_scache_starts_loading ()
18811861
1862+ # First call should fail with connection refused
1863+ with pytest .raises (requests .ConnectionError ):
1864+ postgrest .session .get ("/projects" )
1865+
1866+ # Next call should return 503
1867+ time .sleep (1 )
18821868 response = postgrest .session .get ("/projects" )
18831869 assert response .status_code == 503
18841870
@@ -1890,7 +1876,7 @@ def test_log_error_when_empty_schema_cache_on_startup_to_stderr(defaultenv):
18901876
18911877
18921878def test_no_double_schema_cache_reload_on_empty_schema (defaultenv ):
1893- "Should only load the schema cache once on a 503 error when there's an empty schema cache on startup"
1879+ "Should only load the schema cache once when there's an empty schema cache on startup"
18941880
18951881 env = {
18961882 ** defaultenv ,
@@ -1900,12 +1886,15 @@ def test_no_double_schema_cache_reload_on_empty_schema(defaultenv):
19001886 with run (env = env , port = freeport (), wait_for_readiness = False ) as postgrest :
19011887 postgrest .wait_until_scache_starts_loading ()
19021888
1903- response = postgrest . session . get ( "/projects" )
1904- assert response . status_code == 503
1889+ with pytest . raises ( requests . ConnectionError ):
1890+ postgrest . session . get ( "/projects" )
19051891
19061892 # Should wait enough time to load the schema cache twice to guarantee that the test is valid
19071893 time .sleep (1 )
19081894
1895+ response = postgrest .session .get ("/projects" )
1896+ assert response .status_code == 200
1897+
19091898 response = postgrest .admin .get ("/metrics" )
19101899 assert response .status_code == 200
19111900 assert 'pgrst_schema_cache_loads_total{status="SUCCESS"} 1.0' in response .text
@@ -1987,7 +1976,7 @@ def test_schema_cache_error_observation(defaultenv):
19871976 output = postgrest .read_stdout (nlines = 9 )
19881977 assert (
19891978 "Failed to load the schema cache using db-schemas=public and db-extra-search-path=x"
1990- in output [7 ]
1979+ in output [6 ]
19911980 )
19921981
19931982
0 commit comments