Author: luc
Date: Sun Mar 27 15:02:47 2011
New Revision: 1085954
URL: http://svn.apache.org/viewvc?rev=1085954&view=rev
Log:
Implemented a retry policy for tests that may randomly fail.
Tests classes that may fail should be annotated with
@RunWith(RetryRunner.class). This custom test runner attempts to re-run tests
that fail, up to a maximum number of attempts defined as a constant in the test
runner (currently set to 3 attempts max).
Jira: MATH-423
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryRunner.java
- copied, changed from r1085929,
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryTestCase.java
Removed:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryTestCase.java
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java
Copied:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryRunner.java
(from r1085929,
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryTestCase.java)
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryRunner.java?p2=commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryRunner.java&p1=commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryTestCase.java&r1=1085929&r2=1085954&rev=1085954&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryTestCase.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/RetryRunner.java
Sun Mar 27 15:02:47 2011
@@ -17,26 +17,62 @@
package org.apache.commons.math;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+
/**
- * A Test case that retries tests when assertions fail.
+ * A test runner that retries tests when assertions fail.
* @version $Revision$ $Date$
*/
-public abstract class RetryTestCase {
+public class RetryRunner extends BlockJUnit4ClassRunner {
- // TODO implement retry policy using Junit 4 API
+ /** Maximal number of test run attempts. */
+ private static final int MAX_ATTEMPTS = 3;
- // /**
-// * Override runTest() to catch AssertionFailedError and retry
-// */
-// @Override
-// protected void runTest() throws Throwable {
-// try {
-// super.runTest();
-// } catch (AssertionFailedError err) {
-// // System.out.println("Retrying " + this.getName());
-// super.runTest();
-// }
-// }
+ /** Simple constructor.
+ * @param testClass class to test
+ * @throws InitializationError if default runner cannot be built
+ */
+ public RetryRunner(final Class<?> testClass)
+ throws InitializationError {
+ super(testClass);
+ }
+
+ @Override
+ public Statement methodInvoker(FrameworkMethod method, Object test) {
+ final Statement singleTryStatement = super.methodInvoker(method, test);
+ return new Statement() {
+
+ /** Evaluate the statement.
+ * We attempt several runs for the test, at most MAX_ATTEMPTS.
+ * if one attempt succeeds, we succeed, if all attempts fail, we
+ * fail with the reason corresponding to the last attempt
+ */
+ public void evaluate() throws Throwable {
+ Throwable failureReason = null;
+ for (int i = 0; i < MAX_ATTEMPTS; ++i) {
+ try {
+
+ // do one test run attempt
+ singleTryStatement.evaluate();
+
+ // attempt succeeded, stop evaluation here
+ return;
+
+ } catch (Throwable t) {
+ // attempt failed, store the reason why
+ failureReason = t;
+ }
+ }
+
+ // all attempts failed
+ throw failureReason;
+
+ }
+ };
+ }
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java?rev=1085954&r1=1085953&r2=1085954&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
Sun Mar 27 15:02:47 2011
@@ -23,12 +23,13 @@ import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
-import org.apache.commons.math.RetryTestCase;
+import org.apache.commons.math.RetryRunner;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
/**
* Test cases for the EmpiricalDistribution class
@@ -36,7 +37,8 @@ import org.junit.Test;
* @version $Revision$ $Date$
*/
-public final class EmpiricalDistributionTest extends RetryTestCase {
+@RunWith(RetryRunner.class)
+public final class EmpiricalDistributionTest {
protected EmpiricalDistribution empiricalDistribution = null;
protected EmpiricalDistribution empiricalDistribution2 = null;
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1085954&r1=1085953&r2=1085954&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
Sun Mar 27 15:02:47 2011
@@ -22,7 +22,7 @@ import java.util.HashSet;
import java.util.List;
-import org.apache.commons.math.RetryTestCase;
+import org.apache.commons.math.RetryRunner;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.distribution.BetaDistributionImpl;
import org.apache.commons.math.distribution.BinomialDistributionImpl;
@@ -49,6 +49,7 @@ import org.apache.commons.math.util.Fast
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.junit.Assert;
import org.junit.Test;
+import org.junit.runner.RunWith;
/**
* Test cases for the RandomData class.
@@ -57,7 +58,8 @@ import org.junit.Test;
* 2009) $
*/
-public class RandomDataTest extends RetryTestCase {
+@RunWith(RetryRunner.class)
+public class RandomDataTest {
public RandomDataTest() {
randomData = new RandomDataImpl();
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java?rev=1085954&r1=1085953&r2=1085954&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/ValueServerTest.java
Sun Mar 27 15:02:47 2011
@@ -19,11 +19,12 @@ package org.apache.commons.math.random;
import java.io.EOFException;
import java.net.URL;
-import org.apache.commons.math.RetryTestCase;
+import org.apache.commons.math.RetryRunner;
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
/**
* Test cases for the ValueServer class.
@@ -31,7 +32,8 @@ import org.junit.Test;
* @version $Revision$ $Date$
*/
-public final class ValueServerTest extends RetryTestCase {
+@RunWith(RetryRunner.class)
+public final class ValueServerTest {
private ValueServer vs = new ValueServer();