Skip to contentSkip to conversation

tst_test.sh: achieve TST_RETRY_FUNC function in shell

PATCH Superseded Li Wang 2 participants 5 messages May 3, 2018 – May 17, 2018 1 message not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

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

The commit c2ce4df67d(include: add an exponential backoff macro for function retry) involves a new MACRO for function retry in C code, here achieve it in shell lib and gives a introduction in LTP documents.

---
 doc/test-writing-guidelines.txt | 26 ++++++++++++++++++++++++++
 testcases/lib/tst_test.sh       | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
doc/test-writing-guidelines.txt +26 -0
index cbbfe6c..b2dd091 100644
@@ -1640,6 +1640,32 @@ 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 a limited time,
+test will break from the retries immediately.
+
+[source,c]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC(FUNC, EXPECTED_RET)
+
+# retry function in N second
+TST_RETRY_FN_EXP_BACKOFF(FUNC, EXPECTED_RET, N)
+-------------------------------------------------------------------------------
+
+[source,sh]
+-------------------------------------------------------------------------------
+# retry function in 1 second
+TST_RETRY_FUNC "FUNC arg1 arg2 ..." "EXPECTED_RET"
+
+# retry function in N second
+TST_RETRY_FN_EXP_BACKOFF "FUNC arg1 arg2 ..." "EXPECTED_RET" "N"
+-------------------------------------------------------------------------------
+
 Checking for integers
 +++++++++++++++++++++
 
testcases/lib/tst_test.sh +34 -0
index 8d49d34..8b38dcd 100644
@@ -154,6 +154,40 @@ 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
+
+	if [ $# -ne 3 ]; then
+		tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF expects three parameters"
+	fi
+
+	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 v2 1/1 tst_test.sh: achieve TST_RETRY_FUNC function in shell

Hi Li,

Quoted 7 lines

The commit c2ce4df67d(include: add an exponential backoff macro for function retry) involves a new MACRO for function retry in C code, here achieve it in shell lib and gives a introduction in LTP documents.

---

...

testcases/lib/tst_test.sh EXPECT_FAIL() · line 160

Quoted 9 lines

+++ b/testcases/lib/tst_test.sh

testcases/lib/tst_test.sh +4 -0
@@ -154,6 +154,40 @@ EXPECT_FAIL()
 	fi
 }
 
