Skip to content

Commit

Permalink
Use incremental backoff instead of hardcoding fixed sleep times
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
omajid committed Mar 5, 2024
1 parent 6497a02 commit d8af8e8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
7 changes: 2 additions & 5 deletions dotnet-monitor-works/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -45,4 +42,4 @@ else
echo "generatekey - FAIL"
pkill dotnet-monitor
exit 1
fi
fi
4 changes: 1 addition & 3 deletions libuv-kestrel-sample-app-2x/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions publish-aspnet-selfcontained/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
echo "PASS"
26 changes: 26 additions & 0 deletions run-until-success-with-backoff
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -euo pipefail

echo "Running with backoff:" "$@"

max_retries=10

iterations=0
total_slept=0 # seconds

sleep $((iterations + 1))
((total_slept += iterations + 1))

until "$@"; do
((iterations += 1))
if (( total_slept > 10 )); then
echo "$@" "still failing after more than ${total_slept} seconds"
fi
if (( iterations == max_retries )); then
echo "$@" "still failing after $max_retries retries"
exit 1
fi
sleep $iterations
((total_slept += iterations))
done

0 comments on commit d8af8e8

Please sign in to comment.