From d218a9eaff563231be2bf7d4b84f27ac46cf31b7 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Fri, 4 Dec 2020 19:04:53 +0100 Subject: [PATCH] dev: improve postgrest-watch experience * speed up io tests * avoid infinite loop while watching io tests * improve auto-complete with postgrest-watch postgrest-run * detect when files are added or deleted --- nix/devtools.nix | 4 +- nix/style.nix | 6 ++- nix/tests.nix | 3 +- shell.nix | 3 +- test/io-tests.sh | 62 ++++++++++------------- test/io-tests/configs/app-settings.config | 1 + 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/nix/devtools.nix b/nix/devtools.nix index 77e45d0e8..df64d84cb 100644 --- a/nix/devtools.nix +++ b/nix/devtools.nix @@ -14,7 +14,9 @@ let '' rootdir="$(${git}/bin/git rev-parse --show-toplevel)" - ${silver-searcher}/bin/ag -l . "$rootdir" | ${entr}/bin/entr -r "$@" + while true; do + (! ${silver-searcher}/bin/ag -l . "$rootdir" | ${entr}/bin/entr -dr "$@") + done ''; pushCachix = diff --git a/nix/style.nix b/nix/style.nix index e8dd87d62..9f2a7165e 100644 --- a/nix/style.nix +++ b/nix/style.nix @@ -16,7 +16,8 @@ let ${nixpkgs-fmt}/bin/nixpkgs-fmt "$rootdir" > /dev/null 2> /dev/null # Format Haskell files - ${silver-searcher}/bin/ag -l -g '\.l?hs$' . "$rootdir" \ + # --vimgrep fixes a bug in ag: https://github.com/ggreer/the_silver_searcher/issues/753 + ${silver-searcher}/bin/ag -l --vimgrep -g '\.l?hs$' . "$rootdir" \ | xargs ${stylish-haskell}/bin/stylish-haskell -i ''; @@ -35,7 +36,8 @@ let rootdir="$(${git}/bin/git rev-parse --show-toplevel)" # Lint Haskell files - ${silver-searcher}/bin/ag -l -g '\.l?hs$' "$rootdir" \ + # --vimgrep fixes a bug in ag: https://github.com/ggreer/the_silver_searcher/issues/753 + ${silver-searcher}/bin/ag -l --vimgrep -g '\.l?hs$' "$rootdir" \ | xargs ${hlint}/bin/hlint -X QuasiQuotes -X NoPatternSynonyms ''; in diff --git a/nix/tests.nix b/nix/tests.nix index c91c30498..82d769221 100644 --- a/nix/tests.nix +++ b/nix/tests.nix @@ -13,6 +13,7 @@ , postgrest , postgrestStatic , postgrestProfiled +, procps , runtimeShell }: let @@ -78,7 +79,7 @@ let name '' env="$(cat ${postgrest.env})" - export PATH="$env/bin:${curl}/bin:$PATH" + export PATH="$env/bin:${curl}/bin:${procps}/bin:$PATH" rootdir="$(${git}/bin/git rev-parse --show-toplevel)" cd "$rootdir" diff --git a/shell.nix b/shell.nix index ddac86916..5021fc6d0 100644 --- a/shell.nix +++ b/shell.nix @@ -38,7 +38,8 @@ lib.overrideDerivation postgrest.env ( shellHook = '' - complete -c postgrest-watch + source ${pkgs.bashCompletion}/etc/profile.d/bash_completion.sh + complete -F _command postgrest-watch ''; } ) diff --git a/test/io-tests.sh b/test/io-tests.sh index a286d955b..703ea0a33 100755 --- a/test/io-tests.sh +++ b/test/io-tests.sh @@ -88,8 +88,7 @@ readSecretFromFile(){ while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done if pgrStarted then @@ -113,8 +112,7 @@ readDbUriFromStdin(){ while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done if pgrStarted then @@ -131,8 +129,7 @@ reqWithRoleClaimKey(){ while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done authorsJwt=$(psql -qtAX "$POSTGREST_TEST_CONNECTION" -c "select jwt.sign('$2', 'reallyreallyreallyreallyverysafe');") httpStatus="$( authorsStatus "$authorsJwt" )" @@ -151,8 +148,7 @@ invalidRoleClaimKey(){ while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done if pgrStarted then @@ -169,8 +165,7 @@ ensureIatClaimWorks(){ while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done for i in {1..10}; do \ iatJwt=$(psql -qtAX "$POSTGREST_TEST_CONNECTION" -c "select jwt.sign(row_to_json(r), 'reallyreallyreallyreallyverysafe') from ( select 'postgrest_test_author' as role, extract(epoch from now()) as iat) r") @@ -186,16 +181,16 @@ ensureIatClaimWorks(){ pgrStop } -# ensure app settings don't reset on pool timeout of 10 seconds, see https://github.com/PostgREST/postgrest/issues/1141 +# ensure app settings don't reset on pool timeout, see https://github.com/PostgREST/postgrest/issues/1141 +# pool timeout set to 1s to shorten runtime ensureAppSettings(){ pgrStart "./configs/app-settings.config" while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done - sleep 11 + sleep 2 response=$(curl -s "http://localhost:$pgrPort/rpc/get_guc_value?name=app.settings.external_api_secret") if test "$response" = "\"0123456789abcdef\"" then @@ -207,15 +202,17 @@ ensureAppSettings(){ } checkAppSettingsReload(){ - pgrStart "./configs/sigusr2-settings.config" + configFile=$(mktemp) + trap "rm -f $configFile" ERR RETURN + cat "./configs/sigusr2-settings.config" > "$configFile" + pgrStart "$configFile" while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done # change setting - replaceConfigValue "app.settings.name_var" "Jane" ./configs/sigusr2-settings.config + replaceConfigValue "app.settings.name_var" "Jane" "$configFile" # reload kill -s SIGUSR2 $pgrPID response=$(curl -s "http://localhost:$pgrPort/rpc/get_guc_value?name=app.settings.name_var") @@ -226,21 +223,21 @@ checkAppSettingsReload(){ ko "app.settings.name_var config not reloaded with SIGUSR2. Got: $response" fi pgrStop - # go back to original setting - replaceConfigValue "app.settings.name_var" "John" ./configs/sigusr2-settings.config } checkJwtSecretReload(){ - pgrStart "./configs/sigusr2-settings.config" + configFile=$(mktemp) + trap "rm -f $configFile" ERR RETURN + cat "./configs/sigusr2-settings.config" > "$configFile" + pgrStart "$configFile" while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done secret="reallyreallyreallyreallyverysafe" # change setting - replaceConfigValue "jwt-secret" "$secret" ./configs/sigusr2-settings.config + replaceConfigValue "jwt-secret" "$secret" "$configFile" # reload kill -s SIGUSR2 $pgrPID payload='{"role":"postgrest_test_author"}' @@ -253,20 +250,20 @@ checkJwtSecretReload(){ ko "jwt-secret config not reloaded with SIGUSR2. Got: $httpStatus" fi pgrStop - # go back to original setting - replaceConfigValue "jwt-secret" "invalidinvalidinvalidinvalidinvalid" ./configs/sigusr2-settings.config } checkDbSchemaReload(){ - pgrStart "./configs/sigusr2-settings.config" + configFile=$(mktemp) + trap "rm -f $configFile" ERR RETURN + cat "./configs/sigusr2-settings.config" > "$configFile" + pgrStart "$configFile" while pgrStarted && test "$( rootStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done # add v1 schema to db-schema - replaceConfigValue "db-schema" "test, v1" ./configs/sigusr2-settings.config + replaceConfigValue "db-schema" "test, v1" "$configFile" # reload kill -s SIGUSR2 $pgrPID kill -s SIGUSR1 $pgrPID @@ -278,8 +275,6 @@ checkDbSchemaReload(){ ko "db-schema config not reloaded with SIGUSR2. Got: $httpStatus" fi pgrStop - # go back to original setting - replaceConfigValue "db-schema" "test" ./configs/sigusr2-settings.config } replaceConfigValue(){ @@ -295,8 +290,7 @@ socketConnection(){ while pgrStarted && test "$( getSocketStatus )" -ne 200 do # wait for the server to start - sleep 0.1 \ - || sleep 1 # fallback: subsecond sleep is not standard and may fail + sleep 0.1 done if test $( getSocketStatus ) -eq 200 then diff --git a/test/io-tests/configs/app-settings.config b/test/io-tests/configs/app-settings.config index a8e36008f..74965527f 100644 --- a/test/io-tests/configs/app-settings.config +++ b/test/io-tests/configs/app-settings.config @@ -2,6 +2,7 @@ db-uri = "$(POSTGREST_TEST_CONNECTION)" db-schema = "test" db-anon-role = "postgrest_test_anonymous" db-pool = 1 +db-pool-timeout = 1 server-host = "127.0.0.1" server-port = 49421