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

BugLink: https://bugs.launchpad.net/bugs/2073603

[ Upstream commit a4022a332f437ae5b10921d66058ce98a2db2c20 ]

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: Petr Machata <petrm@nvidia.com>
Reviewed-by: Benjamin Poirier <bpoirier@nvidia.com>
Reviewed-by: Hangbin Liu <liuhangbin@gmail.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Stable-dep-of: ea63ac142925 ("selftests/net: use tc rule to filter the na packet")
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Portia Stephens <portia.stephens@canonical.com>
Signed-off-by: Roxana Nicolescu <roxana.nicolescu@canonical.com>
This commit is contained in:
Petr Machata
2024-04-12 19:03:04 +02:00
committed by Roxana Nicolescu
parent 9f59bc6dbc
commit 6f07821bd1
2 changed files with 15 additions and 21 deletions
+2 -18
View File
@@ -43,25 +43,9 @@ ksft_skip=4
busywait()
{
local timeout=$1; shift
local timeout_sec=$1; shift
local start_time="$(date -u +%s%3N)"
while true
do
local out
out=$("$@")
local ret=$?
if ((!ret)); then
echo -n "$out"
return 0
fi
local current_time="$(date -u +%s%3N)"
if ((current_time - start_time > timeout)); then
echo -n "$out"
return 1
fi
done
loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
}
##############################################################################
+13 -3
View File
@@ -53,9 +53,10 @@ ksft_exit_status_merge()
$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)"
while true
@@ -69,13 +70,22 @@ busywait()
fi
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"
return 1
fi
$sleep_cmd
done
}
busywait()
{
local timeout_ms=$1; shift
loopy_wait : "$timeout_ms" "$@"
}
cleanup_ns()
{
local ns=""