Skip to content

Commit

Permalink
selftests: net: Unify code of busywait() and slowwait()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Petr Machata authored and Paolo Abeni committed Apr 16, 2024
1 parent 62d6d91 commit a4022a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
22 changes: 2 additions & 20 deletions tools/testing/selftests/net/forwarding/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,9 @@ source "$net_forwarding_dir/../lib.sh"
# timeout in seconds
slowwait()
{
local timeout=$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 timeout_sec=$1; shift

local current_time="$(date -u +%s)"
if ((current_time - start_time > timeout)); then
echo -n "$out"
return 1
fi

sleep 0.1
done
loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
}

##############################################################################
Expand Down
16 changes: 13 additions & 3 deletions tools/testing/selftests/net/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,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
Expand All @@ -74,13 +75,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=""
Expand Down

0 comments on commit a4022a3

Please sign in to comment.