Skip to contentSkip to conversation

tst_test.sh: achieve TST_RETRY_FUNC function in shell

PATCH New Li Wang 2 participants 3 messages May 2, 2018 – May 3, 2018

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 tst_test.sh: achieve TST_RETRY_FUNC function in shell

---
 doc/test-writing-guidelines.txt | 20 ++++++++++++++++++++
 testcases/lib/tst_test.sh       | 30 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
doc/test-writing-guidelines.txt +20 -0
index cbbfe6c..318eca6 100644
@@ -1640,6 +1640,26 @@ that can sleep for defined amount of seconds, milliseconds or microseconds.
 tst_sleep 100ms
 -------------------------------------------------------------------------------
 
+Retry a function in limited time
+++++++++++++++++++++++++++++++++
+
+Sometimes LTP test needs retrying a function for many times to get success.
+This achievement makes that possible via keeping it retrying if the return
+value of the function is NOT as we expected. After exceeding one second, the
+test will break from the retries immediately.
+
+[source,c]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC(FUNC, ERET)
+-------------------------------------------------------------------------------
+
+[source,sh]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC "FUNC arg1 arg2" "ERET"
+-------------------------------------------------------------------------------
+
 Checking for integers
 +++++++++++++++++++++
 
testcases/lib/tst_test.sh +30 -0
index 8d49d34..d03ed04 100644
@@ -154,6 +154,36 @@ EXPECT_FAIL()
 	fi
 }
 
+TST_RETRY_FN_EXP_BACKOFF()
+{
+	local tst_fun=$1
+	local tst_exp=$2
+	local tst_sec=$(expr $3 \* 1000000)
+	local tst_delay=1
+
+	while true; do
+		$tst_fun
+		if [ "$?" == "$tst_exp" ]; then
+			break
+		fi
+
+		if [ $tst_delay -lt $tst_sec ]; then
+			tst_sleep ${tst_delay}us
+			tst_delay=$((tst_delay*2))
+		else
+			tst_brk TBROK "$tst_fun failed"
+		fi
+	done
+
+	return $tst_exp
+}
+
+TST_RETRY_FUNC()
+{
+	TST_RETRY_FN_EXP_BACKOFF "$1" "$2" 1
+	return $2
+}
+
 tst_umount()
 {
 	local device="$1"
Signature · 1 line
2.9.5
#1 lore raw

PATCH 1/1 tst_test.sh: achieve TST_RETRY_FUNC function in shell

Hi Li,

nice feature, thanks for adding it.
1 bashism to be fixed + minor issues bellow.

Quoted 1 line
Quoted 15 lines
---
 doc/test-writing-guidelines.txt | 20 ++++++++++++++++++++
 testcases/lib/tst_test.sh       | 30 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
doc/test-writing-guidelines.txt +2 -0
index cbbfe6c..318eca6 100644
@@ -1640,6 +1640,26 @@ that can sleep for defined amount of seconds, milliseconds or microseconds.
 tst_sleep 100ms
 -------------------------------------------------------------------------------
 
+Retry a function in limited time
+++++++++++++++++++++++++++++++++

doc/test-writing-guidelines.txt that can sleep for defined amount of seconds, milliseconds or microseconds. · line 1654

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+
+Sometimes LTP test needs retrying a function for many times to get success.
+This achievement makes that possible via keeping it retrying if the return
+value of the function is NOT as we expected. After exceeding one second, the
+test will break from the retries immediately.
+
+[source,c]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC(FUNC, ERET)

C docs should have been added in c2ce4df67d. Maybe you could just mention this in commit message.

doc/test-writing-guidelines.txt that can sleep for defined amount of seconds, milliseconds or microseconds. · line 1660

Quoted 6 lines
doc/test-writing-guidelines.txt +6 -0
+-------------------------------------------------------------------------------
+
+[source,sh]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC "FUNC arg1 arg2" "ERET"

Maybe EXPECTED_RET to be clearer?

doc/test-writing-guidelines.txt that can sleep for defined amount of seconds, milliseconds or microseconds.

Quoted 9 lines
doc/test-writing-guidelines.txt +6 -0
+-------------------------------------------------------------------------------
+
 Checking for integers
 +++++++++++++++++++++
 
+TST_RETRY_FN_EXP_BACKOFF()
+{
+	local tst_fun=$1
+	local tst_exp=$2

I'd check number of params ($#) and tst_brk to warn user about improper usage. And mention TST_RETRY_FN_EXP_BACKOFF() in docs?

doc/test-writing-guidelines.txt

Quoted 6 lines
doc/test-writing-guidelines.txt +6 -0
+	local tst_sec=$(expr $3 \* 1000000)
+	local tst_delay=1
+
+	while true; do
+		$tst_fun
+		if [ "$?" == "$tst_exp" ]; then
                  ^
This is bashism, it's broken in dash. Please use single equal sign:
		if [ "$?" = "$tst_exp" ]; then
Quoted 13 lines
doc/test-writing-guidelines.txt +13 -0
+			break
+		fi
+
+		if [ $tst_delay -lt $tst_sec ]; then
+			tst_sleep ${tst_delay}us
+			tst_delay=$((tst_delay*2))
+		else
+			tst_brk TBROK "$tst_fun failed"
+		fi
+	done
+
+	return $tst_exp
+}

doc/test-writing-guidelines.txt

Quoted 10 lines
doc/test-writing-guidelines.txt +7 -0
+
+TST_RETRY_FUNC()
+{
+	TST_RETRY_FN_EXP_BACKOFF "$1" "$2" 1
+	return $2
+}
+
 tst_umount()
 {
 	local device="$1"

Kind regards,
Petr

author
#2 lore raw

PATCH 1/1 tst_test.sh: achieve TST_RETRY_FUNC function in shell

Petr Vorel <[email protected]> wrote:

Hi Li,

Quoted 4 lines

nice feature, thanks for adding it.
1 bashism to be fixed + minor issues bellow.

​Hi Petr,​

​Thanks for your testing, I'm glad to accept your comments all below.​ ​Patch V2 is coming soon...​

Signature · 5 lines
Li Wang
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20180503/86b9dfd3/attachment.html>