From b7de427aac5d9e0dc82476188db53aaa52063a4c Mon Sep 17 00:00:00 2001 From: Omair Majid Date: Wed, 7 Feb 2024 08:26:51 -0500 Subject: [PATCH] Use incremental backoff instead of hardcoding fixed sleep times With fixed/long sleep times, tests run slower when there is no need to wait for that long. When the machine is slow (or under heavy load), we often to need to wait for longer before things are running and the fixed sleep times are not sufficient. --- dotnet-monitor-works/test.sh | 7 ++----- libuv-kestrel-sample-app-2x/test.sh | 4 +--- publish-aspnet-selfcontained/test.sh | 5 ++--- run-until-success-with-backoff | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100755 run-until-success-with-backoff diff --git a/dotnet-monitor-works/test.sh b/dotnet-monitor-works/test.sh index 9bec9fe..5e71791 100755 --- a/dotnet-monitor-works/test.sh +++ b/dotnet-monitor-works/test.sh @@ -10,16 +10,13 @@ dotnet tool update -g dotnet-monitor --version "${VERSION_SPLIT[0]}.*-*" export PATH="$HOME/.dotnet/tools:$PATH" dotnet-monitor collect --no-auth & -sleep 5 -indexhttps=$(wget --no-check-certificate -O https.html https://127.0.0.1:52323/info) +../run-until-success-with-backoff wget --no-check-certificate -O https.html https://127.0.0.1:52323/info https=$(cat https.html) if [[ $https == *"version"* ]]; then - sleep 5 echo "collect - OK" else - sleep 5 pkill dotnet-monitor rm "https.html" echo "collect - FAIL" @@ -45,4 +42,4 @@ else echo "generatekey - FAIL" pkill dotnet-monitor exit 1 -fi \ No newline at end of file +fi diff --git a/libuv-kestrel-sample-app-2x/test.sh b/libuv-kestrel-sample-app-2x/test.sh index ad4c6bf..9dc0ad7 100755 --- a/libuv-kestrel-sample-app-2x/test.sh +++ b/libuv-kestrel-sample-app-2x/test.sh @@ -9,9 +9,7 @@ dotnet build dotnet bin/Debug/netcoreapp*/libuv-kestrel-sample-app-2x.dll & root_pid=$! -sleep 5 - -curl "http://localhost:5000" +../run-until-success-with-backoff curl "http://localhost:5000" kill -s SIGTERM "${root_pid}" sleep 1 diff --git a/publish-aspnet-selfcontained/test.sh b/publish-aspnet-selfcontained/test.sh index 88c7955..096405c 100755 --- a/publish-aspnet-selfcontained/test.sh +++ b/publish-aspnet-selfcontained/test.sh @@ -28,11 +28,10 @@ ASPNETCORE_URLS="$url" "./$output_dir/web" & run_pid=$! trap "kill $run_pid && wait $run_pid" EXIT -sleep 5 -if ! curl "$url"; then +if ! ../run-until-success-with-backoff curl "$url" ; then echo 'FAIL: ASP.NET app failed to respond' exit 2 fi -echo "PASS" \ No newline at end of file +echo "PASS" diff --git a/run-until-success-with-backoff b/run-until-success-with-backoff new file mode 100755 index 0000000..30d39d0 --- /dev/null +++ b/run-until-success-with-backoff @@ -0,0 +1,16 @@ +#!/bin/bash + +set -euo pipefail + +set -x + +i=0 +max_retries=20 +until "$@"; do + ((i+=1)) + if [[ $i == "$max_retries" ]]; then + echo "$@" "still failing after $max_retries retries" + exit 1 + fi + sleep $i +done