* [LTP] [PATCH] testcases/lib/test.sh: added tst_timeout()
@ 2014-11-27 15:10 Matus Marhefka
2014-12-02 12:38 ` Cyril Hrubis
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Matus Marhefka @ 2014-11-27 15:10 UTC (permalink / raw)
To: ltp-list
Function tst_timeout() added into LTP test interface:
tst_timeout "command arg1 arg2 ..." timeout
Function enables waiting for specified command for timeout
seconds. Example usage:
cmd_output=$(tst_timeout "ping -c 3 localhost" 5)
if [ $? -ne 0 ]; then
tst_brkm TBROK "timeout reached!"
fi
where $cmd_output contains stdout and stderr of ping command.
Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
testcases/lib/test.sh | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
index eecbfba..1c5bdf2 100644
--- a/testcases/lib/test.sh
+++ b/testcases/lib/test.sh
@@ -129,6 +129,60 @@ tst_check_cmds()
done
}
+# tst_timeout "command arg1 arg2 ..." timeout
+# Runs command for specified timeout (in seconds).
+# Function returns retcode of command or 1 if arguments are invalid.
+tst_timeout()
+{
+ local command=$1
+ local timeout=$2
+ local usleep_time=100000
+
+ # command must be non-empty string with command to run
+ if [ -z "$command" ]; then
+ echo "first argument must be non-empty string"
+ return 1
+ fi
+
+ # accept only numbers as timeout
+ re='^[0-9]+$'
+ if ! [[ $timeout =~ $re ]]; then
+ echo "only numbers as second argument"
+ return 1
+ fi
+
+ if [ -z "$TMPDIR" ]; then
+ TMPDIR="/tmp"
+ fi
+
+ local output=$(mktemp --tmpdir=$TMPDIR)
+ sh -c "eval $command" >$output 2>&1 &
+ local pid=$!
+ timeout=$((timeout*1000000))
+ while [ $timeout -gt 0 ]; do
+ kill -s 0 $pid 2>/dev/null
+ if [ $? -ne 0 ]; then
+ break
+ fi
+ timeout=$((timeout - usleep_time))
+ usleep $usleep_time
+ done
+
+ local ret=0
+ if [ $timeout -le 0 ]; then
+ ret=128
+ kill -9 $pid
+ fi
+
+ wait $pid
+ ret=$((ret | $?))
+
+ cat $output
+ rm -f $output
+
+ return $ret
+}
+
# Check that test name is set
if [ -z "$TCID" ]; then
tst_brkm TBROK "TCID is not defined"
--
1.8.3.1
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH] testcases/lib/test.sh: added tst_timeout()
2014-11-27 15:10 [LTP] [PATCH] testcases/lib/test.sh: added tst_timeout() Matus Marhefka
@ 2014-12-02 12:38 ` Cyril Hrubis
[not found] ` <1961735972.12031389.1417528277602.JavaMail.zimbra@redhat.com>
[not found] ` <54803C62.30803@redhat.com>
2014-12-04 11:03 ` [LTP] [PATCH v2] " Matus Marhefka
[not found] ` <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>
2 siblings, 2 replies; 8+ messages in thread
From: Cyril Hrubis @ 2014-12-02 12:38 UTC (permalink / raw)
To: Matus Marhefka; +Cc: ltp-list
Hi!
> Function tst_timeout() added into LTP test interface:
>
> tst_timeout "command arg1 arg2 ..." timeout
>
> Function enables waiting for specified command for timeout
> seconds. Example usage:
>
> cmd_output=$(tst_timeout "ping -c 3 localhost" 5)
> if [ $? -ne 0 ]; then
> tst_brkm TBROK "timeout reached!"
> fi
>
> where $cmd_output contains stdout and stderr of ping command.
Nice idea, comments below.
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
> testcases/lib/test.sh | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 54 insertions(+)
>
> diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
> index eecbfba..1c5bdf2 100644
> --- a/testcases/lib/test.sh
> +++ b/testcases/lib/test.sh
> @@ -129,6 +129,60 @@ tst_check_cmds()
> done
> }
>
> +# tst_timeout "command arg1 arg2 ..." timeout
> +# Runs command for specified timeout (in seconds).
> +# Function returns retcode of command or 1 if arguments are invalid.
> +tst_timeout()
> +{
> + local command=$1
> + local timeout=$2
> + local usleep_time=100000
> +
> + # command must be non-empty string with command to run
> + if [ -z "$command" ]; then
> + echo "first argument must be non-empty string"
> + return 1
> + fi
> +
> + # accept only numbers as timeout
> + re='^[0-9]+$'
> + if ! [[ $timeout =~ $re ]]; then
This is bashism. The portable way to do this seems to be do
tr -d [:digit:] on the string and checking if result is empty or use
grep.
> + echo "only numbers as second argument"
> + return 1
> + fi
> +
> + if [ -z "$TMPDIR" ]; then
> + TMPDIR="/tmp"
> + fi
I guess that better solution would be to move this check from the
tst_tmpdir() after the LTPROOT check in the test.sh so that we have only
one copy of it.
> + local output=$(mktemp --tmpdir=$TMPDIR)
> + sh -c "eval $command" >$output 2>&1 &
Do we really need to redirect the output to the file here? Shouldn't
just redirecting stderr to stdout work?
> + local pid=$!
> + timeout=$((timeout*1000000))
> + while [ $timeout -gt 0 ]; do
> + kill -s 0 $pid 2>/dev/null
> + if [ $? -ne 0 ]; then
> + break
> + fi
> + timeout=$((timeout - usleep_time))
> + usleep $usleep_time
The usleep is bashism as well. What is not POSIX but tend to work is
sleep with a real number, i.e. sleep 0.1 but it does not seems to be
supported under BussyBox.
So we can either do the polling once a second or create our own
tst_usleep C source that calls usleep().
Ah and I've found that coreutils even includes timeout command that
can be used to fairly simplify the code.
Try:
timeout $timeout sh -c "eval $command"
echo $?
> + done
> +
> + local ret=0
> + if [ $timeout -le 0 ]; then
> + ret=128
> + kill -9 $pid
> +
> + fi
> +
> + wait $pid
> + ret=$((ret | $?))
> +
> + cat $output
> + rm -f $output
> +
> + return $ret
> +}
> +
> # Check that test name is set
> if [ -z "$TCID" ]; then
> tst_brkm TBROK "TCID is not defined"
> --
> 1.8.3.1
>
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
> http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 8+ messages in thread* [LTP] [PATCH v2] testcases/lib/test.sh: added tst_timeout()
2014-11-27 15:10 [LTP] [PATCH] testcases/lib/test.sh: added tst_timeout() Matus Marhefka
2014-12-02 12:38 ` Cyril Hrubis
@ 2014-12-04 11:03 ` Matus Marhefka
2014-12-18 13:30 ` Cyril Hrubis
[not found] ` <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>
2 siblings, 1 reply; 8+ messages in thread
From: Matus Marhefka @ 2014-12-04 11:03 UTC (permalink / raw)
To: ltp-list
Function tst_timeout() added into LTP test interface:
tst_timeout "command arg1 arg2 ..." timeout
Function enables waiting for specified command for timeout
seconds. Example usage:
cmd_output=$(tst_timeout "ping -c 3 localhost" 5)
if [ $? -ne 0 ]; then
tst_brkm TBROK "timeout reached!"
fi
where $cmd_output contains stdout and stderr of ping command.
Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
testcases/lib/test.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
index eecbfba..d31fcb8 100644
--- a/testcases/lib/test.sh
+++ b/testcases/lib/test.sh
@@ -129,6 +129,50 @@ tst_check_cmds()
done
}
+# tst_timeout "command arg1 arg2 ..." timeout
+# Runs command for specified timeout (in seconds).
+# Function returns retcode of command or 1 if arguments are invalid.
+tst_timeout()
+{
+ local command=$1
+ local timeout=$(echo $2 | grep -o "^[0-9]\+$")
+
+ # command must be non-empty string with command to run
+ if [ -z "$command" ]; then
+ echo "first argument must be non-empty string"
+ return 1
+ fi
+
+ # accept only numbers as timeout
+ if [ -z "$timeout" ]; then
+ echo "only numbers as second argument"
+ return 1
+ fi
+
+ setsid sh -c "eval $command" 2>&1 &
+ local pid=$!
+ local pgid=$(ps -eo pid,pgid | grep $pid | sed 's/^.* //')
+ while [ $timeout -gt 0 ]; do
+ kill -s 0 $pid 2>/dev/null
+ if [ $? -ne 0 ]; then
+ break
+ fi
+ timeout=$((timeout - 1))
+ sleep 1
+ done
+
+ local ret=0
+ if [ $timeout -le 0 ]; then
+ ret=128
+ kill -TERM -- -$pgid
+ fi
+
+ wait $pid
+ ret=$((ret | $?))
+
+ return $ret
+}
+
# Check that test name is set
if [ -z "$TCID" ]; then
tst_brkm TBROK "TCID is not defined"
--
1.8.3.1
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH v2] testcases/lib/test.sh: added tst_timeout()
2014-12-04 11:03 ` [LTP] [PATCH v2] " Matus Marhefka
@ 2014-12-18 13:30 ` Cyril Hrubis
0 siblings, 0 replies; 8+ messages in thread
From: Cyril Hrubis @ 2014-12-18 13:30 UTC (permalink / raw)
To: Matus Marhefka; +Cc: ltp-list
Hi!
> +# tst_timeout "command arg1 arg2 ..." timeout
> +# Runs command for specified timeout (in seconds).
> +# Function returns retcode of command or 1 if arguments are invalid.
> +tst_timeout()
> +{
> + local command=$1
> + local timeout=$(echo $2 | grep -o "^[0-9]\+$")
> +
> + # command must be non-empty string with command to run
> + if [ -z "$command" ]; then
> + echo "first argument must be non-empty string"
> + return 1
> + fi
> +
> + # accept only numbers as timeout
> + if [ -z "$timeout" ]; then
> + echo "only numbers as second argument"
> + return 1
> + fi
> +
> + setsid sh -c "eval $command" 2>&1 &
> + local pid=$!
> + local pgid=$(ps -eo pid,pgid | grep $pid | sed 's/^.* //')
Hmm, wouldn't be the $pid == $pgid in this case? Or is there a scenario
where this would not hold?
Otherwise it looks good to me.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>]
* Re: [LTP] [PATCH v3] testcases/lib/test.sh: added tst_timeout()
[not found] ` <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>
@ 2014-12-22 0:53 ` Wanlong Gao
2015-01-05 14:03 ` Cyril Hrubis
1 sibling, 0 replies; 8+ messages in thread
From: Wanlong Gao @ 2014-12-22 0:53 UTC (permalink / raw)
To: Matus Marhefka, ltp-list
On 12/19/2014 07:11 PM, Matus Marhefka wrote:
> Function tst_timeout() added into LTP test interface:
>
> tst_timeout "command arg1 arg2 ..." timeout
>
> Function enables waiting for specified command for timeout
> seconds. Example usage:
>
> cmd_output=$(tst_timeout "ping -c 3 localhost" 5)
> if [ $? -ne 0 ]; then
> tst_brkm TBROK "timeout reached!"
> fi
>
> where $cmd_output contains stdout and stderr of ping command.
>
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
Nice function.
Acked-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
> ---
> testcases/lib/test.sh | 43 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 43 insertions(+)
>
> diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
> index eecbfba..ad78a87 100644
> --- a/testcases/lib/test.sh
> +++ b/testcases/lib/test.sh
> @@ -129,6 +129,49 @@ tst_check_cmds()
> done
> }
>
> +# tst_timeout "command arg1 arg2 ..." timeout
> +# Runs command for specified timeout (in seconds).
> +# Function returns retcode of command or 1 if arguments are invalid.
> +tst_timeout()
> +{
> + local command=$1
> + local timeout=$(echo $2 | grep -o "^[0-9]\+$")
> +
> + # command must be non-empty string with command to run
> + if [ -z "$command" ]; then
> + echo "first argument must be non-empty string"
> + return 1
> + fi
> +
> + # accept only numbers as timeout
> + if [ -z "$timeout" ]; then
> + echo "only numbers as second argument"
> + return 1
> + fi
> +
> + setsid sh -c "eval $command" 2>&1 &
> + local pid=$!
> + while [ $timeout -gt 0 ]; do
> + kill -s 0 $pid 2>/dev/null
> + if [ $? -ne 0 ]; then
> + break
> + fi
> + timeout=$((timeout - 1))
> + sleep 1
> + done
> +
> + local ret=0
> + if [ $timeout -le 0 ]; then
> + ret=128
> + kill -TERM -- -$pid
> + fi
> +
> + wait $pid
> + ret=$((ret | $?))
> +
> + return $ret
> +}
> +
> # Check that test name is set
> if [ -z "$TCID" ]; then
> tst_brkm TBROK "TCID is not defined"
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH v3] testcases/lib/test.sh: added tst_timeout()
[not found] ` <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>
2014-12-22 0:53 ` [LTP] [PATCH v3] " Wanlong Gao
@ 2015-01-05 14:03 ` Cyril Hrubis
1 sibling, 0 replies; 8+ messages in thread
From: Cyril Hrubis @ 2015-01-05 14:03 UTC (permalink / raw)
To: Matus Marhefka; +Cc: ltp-list
Hi!
> Function tst_timeout() added into LTP test interface:
>
> tst_timeout "command arg1 arg2 ..." timeout
>
> Function enables waiting for specified command for timeout
> seconds. Example usage:
>
> cmd_output=$(tst_timeout "ping -c 3 localhost" 5)
> if [ $? -ne 0 ]; then
> tst_brkm TBROK "timeout reached!"
> fi
>
> where $cmd_output contains stdout and stderr of ping command.
Pushed, thanks.
It would be a nice to have a paragraph about the usage in the test
writing guidelines. Can you send a patch for that too?
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-01-05 14:04 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-27 15:10 [LTP] [PATCH] testcases/lib/test.sh: added tst_timeout() Matus Marhefka
2014-12-02 12:38 ` Cyril Hrubis
[not found] ` <1961735972.12031389.1417528277602.JavaMail.zimbra@redhat.com>
2014-12-02 14:04 ` Cyril Hrubis
[not found] ` <54803C62.30803@redhat.com>
2014-12-04 11:23 ` Cyril Hrubis
2014-12-04 11:03 ` [LTP] [PATCH v2] " Matus Marhefka
2014-12-18 13:30 ` Cyril Hrubis
[not found] ` <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>
2014-12-22 0:53 ` [LTP] [PATCH v3] " Wanlong Gao
2015-01-05 14:03 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox