Skip to content

Commit

Permalink
test: requests wait for schema cache load
Browse files Browse the repository at this point in the history
* nix: add postgrest-test-big-schema command
  • Loading branch information
steve-chavez committed Mar 15, 2024
1 parent 9a0aae5 commit 1eb8011
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ jobs:
if: always()
run: postgrest-with-postgresql-${{ matrix.pgVersion }} postgrest-test-io -vv

- name: Run IO tests on a big schema
if: always()
run: postgrest-with-postgresql-${{ matrix.pgVersion }} postgrest-test-big-schema -vv


memory:
name: Memory
Expand Down
1 change: 1 addition & 0 deletions nix/tools/devTools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ let
${tests}/bin/postgrest-test-spec
${tests}/bin/postgrest-test-doctests
${tests}/bin/postgrest-test-io
${tests}/bin/postgrest-test-big-schema
${style}/bin/postgrest-lint
${style}/bin/postgrest-style-check
'';
Expand Down
20 changes: 18 additions & 2 deletions nix/tools/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ let
''
${cabal-install}/bin/cabal v2-build ${devCabalOptions}
${cabal-install}/bin/cabal v2-exec -- ${withTools.withPg} -f test/io/fixtures.sql \
${ioTestPython}/bin/pytest -v test/io "''${_arg_leftovers[@]}"
${ioTestPython}/bin/pytest --ignore=test/io/test_big_schema.py -v test/io "''${_arg_leftovers[@]}"
'';

testBigSchema =
checkedShellScript
{
name = "postgrest-test-big-schema";
docs = "Run a pytest-based IO test on a big schema. Add -k to run tests that match a given expression.";
args = [ "ARG_LEFTOVERS([pytest arguments])" ];
workingDir = "/";
withEnv = postgrest.env;
}
''
${cabal-install}/bin/cabal v2-build ${devCabalOptions}
${cabal-install}/bin/cabal v2-exec -- ${withTools.withPg} -f test/io/big_schema.sql \
${ioTestPython}/bin/pytest -v test/io/test_big_schema.py "''${_arg_leftovers[@]}"
'';

dumpSchema =
Expand Down Expand Up @@ -135,7 +150,7 @@ let
# collect all tests
HPCTIXFILE="$tmpdir"/io.tix \
${withTools.withPg} -f test/io/fixtures.sql \
${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- ${ioTestPython}/bin/pytest -v test/io
${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- ${ioTestPython}/bin/pytest --ignore=test/io/test_big_schema.py -v test/io
HPCTIXFILE="$tmpdir"/spec.tix \
${withTools.withPg} -f test/spec/fixtures/load.sql \
Expand Down Expand Up @@ -200,6 +215,7 @@ buildToolbox
testDoctests
testSpecIdempotence
testIO
testBigSchema
dumpSchema
coverage
coverageDraftOverlay;
Expand Down
47 changes: 47 additions & 0 deletions test/io/test_big_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"IO tests for PostgREST started on the big schema."

import pytest

from config import *
from util import *
from postgrest import *


def test_requests_wait_for_schema_cache_to_be_loaded(defaultenv):
"requests that use the schema cache (e.g. resource embedding) wait for schema cache to be loaded"

env = {
**defaultenv,
"PGRST_DB_SCHEMAS": "apflora",
"PGRST_DB_POOL": "2",
"PGRST_DB_ANON_ROLE": "postgrest_test_anonymous",
"PGRST_SERVER_TIMING_ENABLED": "true",
}

with run(env=env, wait_for_readiness=False) as postgrest:
time.sleep(1.5) # manually wait for schema cache to start loading

response = postgrest.session.get("/tpopmassn?select=*,tpop(*)")
assert response.status_code == 200

server_timings = parse_server_timings_header(response.headers["Server-Timing"])
plan_dur = server_timings["plan"]
assert plan_dur > 10000.0


# TODO: This test fails now because of https://github.com/PostgREST/postgrest/pull/2122
# The stack size of 1K(-with-rtsopts=-K1K) is not enough and this fails with "stack overflow"
# A stack size of 200K seems to be enough for succeess
@pytest.mark.skip
def test_openapi_in_big_schema(defaultenv):
"Should get a successful response from openapi on a big schema"

env = {
**defaultenv,
"PGRST_DB_SCHEMAS": "apflora",
"PGRST_OPENAPI_MODE": "ignore-privileges",
}

with run(env=env) as postgrest:
response = postgrest.session.get("/")
assert response.status_code == 200
18 changes: 0 additions & 18 deletions test/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,24 +1007,6 @@ def test_schema_cache_concurrent_notifications(slow_schema_cache_env):
assert response.status_code == 200


# TODO: This test fails now because of https://github.com/PostgREST/postgrest/pull/2122
# The stack size of 1K(-with-rtsopts=-K1K) is not enough and this fails with "stack overflow"
# A stack size of 200K seems to be enough for succeess
@pytest.mark.skip
def test_openapi_in_big_schema(defaultenv):
"Should get a successful response from openapi on a big schema"

env = {
**defaultenv,
"PGRST_DB_SCHEMAS": "apflora",
"PGRST_OPENAPI_MODE": "ignore-privileges",
}

with run(env=env) as postgrest:
response = postgrest.session.get("/")
assert response.status_code == 200


@pytest.mark.parametrize("dburi_type", ["no_params", "no_params_qmark", "with_params"])
def test_get_pgrst_version_with_uri_connection_string(dburi_type, dburi, defaultenv):
"The fallback_application_name should be added to the db-uri if it has a URI format"
Expand Down
2 changes: 1 addition & 1 deletion test/io/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ def parse_server_timings_header(header):
for timing in header.split(","):
name, duration_text, *_ = timing.split(";")
_, duration = duration_text.split("=")
timings[name] = float(duration)
timings[name.strip()] = float(duration)
return timings

0 comments on commit 1eb8011

Please sign in to comment.