kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From granthe...@apache.org
Subject [2/2] kudu git commit: [Java] Support re-running tests in the Gradle build
Date Mon, 09 Jul 2018 19:22:08 GMT
[Java] Support re-running tests in the Gradle build

Gradle does not have built in support for re-running tests like
Maven Surefire/Failsafe does. This patch adds a Junit Rule to
BaseKuduTest to support retries and configures the Gradle
build and Jenkins script to set the retry property to match
the Maven build.

Change-Id: Ie11e61c40ee991b00616012f0decaaf2c5fe5ef0
Reviewed-on: http://gerrit.cloudera.org:8080/10824
Reviewed-by: Adar Dembo <adar@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/c7271183
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/c7271183
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/c7271183

Branch: refs/heads/master
Commit: c727118320b03fc87befd9941841cdddf8b30762
Parents: eb9c644
Author: Grant Henke <granthenke@apache.org>
Authored: Tue Jun 26 11:08:00 2018 -0500
Committer: Grant Henke <granthenke@apache.org>
Committed: Mon Jul 9 19:20:11 2018 +0000

----------------------------------------------------------------------
 build-support/jenkins/build-and-test.sh         |  2 +-
 java/gradle/tests.gradle                        |  3 +
 .../org/apache/kudu/client/BaseKuduTest.java    | 16 ++++-
 .../java/org/apache/kudu/junit/RetryRule.java   | 74 ++++++++++++++++++++
 4 files changed, 93 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/c7271183/build-support/jenkins/build-and-test.sh
----------------------------------------------------------------------
diff --git a/build-support/jenkins/build-and-test.sh b/build-support/jenkins/build-and-test.sh
index cf60d40..e545394 100755
--- a/build-support/jenkins/build-and-test.sh
+++ b/build-support/jenkins/build-and-test.sh
@@ -389,7 +389,7 @@ if [ "$BUILD_JAVA" == "1" ]; then
 
   # Run the full Gradle build.
   if [ "$BUILD_GRADLE" == "1" ]; then
-     GRADLE_FLAGS="$GRADLE_FLAGS --console=plain --no-daemon"
+     GRADLE_FLAGS="$GRADLE_FLAGS --console=plain --no-daemon -DrerunFailingTestsCount=3"
      # TODO: Run `gradle check` in BUILD_TYPE DEBUG when static code analysis is fixed
      if ! ./gradlew $GRADLE_FLAGS clean test integrationTest ; then
        EXIT_STATUS=1

http://git-wip-us.apache.org/repos/asf/kudu/blob/c7271183/java/gradle/tests.gradle
----------------------------------------------------------------------
diff --git a/java/gradle/tests.gradle b/java/gradle/tests.gradle
index f826c2b..c2df581 100644
--- a/java/gradle/tests.gradle
+++ b/java/gradle/tests.gradle
@@ -57,6 +57,9 @@ tasks.withType(Test) {
   systemProperty "java.net.preferIPv4Stack", true
   systemProperty "java.security.egd", "file:/dev/urandom" // Improve RNG generation speed.
 
+  // Set rerunFailingTestsCount for use in BaseKuduTest.java to rerun failing tests
+  systemProperty "rerunFailingTestsCount", propertyWithDefault("rerunFailingTestsCount",
0)
+
   // Don't fail the build if a --tests filter doesn't match any tests.
   // This is useful for filtering tests from the top of the project where some
   // subprojects won't match the filter. Without this, those subprojects would fail.

http://git-wip-us.apache.org/repos/asf/kudu/blob/c7271183/java/kudu-client/src/test/java/org/apache/kudu/client/BaseKuduTest.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/apache/kudu/client/BaseKuduTest.java b/java/kudu-client/src/test/java/org/apache/kudu/client/BaseKuduTest.java
index 9078085..8d91e01 100644
--- a/java/kudu-client/src/test/java/org/apache/kudu/client/BaseKuduTest.java
+++ b/java/kudu-client/src/test/java/org/apache/kudu/client/BaseKuduTest.java
@@ -6,7 +6,8 @@
 // "License"); you may not use this file except in compliance
 // with the License.  You may obtain a copy of the License at
 //
-//   http://www.apache.org/licenses/LICENSE-2.0 //
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
 // Unless required by applicable law or agreed to in writing,
 // software distributed under the License is distributed on an
 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -27,8 +28,10 @@ import java.util.concurrent.TimeUnit;
 import com.google.common.base.Stopwatch;
 import com.google.common.net.HostAndPort;
 import com.stumbleupon.async.Deferred;
+import org.apache.kudu.junit.RetryRule;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Rule;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,6 +65,17 @@ public class BaseKuduTest {
   protected static final Schema basicSchema = getBasicSchema();
   protected static final Schema allTypesSchema = getSchemaWithAllTypes();
 
+  // Add a rule to rerun tests. We use this with Gradle because it doesn't support
+  // Surefire/Failsafe rerunFailingTestsCount like Maven does. We use the system
+  // property rerunFailingTestsCount to mimic the maven arguments closely.
+  private static final String RETRY_PROPERTY_NAME = "rerunFailingTestsCount";
+  private static final int DEFAULT_RETRY_COUNT = 0;
+  private static final String retryPropVal = System.getProperty(RETRY_PROPERTY_NAME);
+  private static final int retryCount =
+      retryPropVal != null ? Integer.parseInt(retryPropVal) : DEFAULT_RETRY_COUNT;
+  @Rule
+  public RetryRule retryRule = new RetryRule(retryCount);
+
   @Before
   public void setUpBase() throws Exception {
     FakeDNS.getInstance().install();

http://git-wip-us.apache.org/repos/asf/kudu/blob/c7271183/java/kudu-client/src/test/java/org/apache/kudu/junit/RetryRule.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/apache/kudu/junit/RetryRule.java b/java/kudu-client/src/test/java/org/apache/kudu/junit/RetryRule.java
new file mode 100644
index 0000000..24defed
--- /dev/null
+++ b/java/kudu-client/src/test/java/org/apache/kudu/junit/RetryRule.java
@@ -0,0 +1,74 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.kudu.junit;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * A JUnit rule to retry failed tests.
+ */
+public class RetryRule implements TestRule {
+  private int retryCount;
+
+  public RetryRule (int retryCount) {
+    this.retryCount = retryCount;
+  }
+
+  @Override
+  public Statement apply(Statement base, Description description) {
+    return new RetryStatement(base, description, retryCount);
+  }
+
+  private static class RetryStatement extends Statement {
+
+    private final Statement base;
+    private final Description description;
+    private final int retryCount;
+
+    RetryStatement(final Statement base, final Description description, final int retryCount)
{
+      this.base = base;
+      this.description = description;
+      this.retryCount = retryCount;
+    }
+
+    @Override
+    public void evaluate() throws Throwable {
+      // If there are no retries, just pass through to evaluate as usual.
+      if (retryCount == 0) {
+        base.evaluate();
+        return;
+      }
+
+      // To retry we catch the exception for the evaluate, log a message, and retry.
+      // We track and throw the last failure if all tries fail.
+      Throwable lastException = null;
+      for (int i = 0; i < retryCount; i++) {
+        try {
+          base.evaluate();
+          return;
+        } catch (Throwable t) {
+          lastException = t;
+          System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed.");
+        }
+      }
+      System.err.println(description.getDisplayName() + ": giving up after " + retryCount
+ " failures.");
+      throw lastException;
+    }
+  }
+}


Mime
View raw message