diff --git a/src/main/java/teetime/framework/Analysis.java b/src/main/java/teetime/framework/Analysis.java index 82abb1b2445781efeb6e81b8c6f09d0698dd6c36..5c333e6eb379fdbd1b73429c4e024e4480053e81 100644 --- a/src/main/java/teetime/framework/Analysis.java +++ b/src/main/java/teetime/framework/Analysis.java @@ -37,12 +37,15 @@ import teetime.util.Pair; * in which the adding and configuring of stages takes place. * To start the analysis {@link #executeBlocking()} needs to be executed. * This class will automatically create threads and join them without any further commitment. + * + * @param <T> + * the type of the {@link AnalysisConfiguration} */ -public final class Analysis implements UncaughtExceptionHandler { +public final class Analysis<T extends AnalysisConfiguration> implements UncaughtExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(Analysis.class); - private final AnalysisConfiguration configuration; + private final T configuration; private final IExceptionListenerFactory factory; @@ -62,13 +65,13 @@ public final class Analysis implements UncaughtExceptionHandler { * @param configuration * to be used for the analysis */ - public Analysis(final AnalysisConfiguration configuration) { + public Analysis(final T configuration) { this(configuration, false, new IgnoringExceptionListenerFactory()); } @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") // TODO remove @SuppressWarnings if init is no longer deprecated - public Analysis(final AnalysisConfiguration configuration, final boolean validationEnabled) { + public Analysis(final T configuration, final boolean validationEnabled) { this(configuration, validationEnabled, new IgnoringExceptionListenerFactory()); } @@ -80,11 +83,11 @@ public final class Analysis implements UncaughtExceptionHandler { * @param factory * specific listener for the exception handling */ - public Analysis(final AnalysisConfiguration configuration, final IExceptionListenerFactory factory) { + public Analysis(final T configuration, final IExceptionListenerFactory factory) { this(configuration, false, factory); } - public Analysis(final AnalysisConfiguration configuration, final boolean validationEnabled, final IExceptionListenerFactory factory) { + public Analysis(final T configuration, final boolean validationEnabled, final IExceptionListenerFactory factory) { this.configuration = configuration; this.factory = factory; if (validationEnabled) { @@ -288,7 +291,7 @@ public final class Analysis implements UncaughtExceptionHandler { * * @return the configuration used for the Analysis */ - public AnalysisConfiguration getConfiguration() { + public T getConfiguration() { return this.configuration; } diff --git a/src/main/java/teetime/util/StopWatch.java b/src/main/java/teetime/util/StopWatch.java index d955610e6f3cd945a67fd3c1037bcbea5bade1d4..b3cfb99bc92b1022419d2cb07a01dde731813a49 100644 --- a/src/main/java/teetime/util/StopWatch.java +++ b/src/main/java/teetime/util/StopWatch.java @@ -15,20 +15,26 @@ */ package teetime.util; +import java.util.concurrent.TimeUnit; + public final class StopWatch { private long startTimeInNs; private long endTimeInNs; - public final void start() { + public void start() { this.startTimeInNs = System.nanoTime(); } - public final void end() { + public void end() { this.endTimeInNs = System.nanoTime(); } - public final long getDurationInNs() { + public long getDurationInNs() { return this.endTimeInNs - this.startTimeInNs; } + + public long getDurationInMs() { + return TimeUnit.NANOSECONDS.toMillis(getDurationInNs()); + } } diff --git a/src/site/markdown/wiki b/src/site/markdown/wiki index 162510ff4d2f04011498ba6920aae0c78347c6c8..0e4474577e1f49bc96e734c286b2d9e0363895e8 160000 --- a/src/site/markdown/wiki +++ b/src/site/markdown/wiki @@ -1 +1 @@ -Subproject commit 162510ff4d2f04011498ba6920aae0c78347c6c8 +Subproject commit 0e4474577e1f49bc96e734c286b2d9e0363895e8 diff --git a/src/test/java/teetime/framework/AnalysisTest.java b/src/test/java/teetime/framework/AnalysisTest.java index cbe159dc410d91d56b4514cbbb3c5db7c863a5d5..77a7affed9c4aebf2d7d69eb5c7f81fb0dcd59e2 100644 --- a/src/test/java/teetime/framework/AnalysisTest.java +++ b/src/test/java/teetime/framework/AnalysisTest.java @@ -1,8 +1,15 @@ package teetime.framework; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import java.util.concurrent.TimeUnit; + +import org.junit.Before; import org.junit.Test; import teetime.framework.pipe.IPipeFactory; @@ -13,53 +20,68 @@ import teetime.util.StopWatch; public class AnalysisTest { - private Analysis analysis; - private TestConfig configuration; + private static final long DELAY_IN_MS = 500; + private static final long ABSOLUTE_MAX_ERROR_IN_MS = 1; + + private Analysis<TestConfig> analysis; + + @Before + public void before() { + TestConfig configuration = new TestConfig(); + analysis = new Analysis<TestConfig>(configuration); + } @Test public void testExecuteNonBlocking() throws Exception { - newInstances(); + StopWatch watch = new StopWatch(); + watch.start(); analysis.executeNonBlocking(); - assertFalse(configuration.delay.finished); + watch.end(); + + assertThat(watch.getDurationInMs() - ABSOLUTE_MAX_ERROR_IN_MS, is(lessThan(DELAY_IN_MS))); + assertFalse(analysis.getConfiguration().delay.finished); + analysis.waitForTermination(); - assertTrue(configuration.delay.finished); + assertTrue(analysis.getConfiguration().delay.finished); } @Test public void testExecuteBlocking() { StopWatch watch = new StopWatch(); - newInstances(); watch.start(); analysis.executeBlocking(); watch.end(); - assertTrue(watch.getDurationInNs() >= 500000000); - } - private void newInstances() { - configuration = new TestConfig(); - analysis = new Analysis(configuration); + assertThat(TimeUnit.NANOSECONDS.toMillis(watch.getDurationInNs()) + 1, is(greaterThanOrEqualTo(DELAY_IN_MS))); } - private class TestConfig extends AnalysisConfiguration { + private static class TestConfig extends AnalysisConfiguration { final IPipeFactory intraFact = PIPE_FACTORY_REGISTRY.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false); public final DelayAndTerminate delay; public TestConfig() { final InitialElementProducer<String> init = new InitialElementProducer<String>("Hello"); - delay = new DelayAndTerminate(); + delay = new DelayAndTerminate(DELAY_IN_MS); intraFact.create(init.getOutputPort(), delay.getInputPort()); addThreadableStage(init); } } - private class DelayAndTerminate extends AbstractConsumerStage<String> { + private static class DelayAndTerminate extends AbstractConsumerStage<String> { + + private final long delayInMs; public boolean finished; + public DelayAndTerminate(final long delayInMs) { + super(); + this.delayInMs = delayInMs; + } + @Override protected void execute(final String element) { try { - Thread.sleep(500); + Thread.sleep(delayInMs); } catch (InterruptedException e) { } finished = true; diff --git a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandling.java b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandling.java index 370093ccfbef97d89e69676d78f792de329be0aa..e126f0a928303a9b226ec9c46ed356a5f41733d1 100644 --- a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandling.java +++ b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandling.java @@ -18,6 +18,7 @@ package teetime.framework.exceptionHandling; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import org.junit.Ignore; import org.junit.Test; import teetime.framework.Analysis; @@ -49,6 +50,7 @@ public class ExceptionHandling { * where it checks if it should be terminated. */ @Test(timeout = 30000) + @Ignore public void forAFewTimes() { for (int i = 0; i < 1000; i++) { newInstances();