Permalink
Browse files

JUNITBENCH-57: The runner does not honour skipping tests with Assume.

  • Loading branch information...
1 parent 148a530 commit 7df97d4fcbff68eea1b2771c6fafd3933cca9384 @dweiss dweiss committed Aug 7, 2013
View
@@ -154,6 +154,13 @@
</exclusion>
</exclusions>
</dependency>
+
+ <dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert-core</artifactId>
+ <version>2.0M10</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
@@ -87,7 +87,7 @@ protected BaseEvaluator(int warmupRounds, int benchmarkRounds, int totalRounds,
protected abstract Result evaluate() throws Throwable;
- protected final SingleResult evaluateInternally(int round) throws InvocationTargetException
+ protected final SingleResult evaluateInternally(int round) throws Throwable
{
// We assume no reordering will take place here.
final long startTime = clock.time();
@@ -101,27 +101,21 @@ protected final SingleResult evaluateInternally(int round) throws InvocationTarg
warmupTime = benchmarkTime - warmupTime;
}
- try
- {
- base.evaluate();
- final long endTime = clock.time();
-
- final long roundBlockedTime;
- if (supportsThreadContention) {
- final long threadId = Thread.currentThread().getId();
- final ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
- final long threadBlockedTime = threadInfo.getBlockedTime();
- final long previousValue = previousThreadBlockedTime.get().getAndSet(threadBlockedTime);
- roundBlockedTime = threadBlockedTime - previousValue;
- } else {
- roundBlockedTime = 0;
- }
-
- return new SingleResult(startTime, afterGC, endTime, roundBlockedTime);
- } catch (Throwable t)
- {
- throw new InvocationTargetException(t);
+ base.evaluate();
+ final long endTime = clock.time();
+
+ final long roundBlockedTime;
+ if (supportsThreadContention) {
+ final long threadId = Thread.currentThread().getId();
+ final ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
+ final long threadBlockedTime = threadInfo.getBlockedTime();
+ final long previousValue = previousThreadBlockedTime.get().getAndSet(threadBlockedTime);
+ roundBlockedTime = threadBlockedTime - previousValue;
+ } else {
+ roundBlockedTime = 0;
}
+
+ return new SingleResult(startTime, afterGC, endTime, roundBlockedTime);
}
protected Result computeResult()
@@ -183,7 +177,13 @@ public EvaluatorCallable(int i)
public SingleResult call() throws Exception
{
latch.await();
- return evaluateInternally(i);
+ try {
+ return evaluateInternally(i);
+ } catch (Exception e) {
+ throw e;
+ } catch (Throwable t) {
+ throw new InvocationTargetException(t);
+ }
}
}
@@ -0,0 +1,117 @@
+package com.carrotsearch.junitbenchmarks;
+
+import static org.fest.assertions.api.Assertions.assertThat;
+
+import java.util.ArrayList;
+
+import org.junit.After;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestRule;
+import org.junit.runner.JUnitCore;
+
+/**
+ * Test assumptions don't cause a failure.
+ */
+public class TestAssumptions
+{
+ private static final ArrayList<Result> results = new ArrayList<Result>();
+ private static final IResultsConsumer resultsConsumer = new IResultsConsumer()
+ {
+ public void accept(Result result)
+ {
+ results.add(result);
+ }
+ };
+
+ static volatile boolean runsAsNested;
+
+ public static class AssumeOnBeforeClass
+ {
+ @Rule
+ public TestRule benchmarkRun = new BenchmarkRule(resultsConsumer);
+
+ @BeforeClass
+ public static void beforeClass()
+ {
+ if (runsAsNested) Assume.assumeTrue(false);
+ }
+
+ @Test
+ public void testDummy() {}
+ }
+
+ public static class AssumeOnBeforeInstance
+ {
+ @Rule
+ public TestRule benchmarkRun = new BenchmarkRule(resultsConsumer);
+
+ @Before
+ public void beforeInstance()
+ {
+ if (runsAsNested) Assume.assumeTrue(false);
+ }
+
+ @Test
+ public void testDummy() {}
+ }
+
+ public static class AssumeOnTest
+ {
+ @Rule
+ public TestRule benchmarkRun = new BenchmarkRule(resultsConsumer);
+
+ @Test
+ public void testDummy()
+ {
+ if (runsAsNested) Assume.assumeTrue(false);
+ }
+ }
+
+ @Before
+ public void enableNested()
+ {
+ runsAsNested = true;
+ results.clear();
+ }
+
+ @After
+ public void disableNested()
+ {
+ runsAsNested = false;
+ }
+
+ @Test
+ public void testBeforeClass()
+ {
+ org.junit.runner.Result result = JUnitCore.runClasses(AssumeOnBeforeClass.class);
+ assertThat(result.getFailures()).isEmpty();
+ assertThat(result.getIgnoreCount()).isEqualTo(1);
+ assertThat(result.getRunCount()).isEqualTo(0);
+ assertThat(result.getFailureCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void testBeforeInstance()
+ {
+ org.junit.runner.Result result = JUnitCore.runClasses(AssumeOnBeforeInstance.class);
+ assertThat(result.getFailures()).isEmpty();
+ assertThat(result.getIgnoreCount()).isEqualTo(0);
+ assertThat(result.getRunCount()).isEqualTo(1);
+ assertThat(result.getFailureCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void testOnTest()
+ {
+ org.junit.runner.Result result = JUnitCore.runClasses(AssumeOnTest.class);
+ assertThat(result.getFailures()).isEmpty();
+ assertThat(result.getIgnoreCount()).isEqualTo(0);
+ assertThat(result.getRunCount()).isEqualTo(1);
+ assertThat(result.getFailureCount()).isEqualTo(0);
+ }
+
+}

0 comments on commit 7df97d4

Please sign in to comment.