All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wanlong Gao <gaowanlong@cn.fujitsu.com>
To: Matus Marhefka <mmarhefk@redhat.com>, ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH v3] testcases/lib/test.sh: added tst_timeout()
Date: Mon, 22 Dec 2014 08:53:21 +0800	[thread overview]
Message-ID: <54976B81.20203@cn.fujitsu.com> (raw)
In-Reply-To: <1418987492-17553-1-git-send-email-mmarhefk@redhat.com>

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

  parent reply	other threads:[~2014-12-22  0:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Wanlong Gao [this message]
2015-01-05 14:03   ` [LTP] [PATCH v3] " Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54976B81.20203@cn.fujitsu.com \
    --to=gaowanlong@cn.fujitsu.com \
    --cc=ltp-list@lists.sourceforge.net \
    --cc=mmarhefk@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.