This is an automated email from the ASF dual-hosted git repository.

houshengbo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e5cec1  Allow for commands to retry on network error. (#3776)
9e5cec1 is described below

commit 9e5cec1f0e4b98c59e584d453b06a741be4a8c3b
Author: rodric rabbah <rod...@gmail.com>
AuthorDate: Mon Jun 18 10:33:54 2018 -0400

    Allow for commands to retry on network error. (#3776)
    
    Add test for run cmd with retry.
---
 .../scala/src/main/scala/whisk/utils/Retry.scala   |  5 ++-
 tests/src/test/scala/common/TestUtils.java         |  2 +-
 tests/src/test/scala/common/Wsk.scala              | 46 +++++++++++-----------
 3 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/utils/Retry.scala 
b/common/scala/src/main/scala/whisk/utils/Retry.scala
index 9ab3102..2931e71 100644
--- a/common/scala/src/main/scala/whisk/utils/Retry.scala
+++ b/common/scala/src/main/scala/whisk/utils/Retry.scala
@@ -22,12 +22,13 @@ import scala.concurrent.duration._
 object retry {
 
   /**
-   * Retry a method which returns a value or throws an exception on failure, 
up to N times,
+   * Retries a method which returns a value or throws an exception on failure, 
up to N times,
    * and optionally sleeping up to specified duration between retries.
    *
-   * @param fn the method to retry, fn is expected to throw an exception if it 
fails, else should return a value of type T
+   * @param fn the function to retry; fn is expected to throw an exception if 
it fails, else should return a value of type T
    * @param N the maximum number of times to apply fn, must be >= 1
    * @param waitBeforeRetry an option specifying duration to wait before 
retrying method, will not wait if none given
+   * @param retryMessage an optional message to emit before retrying function
    * @return the result of fn iff it is successful
    * @throws Throwable exception from fn (or an illegal argument exception if 
N is < 1)
    */
diff --git a/tests/src/test/scala/common/TestUtils.java 
b/tests/src/test/scala/common/TestUtils.java
index d310ff4..a4e7a4b 100644
--- a/tests/src/test/scala/common/TestUtils.java
+++ b/tests/src/test/scala/common/TestUtils.java
@@ -57,9 +57,9 @@ public class TestUtils {
     public static final int SUCCESS_EXIT        = 0;
     public static final int ERROR_EXIT          = 1;
     public static final int MISUSE_EXIT         = 2;
+    public static final int NETWORK_ERROR_EXIT  = 3;
     public static final int DONTCARE_EXIT       = -1;       // any value is ok
     public static final int ANY_ERROR_EXIT      = -2;       // any non-zero 
value is ok
-    public static final int NETWORK_ERROR_EXIT  = 3;
 
     public static final int ACCEPTED        = 202;      // 202
     public static final int BAD_REQUEST     = 144;      // 400 - 256 = 144
diff --git a/tests/src/test/scala/common/Wsk.scala 
b/tests/src/test/scala/common/Wsk.scala
index 27ac636..a335f03 100644
--- a/tests/src/test/scala/common/Wsk.scala
+++ b/tests/src/test/scala/common/Wsk.scala
@@ -1032,32 +1032,22 @@ trait RunWskCmd extends BaseRunWsk {
           workingDir: File = new File("."),
           stdinFile: Option[File] = None,
           showCmd: Boolean = false,
-          hideFromOutput: Seq[String] = Seq()): RunResult = {
+          hideFromOutput: Seq[String] = Seq(),
+          retriesOnNetworkError: Int = 3): RunResult = {
     val args = baseCommand
     if (verbose) args += "--verbose"
     if (showCmd) println(args.mkString(" ") + " " + params.mkString(" "))
-    val rr =
-      retry(
-        {
-          val rr = TestUtils.runCmd(
-            DONTCARE_EXIT,
-            workingDir,
-            TestUtils.logger,
-            sys.env ++ env,
-            stdinFile.getOrElse(null),
-            args ++ params: _*)
-
-          if (expectedExitCode != NETWORK_ERROR_EXIT) {
-            withClue(hideStr(reportFailure(args ++ params, expectedExitCode, 
rr).toString(), hideFromOutput)) {
-              rr.exitCode should not be NETWORK_ERROR_EXIT
-            }
-          }
-
-          rr
-        },
-        3,
-        Some(1.second),
-        Some(s"CLI encountered a network error, retrying command..."))
+    val rr = retry(
+      0,
+      retriesOnNetworkError,
+      () =>
+        TestUtils.runCmd(
+          DONTCARE_EXIT,
+          workingDir,
+          TestUtils.logger,
+          sys.env ++ env,
+          stdinFile.getOrElse(null),
+          args ++ params: _*))
 
     withClue(hideStr(reportFailure(args ++ params, expectedExitCode, 
rr).toString(), hideFromOutput)) {
       if (expectedExitCode != TestUtils.DONTCARE_EXIT) {
@@ -1070,6 +1060,16 @@ trait RunWskCmd extends BaseRunWsk {
 
     rr
   }
+
+  /** Retries cmd on network error exit. */
+  private def retry(i: Int, N: Int, cmd: () => RunResult): RunResult = {
+    val rr = cmd()
+    if (rr.exitCode == NETWORK_ERROR_EXIT && i < N) {
+      Thread.sleep(1.second.toMillis)
+      println(s"command will retry to due to network error: $rr")
+      retry(i + 1, N, cmd)
+    } else rr
+  }
 }
 
 object WskAdmin {

-- 
To stop receiving notification emails like this one, please contact
houshen...@apache.org.

Reply via email to