+TST_RETRY_FN_EXP_BACKOFF()
+{
+	local tst_fun=$1
+	local tst_exp=$2

Maybe add check for int, something like this?
if ! tst_is_int "$tst_sec"; then
tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF: tst_sec must be integer ('$tst_sec')"
fi

testcases/lib/tst_test.sh EXPECT_FAIL() · line 165

Quoted 5 lines
testcases/lib/tst_test.sh +5 -0
+	local tst_sec=$(expr $3 \* 1000000)
+	local tst_delay=1
+
+	if [ $# -ne 3 ]; then
+		tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF expects three parameters"

I'd personally use number than word, but feel free to ignore that.
tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF expects 3 parameters"
I'm not sure if checking
...

testcases/lib/tst_test.sh EXPECT_FAIL() · line 186

Quoted 2 lines
testcases/lib/tst_test.sh +2 -0
+TST_RETRY_FUNC()
+{

TST_RETRY_FUNC needs check for number of params as well:

	if [ $# -ne 2 ]; then
		tst_brk TBROK "TST_RETRY_FUNC expects 2 parameters"
	fi

testcases/lib/tst_test.sh EXPECT_FAIL() · line 187

Quoted 1 line
testcases/lib/tst_test.sh +1 -0
+	TST_RETRY_FN_EXP_BACKOFF "$1" "$2" 1

Otherwise you run TST_RETRY_FUNC without params and didn't get check. some-test 1 TBROK: "" failed

Kind regards,
Petr

A message that is not in the archive (sent off-list or not yet received): CAEemH2ccJR4aiowoogZ5nmOdQFvcCzUbE0k4A+5h=mW=XVgVnQ@mail.gmail.com

author
#3 lore raw

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

Ah, forgot to CC' LTP list.

---------- Forwarded message ----------
From: Li Wang <[email protected]>
Date: Thu, May 17, 2018 at 1:03 PM
Subject: Re: [LTP] [PATCH v2] tst_test.sh: achieve TST_RETRY_FUNC function in shell
To: Petr Vorel <[email protected]>

Hi Petr,

Quoted 15 lines · Petr Vorel

Hi Li,

Quoted 7 lines

The commit c2ce4df67d(include: add an exponential backoff macro for function retry) involves a new MACRO for function retry in C code, here achieve it in shell lib and gives a introduction in LTP documents.

---

...

Quoted 4 lines

+++ b/testcases/lib/tst_test.sh

(quoted diff) +0 -0
@@ -154,6 +154,40 @@ EXPECT_FAIL()
      fi
 }
Quoted 11 lines

testcases/lib/tst_test.sh EXPECT_FAIL() · line 160

Quoted 4 lines
testcases/lib/tst_test.sh +4 -0
+TST_RETRY_FN_EXP_BACKOFF()
+{
+     local tst_fun=$1
+     local tst_exp=$2

Maybe add check for int, something like this?
if ! tst_is_int "$tst_sec"; then
tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF: tst_sec must be
integer ('$tst_sec')"
fi

​Sounds good. If do this, I'd put the check after tst_sec assignment. ​

Quoted 5 lines

testcases/lib/tst_test.sh EXPECT_FAIL()

Quoted 5 lines
testcases/lib/tst_test.sh +5 -0
+     local tst_sec=$(expr $3 \* 1000000)
+     local tst_delay=1
+
+     if [ $# -ne 3 ]; then
+             tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF expects three
Quoted 7 lines

parameters"
I'd personally use number than word, but feel free to ignore that.
tst_brk TBROK "TST_RETRY_FN_EXP_BACKOFF expects 3
parameters"
I'm not sure if checking
...

​Ok, fine.
​

Quoted 5 lines

testcases/lib/tst_test.sh EXPECT_FAIL() · line 186

Quoted 3 lines
testcases/lib/tst_test.sh +2 -0
+TST_RETRY_FUNC()
+{

TST_RETRY_FUNC needs check for number of params as well:

Quoted 8 lines

if [ $# -ne 2 ]; then
tst_brk TBROK "TST_RETRY_FUNC expects 2 parameters"
fi

testcases/lib/tst_test.sh EXPECT_FAIL() · line 187

Quoted 1 line
testcases/lib/tst_test.sh +1 -0
+     TST_RETRY_FN_EXP_BACKOFF "$1" "$2" 1

Otherwise you run TST_RETRY_FUNC without params and didn't get check. some-test 1 TBROK: "" failed

​Agree.
​

Quoted 5 lines

Kind regards,
Petr

Signature · 11 lines
Regards,
Li Wang



-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20180517/ece73c9a/attachment-0001.html>
author
#4 lore raw

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

Hi Petr,

​And also, we'd better skip the TST_ prefix name check in test library, otherwise we will get LTP warnings like:

<<<test_output>>>
numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used! numa01 1 TINFO: The system contains 2 nodes: 0 1 numa01 1 TPASS: NUMA local node and memory affinity numa01 2 TPASS: NUMA preferred node policy
numa01 3 TPASS: NUMA share memory allocated in preferred node numa01 4 TPASS: NUMA interleave policy
numa01 5 TPASS: NUMA interleave policy on shared memory numa01 6 TPASS: NUMA phycpubind policy
numa01 7 TPASS: NUMA local node allocation
numa01 8 TPASS: NUMA MEMHOG policy

----------------
tst_umount()

 {
        local device="$1"
@@ -255,6 +297,7 @@ tst_run()
                        OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
                        NEEDS_ROOT|NEEDS_TMPDIR|NEEDS_DEVICE|DEVICE);;
                        NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);;
+                       RETRY_FUNC|RETRY_FN_EXP_BACKOFF);;

IPV6);;
*) tst_res TWARN "Reserved variable TST_$tst_i
used!";;
esac
​

​I prefer to merge this change along with your suggestions in PATCH v3 as well.

Thanks,
Li Wang​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20180517/04f5a587/attachment.html>

#5 lore raw

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

Hi Li,

Quoted 7 lines

Hi Petr,

And also, we'd better skip the TST_ prefix name check in test library, otherwise we will get LTP warnings like:

<<<test_output>>>
numa01 1 TWARN: Reserved variable TST_RETRY_FUNC used!

I'm sorry I've noticed it but forget to report it.
...

Quoted 9 lines
(quoted diff) +1 -0
@@ -255,6 +297,7 @@ tst_run()
                        OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
                        NEEDS_ROOT|NEEDS_TMPDIR|NEEDS_DEVICE|DEVICE);;
                        NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);;
+                       RETRY_FUNC|RETRY_FN_EXP_BACKOFF);;
                        IPV6);;
 

I prefer to merge this change along with your suggestions in PATCH v3 as well.

Sure!

Quoted 2 lines

Thanks,
Li Wang

Kind regards,
Petr