You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2015/09/23 14:05:07 UTC

flink git commit: [FLINK-2746] [tests] Add RetryOnException annotation for tests

Repository: flink
Updated Branches:
  refs/heads/master ca542057a -> 1f17ff540


[FLINK-2746] [tests] Add RetryOnException annotation for tests


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

Branch: refs/heads/master
Commit: 1f17ff5408ad0d8821b9b1a8a2d8ddf27be783cd
Parents: ca54205
Author: Stephan Ewen <se...@apache.org>
Authored: Wed Sep 23 13:28:16 2015 +0200
Committer: Stephan Ewen <se...@apache.org>
Committed: Wed Sep 23 14:04:36 2015 +0200

----------------------------------------------------------------------
 .../flink/testutils/junit/RetryOnException.java | 60 ++++++++++++++
 .../testutils/junit/RetryOnExceptionTest.java   | 83 ++++++++++++++++++++
 .../flink/testutils/junit/RetryOnFailure.java   |  6 +-
 .../apache/flink/testutils/junit/RetryRule.java | 70 +++++++++++++++--
 4 files changed, 209 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/1f17ff54/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnException.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnException.java b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnException.java
new file mode 100644
index 0000000..080377b
--- /dev/null
+++ b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnException.java
@@ -0,0 +1,60 @@
+/*
+ * 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.flink.testutils.junit;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation to use with {@link org.apache.flink.testutils.junit.RetryRule}.
+ *
+ * <p>Add the {@link org.apache.flink.testutils.junit.RetryRule} to your test and
+ * annotate tests with {@link org.apache.flink.testutils.junit.RetryOnException}.
+ *
+ * <pre>
+ * public class YourTest {
+ *
+ *     {@literal @}Rule
+ *     public RetryRule retryRule = new RetryRule();
+ *
+ *     {@literal @}Test
+ *     {@literal @}RetryOnException(times=1, exception=IOException.class)
+ *     public void yourTest() throws Exception {
+ *         // This will be retried 1 time (total runs 2) before failing the test.
+ *         throw new IOException("Failing test");
+ *     }
+ *     
+ *     {@literal @}Test
+ *     {@literal @}RetryOnException(times=1, exception=IOException.class)
+ *     public void yourTest() throws Exception {
+ *         // This will not be retried, because it throws the wrong exception
+ *         throw new IllegalStateException("Failing test");
+ *     }
+ * }
+ * </pre>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(java.lang.annotation.ElementType.METHOD)
+public @interface RetryOnException {
+
+	int times();
+	
+	Class<? extends Throwable> exception();
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/1f17ff54/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnExceptionTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnExceptionTest.java b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnExceptionTest.java
new file mode 100644
index 0000000..a7a6f4b
--- /dev/null
+++ b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnExceptionTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.flink.testutils.junit;
+
+import org.junit.AfterClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class RetryOnExceptionTest {
+
+	@Rule
+	public RetryRule retryRule = new RetryRule();
+
+	private static final int NUMBER_OF_RUNS = 3;
+	
+	private static int runsForSuccessfulTest = 0;
+
+	private static int runsForTestWithMatchingException = 0;
+
+	private static int runsForTestWithSubclassException = 0;
+	
+	private static int runsForPassAfterOneFailure = 0;
+
+	
+	@AfterClass
+	public static void verify() {
+		assertEquals(NUMBER_OF_RUNS + 1, runsForTestWithMatchingException);
+		assertEquals(NUMBER_OF_RUNS + 1, runsForTestWithSubclassException);
+		assertEquals(1, runsForSuccessfulTest);
+		assertEquals(2, runsForPassAfterOneFailure);
+	}
+
+	@Test
+	@RetryOnException(times = NUMBER_OF_RUNS, exception = IllegalArgumentException.class)
+	public void testSuccessfulTest() {
+		runsForSuccessfulTest++;
+	}
+
+	@Test
+	@RetryOnException(times = NUMBER_OF_RUNS, exception = IllegalArgumentException.class)
+	public void testMatchingException() {
+		runsForTestWithMatchingException++;
+		if (runsForTestWithMatchingException <= NUMBER_OF_RUNS) {
+			throw new IllegalArgumentException();
+		}
+	}
+
+	@Test
+	@RetryOnException(times = NUMBER_OF_RUNS, exception = RuntimeException.class)
+	public void testSubclassException() {
+		runsForTestWithSubclassException++;
+		if (runsForTestWithSubclassException <= NUMBER_OF_RUNS) {
+			throw new IllegalArgumentException();
+		}
+	}
+
+	@Test
+	@RetryOnException(times = NUMBER_OF_RUNS, exception = IllegalArgumentException.class)
+	public void testPassAfterOneFailure() {
+		runsForPassAfterOneFailure++;
+		if (runsForPassAfterOneFailure == 1) {
+			throw new IllegalArgumentException();
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/1f17ff54/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java
index 40e01c1..42b8ef6 100644
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java
+++ b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java
@@ -30,11 +30,11 @@ import java.lang.annotation.Target;
  * <pre>
  * public class YourTest {
  *
- *     {@literal@}Rule
+ *     {@literal @}Rule
  *     public RetryRule retryRule = new RetryRule();
  *
- *     {@literal@}Test
- *     {@literal@}RetryOnFailure(times=1)
+ *     {@literal @}Test
+ *     {@literal @}RetryOnFailure(times=1)
  *     public void yourTest() {
  *         // This will be retried 1 time (total runs 2) before failing the test.
  *         throw new Exception("Failing test");

http://git-wip-us.apache.org/repos/asf/flink/blob/1f17ff54/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java
index 4c4d688..a4aff86 100644
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java
+++ b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java
@@ -26,6 +26,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * A rule to retry failed tests for a fixed number of times.
@@ -35,11 +36,11 @@ import static com.google.common.base.Preconditions.checkArgument;
  * <pre>
  * public class YourTest {
  *
- *     {@literal@}Rule
+ *     {@literal @}Rule
  *     public RetryRule retryRule = new RetryRule();
  *
- *     {@literal@}Test
- *     {@literal@}RetryOnFailure(times=1)
+ *     {@literal @}Test
+ *     {@literal @}RetryOnFailure(times=1)
  *     public void yourTest() {
  *         // This will be retried 1 time (total runs 2) before failing the test.
  *         throw new Exception("Failing test");
@@ -54,18 +55,32 @@ public class RetryRule implements TestRule {
 	@Override
 	public Statement apply(Statement statement, Description description) {
 		RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
+		RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);
 
-		if (retryOnFailure != null) {
+		// sanity check that we don't use expected exceptions with the RetryOnX annotations
+		if (retryOnFailure != null || retryOnException != null) {
 			Test test = description.getAnnotation(Test.class);
 			if (test.expected() != Test.None.class) {
 				throw new IllegalArgumentException("You cannot combine the RetryOnFailure " +
 						"annotation with the Test(expected) annotation.");
 			}
-
-			statement = new RetryOnFailureStatement(retryOnFailure.times(), statement);
 		}
 
-		return statement;
+		// sanity check that we don't use both annotations
+		if (retryOnFailure != null && retryOnException != null) {
+			throw new IllegalArgumentException(
+					"You cannot combine the RetryOnFailure and RetryOnException annotations.");
+		}
+		
+		if (retryOnFailure != null) {
+			return new RetryOnFailureStatement(retryOnFailure.times(), statement);
+		}
+		else if (retryOnException != null) {
+			return new RetryOnExceptionStatement(retryOnException.times(), retryOnException.exception(), statement);
+		}
+		else {
+			return statement;
+		}
 	}
 
 	/**
@@ -110,4 +125,45 @@ public class RetryRule implements TestRule {
 		}
 	}
 
+	/**
+	 * Retries a test in case of a failure.
+	 */
+	private static class RetryOnExceptionStatement extends Statement {
+
+		private final Class<? extends Throwable> exceptionClass;
+		private final int timesOnFailure;
+		private final Statement statement;
+		
+		private int currentRun;
+
+		private RetryOnExceptionStatement(int timesOnFailure, Class<? extends Throwable> exceptionClass, Statement statement) {
+			checkArgument(timesOnFailure >= 0, "Negatives number of retries on failure");
+			this.exceptionClass = checkNotNull(exceptionClass);
+			this.timesOnFailure = timesOnFailure;
+			this.statement = statement;
+		}
+
+		/**
+		 * Retry a test in case of a failure with a specific exception
+		 *
+		 * @throws Throwable
+		 */
+		@Override
+		public void evaluate() throws Throwable {
+			for (currentRun = 0; currentRun <= timesOnFailure; currentRun++) {
+				try {
+					statement.evaluate();
+					break; // success
+				}
+				catch (Throwable t) {
+					LOG.debug(String.format("Test run failed (%d/%d).", currentRun, timesOnFailure + 1), t);
+
+					if (!exceptionClass.isAssignableFrom(t.getClass()) || currentRun >= timesOnFailure) {
+						// Throw the failure if retried too often, or if it is the wrong exception
+						throw t;
+					}
+				}
+			}
+		}
+	}
 }