Ghidra is a software reverse engineering (SRE) framework
GP-2306: Add @Repeated annotation
Danbc4742c1c07e
diff --git a/Ghidra/Framework/Generic/src/main/java/generic/test/AbstractGenericTest.java b/Ghidra/Framework/Generic/src/main/java/generic/test/AbstractGenericTest.javaindex 09fd222752..b21cd3e443 100644--- a/Ghidra/Framework/Generic/src/main/java/generic/test/AbstractGenericTest.java+++ b/Ghidra/Framework/Generic/src/main/java/generic/test/AbstractGenericTest.java@@ -15,7 +15,7 @@*/package generic.test;-import static org.junit.Assert.*;+import static org.junit.Assert.assertNotNull;import java.awt.*;import java.awt.event.*;@@ -43,6 +43,7 @@ import org.junit.rules.*;import org.junit.runner.Description;import generic.jar.ResourceFile;+import generic.test.rule.RepeatedTestRule;import generic.util.WindowUtilities;import ghidra.GhidraTestApplicationLayout;import ghidra.framework.Application;@@ -113,6 +114,17 @@ public abstract class AbstractGenericTest extends AbstractGTest {@Rulepublic RuleChain ruleChain = RuleChain.outerRule(testName).around(watchman);// control rule ordering+ /**+ * This rule handles the {@link Repeated} annotation+ *+ * <p>+ * During batch mode, this rule should never be needed. This rule is included here as a+ * convenience, in case a developer wants to use the {@link Repeated} annotation to diagnose a+ * non-deterministic test failure. Without this rule, the annotation would be silently ignored.+ */+ @Rule+ public TestRule repeatedRule = new RepeatedTestRule();+private void debugBatch(String message) {if (BATCH_MODE) {Msg.debug(AbstractGenericTest.class, message);diff --git a/Ghidra/Framework/Generic/src/main/java/generic/test/rule/Repeated.java b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/Repeated.javanew file mode 100644index 0000000000..682a7a16bb--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/Repeated.java@@ -0,0 +1,44 @@+/* ###+ * IP: Apache License 2.0+ */+package generic.test.rule;++import java.lang.annotation.*;++/**+ * Repeat the annotated test method some number of times+ *+ * <p>+ * As a matter of practice, no test should ever be committed into source control with this+ * annotation. It is only a tool for diagnosing non-deterministic test failures on the developer's+ * workstation. For example, suppose {@code testSomeMethod} fails every other Tuesday on the CI+ * system, but never seems to fail on the developer's workstation. It might help to repeat a test+ * 100 times, including its set-up and tear-down, in a single test run. To do this, the developer+ * can temporarily add the {@code @Repeated(100)} annotation, and then run the test from their IDE.+ *+ * <pre>+ * @Test+ * @Repeated(100)+ * public void testSomeMethod() {+ * SomeClass obj = new SomeClass();+ * obj.someMethod();+ * assertEquals(3, obj.getSomeState());+ * }+ * </pre>+ *+ * <p>+ * The number of repetitions can be adjusted depending on the desired level of assurance. If the+ * failure is truly due to timing, and not some other condition unique to the CI system, then it+ * will likely fail within 100 repetitions. Once the code is fixed, and the test passes for the+ * desired number of repetitions, the annotation should be removed before the changes are committed.+ */+@Retention(RetentionPolicy.RUNTIME)+@Target(ElementType.METHOD)+public @interface Repeated {+ /**+ * The number of times to repeat the test, must be positive+ *+ * @return the count+ */+ int value();+}diff --git a/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedStatement.java b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedStatement.javanew file mode 100644index 0000000000..62a833a6f9--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedStatement.java@@ -0,0 +1,51 @@+/* ###+ * IP: Apache License 2.0+ */+package generic.test.rule;++import java.util.Date;++import org.junit.runner.Description;+import org.junit.runners.model.Statement;++import ghidra.util.Msg;++/**+ * A JUnit test statement that repeats its base statement 1 or more times+ *+ * @see Repeated+ */+public class RepeatedStatement extends Statement {+ private final Statement base;+ private final Description description;+ private final int count;++ /**+ * Construct the statement+ *+ * @param base the base statement to repeat+ * @param description the description of the test+ * @param count the number of repetitions, must be positive+ */+ public RepeatedStatement(Statement base, Description description, int count) {+ if (count <= 0) {+ throw new IllegalArgumentException(+ "@Repeated count must be positive. To ignore a test. Use @Ignore");+ }+ this.base = base;+ this.description = description;+ this.count = count;+ }++ @Override+ public void evaluate() throws Throwable {+ for (int i = 0; i < count; i++) {+ if (count > 1) {+ Msg.debug(this,+ (new Date()) + "\n *** REPETITION " + (i + 1) + "/" + count + " of " ++ description.getDisplayName() + " ***");+ }+ base.evaluate();+ }+ }+}diff --git a/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedTestRule.java b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedTestRule.javanew file mode 100644index 0000000000..4bf87cd02d--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/java/generic/test/rule/RepeatedTestRule.java@@ -0,0 +1,30 @@+/* ###+ * IP: Apache License 2.0+ */+package generic.test.rule;++import org.junit.Rule;+import org.junit.rules.TestRule;+import org.junit.runner.Description;+import org.junit.runners.model.Statement;++import generic.test.AbstractGenericTest;++/**+ * A test rule which processes the {@link Repeated} annotation+ *+ * <p>+ * This must be included in your test case (or a superclass) as a field with the {@link Rule}+ * annotation. It's included in {@link AbstractGenericTest}, so most Ghidra test classes already+ * have it.+ */+public class RepeatedTestRule implements TestRule {+ @Override+ public Statement apply(Statement base, Description description) {+ Repeated annot = description.getAnnotation(Repeated.class);+ if (annot == null) {+ return base;+ }+ return new RepeatedStatement(base, description, annot.value());+ }+}