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 2016/12/14 12:10:18 UTC
[11/12] flink git commit: [tests] Remove redundant copies of the
JUnit RetryRules
[tests] Remove redundant copies of the JUnit RetryRules
The classes were noved to 'flink-test-utils-junit', but apparently copies remained in 'flink-core'.
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/a4ff4804
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/a4ff4804
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/a4ff4804
Branch: refs/heads/master
Commit: a4ff4804a7029e5593b03c32074a386bc9d6102b
Parents: c906ad9
Author: Stephan Ewen <se...@apache.org>
Authored: Tue Dec 13 21:42:08 2016 +0100
Committer: Stephan Ewen <se...@apache.org>
Committed: Wed Dec 14 12:43:33 2016 +0100
----------------------------------------------------------------------
.../flink/testutils/junit/RetryOnException.java | 60 -------
.../testutils/junit/RetryOnExceptionTest.java | 83 ---------
.../flink/testutils/junit/RetryOnFailure.java | 49 ------
.../testutils/junit/RetryOnFailureTest.java | 78 ---------
.../apache/flink/testutils/junit/RetryRule.java | 170 -------------------
5 files changed, 440 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/a4ff4804/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
deleted file mode 100644
index 080377b..0000000
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnException.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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/a4ff4804/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
deleted file mode 100644
index a7a6f4b..0000000
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnExceptionTest.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * 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/a4ff4804/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
deleted file mode 100644
index 42b8ef6..0000000
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailure.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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 RetryRule}.
- *
- * <p>Add the {@link RetryRule} to your test and annotate tests with {@link RetryOnFailure}.
- *
- * <pre>
- * public class YourTest {
- *
- * {@literal @}Rule
- * public RetryRule retryRule = new RetryRule();
- *
- * {@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");
- * }
- * }
- * </pre>
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target({ java.lang.annotation.ElementType.METHOD })
-public @interface RetryOnFailure {
- int times();
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/a4ff4804/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailureTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailureTest.java b/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailureTest.java
deleted file mode 100644
index efc7ba4..0000000
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryOnFailureTest.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * 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 RetryOnFailureTest {
-
- @Rule
- public RetryRule retryRule = new RetryRule();
-
- private static final int NUMBER_OF_RUNS = 5;
-
- private static int numberOfFailedRuns;
-
- private static int numberOfSuccessfulRuns;
-
- private static boolean firstRun = true;
-
- @AfterClass
- public static void verify() throws Exception {
- assertEquals(NUMBER_OF_RUNS + 1, numberOfFailedRuns);
- assertEquals(3, numberOfSuccessfulRuns);
- }
-
- @Test
- @RetryOnFailure(times = NUMBER_OF_RUNS)
- public void testRetryOnFailure() throws Exception {
- // All but the (expected) last run should be successful
- if (numberOfFailedRuns < NUMBER_OF_RUNS) {
- numberOfFailedRuns++;
- throw new RuntimeException("Expected test exception");
- }
- else {
- numberOfSuccessfulRuns++;
- }
- }
-
- @Test
- @RetryOnFailure(times = NUMBER_OF_RUNS)
- public void testRetryOnceOnFailure() throws Exception {
- if (firstRun) {
- numberOfFailedRuns++;
- firstRun = false;
- throw new RuntimeException("Expected test exception");
- }
- else {
- numberOfSuccessfulRuns++;
- }
- }
-
- @Test
- @RetryOnFailure(times = NUMBER_OF_RUNS)
- public void testDontRetryOnSuccess() throws Exception {
- numberOfSuccessfulRuns++;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/flink/blob/a4ff4804/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
deleted file mode 100644
index 782f4fb..0000000
--- a/flink-core/src/test/java/org/apache/flink/testutils/junit/RetryRule.java
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * 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.Test;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import static org.apache.flink.util.Preconditions.checkArgument;
-import static org.apache.flink.util.Preconditions.checkNotNull;
-
-/**
- * A rule to retry failed tests for a fixed number of times.
- *
- * <p>Add the {@link RetryRule} to your test and annotate tests with {@link RetryOnFailure}.
- *
- * <pre>
- * public class YourTest {
- *
- * {@literal @}Rule
- * public RetryRule retryRule = new RetryRule();
- *
- * {@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");
- * }
- * }
- * </pre>
- */
-public class RetryRule implements TestRule {
-
- public final static Logger LOG = LoggerFactory.getLogger(RetryRule.class);
-
- @Override
- public Statement apply(Statement statement, Description description) {
- RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
- RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);
-
- // 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.");
- }
- }
-
- // 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;
- }
- }
-
- /**
- * Retries a test in case of a failure.
- */
- private static class RetryOnFailureStatement extends Statement {
-
- private final int timesOnFailure;
-
- private int currentRun;
-
- private final Statement statement;
-
- private RetryOnFailureStatement(int timesOnFailure, Statement statement) {
- checkArgument(timesOnFailure >= 0, "Negatives number of retries on failure");
- this.timesOnFailure = timesOnFailure;
- this.statement = statement;
- }
-
- /**
- * Retry a test in case of a failure.
- *
- * @throws Throwable
- */
- @Override
- public void evaluate() throws Throwable {
- for (currentRun = 0; currentRun <= timesOnFailure; currentRun++) {
- try {
- statement.evaluate();
- break; // success
- }
- catch (Throwable t) {
- LOG.warn(String.format("Test run failed (%d/%d).",
- currentRun, timesOnFailure + 1), t);
-
- // Throw the failure if retried too often
- if (currentRun == timesOnFailure) {
- throw t;
- }
- }
- }
- }
- }
-
- /**
- * 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.warn(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;
- }
- }
- }
- }
- }
-}