From patchwork Thu May 3 03:34:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Wang X-Patchwork-Id: 907812 X-Patchwork-Delegate: petr.vorel@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40c13S2bnRz9s37 for ; Thu, 3 May 2018 13:34:26 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 2539B3E70C2 for ; Thu, 3 May 2018 05:34:24 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id F39873E7097 for ; Thu, 3 May 2018 05:34:21 +0200 (CEST) Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id AB7E610009F7 for ; Thu, 3 May 2018 05:34:19 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A2C66406C748 for ; Thu, 3 May 2018 03:34:17 +0000 (UTC) Received: from dhcp-12-102.nay.redhat.com (unknown [10.66.12.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id B12742017F00 for ; Thu, 3 May 2018 03:34:16 +0000 (UTC) From: Li Wang To: ltp@lists.linux.it Date: Thu, 3 May 2018 11:34:14 +0800 Message-Id: <20180503033414.5601-1-liwang@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 03 May 2018 03:34:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 03 May 2018 03:34:17 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'liwang@redhat.com' RCPT:'' X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH v2] tst_test.sh: achieve TST_RETRY_FUNC function in shell X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 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. Signed-off-by: Li Wang Tested-by: Petr Vorel --- doc/test-writing-guidelines.txt | 26 ++++++++++++++++++++++++++ testcases/lib/tst_test.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt index cbbfe6c..b2dd091 100644 --- a/doc/test-writing-guidelines.txt +++ b/doc/test-writing-guidelines.txt @@ -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 +++++++++++++++++++++ diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh index 8d49d34..8b38dcd 100644 --- a/testcases/lib/tst_test.sh +++ b/testcases/lib/tst_test.sh @@ -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"