Commit a4022a33 authored by Petr Machata's avatar Petr Machata Committed by Paolo Abeni

selftests: net: Unify code of busywait() and slowwait()

Bodies of busywait() and slowwait() functions are almost identical. Extract
the common code into a helper, loopy_wait, and convert busywait() and
slowwait() into trivial wrappers.

Moreover, the fact that slowwait() uses seconds for units is really not
intuitive, and the comment does not help much. Instead make the unit part
of the name of the argument to further clarify what units are expected.

Cc: Hangbin Liu <liuhangbin@gmail.com>
Signed-off-by: default avatarPetr Machata <petrm@nvidia.com>
Reviewed-by: default avatarBenjamin Poirier <bpoirier@nvidia.com>
Reviewed-by: default avatarHangbin Liu <liuhangbin@gmail.com>
Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent 62d6d91d
...@@ -95,27 +95,9 @@ source "$net_forwarding_dir/../lib.sh" ...@@ -95,27 +95,9 @@ source "$net_forwarding_dir/../lib.sh"
# timeout in seconds # timeout in seconds
slowwait() slowwait()
{ {
local timeout=$1; shift local timeout_sec=$1; shift
local start_time="$(date -u +%s)"
while true
do
local out
out=$("$@")
local ret=$?
if ((!ret)); then
echo -n "$out"
return 0
fi
local current_time="$(date -u +%s)" loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
if ((current_time - start_time > timeout)); then
echo -n "$out"
return 1
fi
sleep 0.1
done
} }
############################################################################## ##############################################################################
......
...@@ -58,9 +58,10 @@ ksft_exit_status_merge() ...@@ -58,9 +58,10 @@ ksft_exit_status_merge()
$ksft_xfail $ksft_pass $ksft_skip $ksft_fail $ksft_xfail $ksft_pass $ksft_skip $ksft_fail
} }
busywait() loopy_wait()
{ {
local timeout=$1; shift local sleep_cmd=$1; shift
local timeout_ms=$1; shift
local start_time="$(date -u +%s%3N)" local start_time="$(date -u +%s%3N)"
while true while true
...@@ -74,13 +75,22 @@ busywait() ...@@ -74,13 +75,22 @@ busywait()
fi fi
local current_time="$(date -u +%s%3N)" local current_time="$(date -u +%s%3N)"
if ((current_time - start_time > timeout)); then if ((current_time - start_time > timeout_ms)); then
echo -n "$out" echo -n "$out"
return 1 return 1
fi fi
$sleep_cmd
done done
} }
busywait()
{
local timeout_ms=$1; shift
loopy_wait : "$timeout_ms" "$@"
}
cleanup_ns() cleanup_ns()
{ {
local ns="" local ns=""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment