diff mbox series

[net-next,01/10] selftests: net: Unify code of busywait() and slowwait()

Message ID db8b8885e254893bba61d824d7cf2a6774dcb336.1712940759.git.petrm@nvidia.com (mailing list archive)
State Accepted
Commit a4022a332f437ae5b10921d66058ce98a2db2c20
Delegated to: Netdev Maintainers
Headers show
Series selftests: Assortment of fixes | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success net selftest script(s) already in Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 64 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-15--15-00 (tests: 961)

Commit Message

Petr Machata April 12, 2024, 5:03 p.m. UTC
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>
---
 tools/testing/selftests/net/forwarding/lib.sh | 22 ++-----------------
 tools/testing/selftests/net/lib.sh            | 16 +++++++++++---
 2 files changed, 15 insertions(+), 23 deletions(-)

Comments

Hangbin Liu April 15, 2024, 7:21 a.m. UTC | #1
On Fri, Apr 12, 2024 at 07:03:04PM +0200, Petr Machata wrote:
> 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>
> ---
>  tools/testing/selftests/net/forwarding/lib.sh | 22 ++-----------------
>  tools/testing/selftests/net/lib.sh            | 16 +++++++++++---
>  2 files changed, 15 insertions(+), 23 deletions(-)
> 
> diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testing/selftests/net/forwarding/lib.sh
> index 4103ed7afcde..658e4e7bf4b9 100644
> --- a/tools/testing/selftests/net/forwarding/lib.sh
> +++ b/tools/testing/selftests/net/forwarding/lib.sh
> @@ -95,27 +95,9 @@ source "$net_forwarding_dir/../lib.sh"
>  # timeout in seconds
>  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)"
> -		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))" "$@"
>  }
>  
>  ##############################################################################
> diff --git a/tools/testing/selftests/net/lib.sh b/tools/testing/selftests/net/lib.sh
> index b7f7b8695165..c868c0aec121 100644
> --- a/tools/testing/selftests/net/lib.sh
> +++ b/tools/testing/selftests/net/lib.sh
> @@ -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
> @@ -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=""
> -- 
> 2.43.0
> 

Reviewed-by: Hangbin Liu <liuhangbin@gmail.com>
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testing/selftests/net/forwarding/lib.sh
index 4103ed7afcde..658e4e7bf4b9 100644
--- a/tools/testing/selftests/net/forwarding/lib.sh
+++ b/tools/testing/selftests/net/forwarding/lib.sh
@@ -95,27 +95,9 @@  source "$net_forwarding_dir/../lib.sh"
 # timeout in seconds
 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)"
-		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))" "$@"
 }
 
 ##############################################################################
diff --git a/tools/testing/selftests/net/lib.sh b/tools/testing/selftests/net/lib.sh
index b7f7b8695165..c868c0aec121 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -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
@@ -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=""