mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-09 02:36:01 +02:00
InterimittentRule generic junit rule
Change-Id: I375ed04cb6c44f6c923d048523c008b28883700d
This commit is contained in:
parent
7abda712d9
commit
2cf32973cc
2 changed files with 95 additions and 0 deletions
|
@ -0,0 +1,72 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016 Alena Laskavaia and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alena Laskavaia - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.tests.dsf.gdb.framework;
|
||||
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* This is the rule to add to tests that rarely fail randomly and you want to keep them but cannot figure out they fail.
|
||||
* It is safe to use it in any class, it will only apply to tests which have @InterimittentRule annotation
|
||||
* <code>
|
||||
|
||||
public @Rule InterimittentRule rule = new InterimittentRule();
|
||||
|
||||
@Test
|
||||
@InterimittentRule(repetition = 3)
|
||||
public void someTest (){}
|
||||
|
||||
* </code>
|
||||
*/
|
||||
public class InterimittentRule implements MethodRule {
|
||||
public static class RunIntermittent extends Statement {
|
||||
private final FrameworkMethod method;
|
||||
private final Statement statement;
|
||||
|
||||
public RunIntermittent(FrameworkMethod method, Statement statement) {
|
||||
this.method = method;
|
||||
this.statement = statement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
int repetition = 1;
|
||||
Intermittent methodAnnot = method.getAnnotation(Intermittent.class);
|
||||
if (methodAnnot != null) {
|
||||
repetition = methodAnnot.repetition();
|
||||
} else {
|
||||
Intermittent classAnnot = method.getDeclaringClass().getAnnotation(Intermittent.class);
|
||||
if (classAnnot != null) {
|
||||
repetition = classAnnot.repetition();
|
||||
}
|
||||
}
|
||||
if (repetition > 1) {
|
||||
for (int i = 0; i < repetition; i++) {
|
||||
try {
|
||||
statement.evaluate();
|
||||
break; // did not fail yay, we are done
|
||||
} catch (Throwable e) {
|
||||
if (i < repetition - 1)
|
||||
continue; // try again
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} else
|
||||
statement.evaluate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, final FrameworkMethod method, final Object target) {
|
||||
return new RunIntermittent(method, base);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016 Alena Laskavaia and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alena Laskavaia - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.tests.dsf.gdb.framework;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Target({ METHOD, TYPE })
|
||||
@Retention(RUNTIME)
|
||||
public @interface Intermittent {
|
||||
int repetition() default 5;
|
||||
}
|
Loading…
Add table
Reference in a new issue