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,21 @@ 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 )
1517-
1490+ output = postgrest .read_stdout (nlines = 11 )
1491+ # time.sleep(1)
1492+ # assert False
1493+
1494+ # Cannot assume a particular log entry order
1495+ # Listening on a socket happens after schema querying
1496+ # but is concurrent to the schema loading process
1497+ # and migh happen before or after writing of the
1498+ # "Schema cache loaded" log entry
15181499 if is_unix :
1519- re . match ( r' API server listening on " /tmp/.*\.sock"' , output [ 2 ])
1500+ match_log ( output , [ r".* API server listening on .* /tmp/.*\.sock" ])
15201501 elif is_ipv6 (host ):
1521- assert f" API server listening on [ { host } ]: { port } " in output [ 2 ]
1502+ match_log ( output , [ r".* API server listening on \[.+]:\d+" ])
15221503 else : # IPv4
1523- assert f" API server listening on { host } : { port } " in output [ 2 ]
1504+ match_log ( output , [ r".* API server listening on .+:\d+" ])
15241505
15251506
15261507def test_succeed_w_role_having_superuser_settings (defaultenv ):
@@ -1868,17 +1849,24 @@ def test_pgrst_log_503_client_error_to_stderr(defaultenv):
18681849 assert any (log_message in line for line in output )
18691850
18701851
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"
1852+ def test_log_error_when_schema_cache_load_error_on_startup_to_stderr (defaultenv ):
1853+ "Should log the 503 error message when there is an error loading schema cache on startup"
18731854
18741855 env = {
18751856 ** defaultenv ,
1876- "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP" : "300" ,
1857+ "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP" : "1000" ,
1858+ "PGRST_DB_SCHEMAS" : "non_existent_schema_aaaa" ,
18771859 }
18781860
18791861 with run (env = env , wait_for_readiness = False ) as postgrest :
18801862 postgrest .wait_until_scache_starts_loading ()
18811863
1864+ # First call should fail with connection refused
1865+ with pytest .raises (requests .ConnectionError ):
1866+ postgrest .session .get ("/projects" )
1867+
1868+ # Next call should return 503
1869+ time .sleep (1 )
18821870 response = postgrest .session .get ("/projects" )
18831871 assert response .status_code == 503
18841872
@@ -1890,7 +1878,7 @@ def test_log_error_when_empty_schema_cache_on_startup_to_stderr(defaultenv):
18901878
18911879
18921880def 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"
1881+ "Should only load the schema cache once when there's an empty schema cache on startup"
18941882
18951883 env = {
18961884 ** defaultenv ,
@@ -1900,12 +1888,15 @@ def test_no_double_schema_cache_reload_on_empty_schema(defaultenv):
19001888 with run (env = env , port = freeport (), wait_for_readiness = False ) as postgrest :
19011889 postgrest .wait_until_scache_starts_loading ()
19021890
1903- response = postgrest . session . get ( "/projects" )
1904- assert response . status_code == 503
1891+ with pytest . raises ( requests . ConnectionError ):
1892+ postgrest . session . get ( "/projects" )
19051893
19061894 # Should wait enough time to load the schema cache twice to guarantee that the test is valid
19071895 time .sleep (1 )
19081896
1897+ response = postgrest .session .get ("/projects" )
1898+ assert response .status_code == 200
1899+
19091900 response = postgrest .admin .get ("/metrics" )
19101901 assert response .status_code == 200
19111902 assert 'pgrst_schema_cache_loads_total{status="SUCCESS"} 1.0' in response .text
@@ -1987,7 +1978,7 @@ def test_schema_cache_error_observation(defaultenv):
19871978 output = postgrest .read_stdout (nlines = 9 )
19881979 assert (
19891980 "Failed to load the schema cache using db-schemas=public and db-extra-search-path=x"
1990- in output [7 ]
1981+ in output [6 ]
19911982 )
19921983
19931984
0 commit comments