The "@Retry" annotation allows to select the number of retries of a
Junit test method (provided that the test class is itself annotated
with "@RunWith(RetryRunner.class).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1086539 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-03-29 10:28:37 +00:00
parent 7f1d7b7e33
commit dfea5addf8
3 changed files with 113 additions and 25 deletions

View File

@ -0,0 +1,30 @@
/*
* 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.commons.math;
import java.lang.annotation.*;
/**
* Annotation that enables test retries.
* @version $Id$
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Retry {
int value() default 2;
}

View File

@ -28,13 +28,11 @@ import org.junit.runners.model.Statement;
* @version $Revision$ $Date$
*/
public class RetryRunner extends BlockJUnit4ClassRunner {
/** Maximal number of test run attempts. */
private static final int MAX_ATTEMPTS = 3;
/** Simple constructor.
* @param testClass class to test
* @throws InitializationError if default runner cannot be built
/**
* Simple constructor.
*
* @param testClass Class to test.
* @throws InitializationError if default runner cannot be built.
*/
public RetryRunner(final Class<?> testClass)
throws InitializationError {
@ -42,37 +40,42 @@ public class RetryRunner extends BlockJUnit4ClassRunner {
}
@Override
public Statement methodInvoker(FrameworkMethod method, Object test) {
public Statement methodInvoker(final FrameworkMethod method,
Object test) {
final Statement singleTryStatement = super.methodInvoker(method, test);
return new Statement() {
/** Evaluate the 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();
final Retry retry = method.getAnnotation(Retry.class);
if (retry == null) {
// Do a single test run attempt.
singleTryStatement.evaluate();
} else {
final int numRetries = retry.value();
// attempt succeeded, stop evaluation here
return;
} catch (Throwable t) {
// attempt failed, store the reason why
failureReason = t;
for (int i = 0; i < numRetries; ++i) {
try {
// Do a single test run attempt.
singleTryStatement.evaluate();
// Attempt succeeded, stop evaluation here.
return;
} catch (Throwable t) {
// Attempt failed, store the reason.
failureReason = t;
}
}
// All attempts failed.
throw failureReason;
}
// all attempts failed
throw failureReason;
}
};
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.commons.math;
import java.util.Random;
import org.apache.commons.math.exception.MathRuntimeException;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test for the "Retry" functionality (retrying Junit test methods).
*/
@RunWith(RetryRunner.class)
public class RetryRunnerTest {
final Random rng = new Random();
/**
* Shows that an always failing test will fail even if it is retried.
*/
@Test(expected=MathRuntimeException.class)
@Retry
public void testRetryFailAlways() {
throw new MathRuntimeException();
}
/**
* Shows that a test that sometimes fail might succeed if it is retried.
* In this case the high number of retries makes it quite unlikely that
* the exception will be thrown by all of the calls.
*/
@Test
@Retry(100)
public void testRetryFailSometimes() {
if (rng.nextBoolean()) {
throw new MathRuntimeException();
}
}
}