Skip to content
Snippets Groups Projects
Commit e3adb495 authored by Christian Wulf's avatar Christian Wulf
Browse files

added type parameter for Analysis;

refactored AnalysisTest
parent 79bd0037
No related branches found
No related tags found
No related merge requests found
...@@ -37,12 +37,15 @@ import teetime.util.Pair; ...@@ -37,12 +37,15 @@ import teetime.util.Pair;
* in which the adding and configuring of stages takes place. * in which the adding and configuring of stages takes place.
* To start the analysis {@link #executeBlocking()} needs to be executed. * To start the analysis {@link #executeBlocking()} needs to be executed.
* This class will automatically create threads and join them without any further commitment. * 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 static final Logger LOGGER = LoggerFactory.getLogger(Analysis.class);
private final AnalysisConfiguration configuration; private final T configuration;
private final IExceptionListenerFactory factory; private final IExceptionListenerFactory factory;
...@@ -62,13 +65,13 @@ public final class Analysis implements UncaughtExceptionHandler { ...@@ -62,13 +65,13 @@ public final class Analysis implements UncaughtExceptionHandler {
* @param configuration * @param configuration
* to be used for the analysis * to be used for the analysis
*/ */
public Analysis(final AnalysisConfiguration configuration) { public Analysis(final T configuration) {
this(configuration, false, new IgnoringExceptionListenerFactory()); this(configuration, false, new IgnoringExceptionListenerFactory());
} }
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod") @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
// TODO remove @SuppressWarnings if init is no longer deprecated // 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()); this(configuration, validationEnabled, new IgnoringExceptionListenerFactory());
} }
...@@ -80,11 +83,11 @@ public final class Analysis implements UncaughtExceptionHandler { ...@@ -80,11 +83,11 @@ public final class Analysis implements UncaughtExceptionHandler {
* @param factory * @param factory
* specific listener for the exception handling * 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); 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.configuration = configuration;
this.factory = factory; this.factory = factory;
if (validationEnabled) { if (validationEnabled) {
...@@ -288,7 +291,7 @@ public final class Analysis implements UncaughtExceptionHandler { ...@@ -288,7 +291,7 @@ public final class Analysis implements UncaughtExceptionHandler {
* *
* @return the configuration used for the Analysis * @return the configuration used for the Analysis
*/ */
public AnalysisConfiguration getConfiguration() { public T getConfiguration() {
return this.configuration; return this.configuration;
} }
......
...@@ -15,20 +15,26 @@ ...@@ -15,20 +15,26 @@
*/ */
package teetime.util; package teetime.util;
import java.util.concurrent.TimeUnit;
public final class StopWatch { public final class StopWatch {
private long startTimeInNs; private long startTimeInNs;
private long endTimeInNs; private long endTimeInNs;
public final void start() { public void start() {
this.startTimeInNs = System.nanoTime(); this.startTimeInNs = System.nanoTime();
} }
public final void end() { public void end() {
this.endTimeInNs = System.nanoTime(); this.endTimeInNs = System.nanoTime();
} }
public final long getDurationInNs() { public long getDurationInNs() {
return this.endTimeInNs - this.startTimeInNs; return this.endTimeInNs - this.startTimeInNs;
} }
public long getDurationInMs() {
return TimeUnit.NANOSECONDS.toMillis(getDurationInNs());
}
} }
wiki @ 0e447457
Subproject commit 162510ff4d2f04011498ba6920aae0c78347c6c8 Subproject commit 0e4474577e1f49bc96e734c286b2d9e0363895e8
package teetime.framework; 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.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import teetime.framework.pipe.IPipeFactory; import teetime.framework.pipe.IPipeFactory;
...@@ -13,53 +20,68 @@ import teetime.util.StopWatch; ...@@ -13,53 +20,68 @@ import teetime.util.StopWatch;
public class AnalysisTest { public class AnalysisTest {
private Analysis analysis; private static final long DELAY_IN_MS = 500;
private TestConfig configuration; 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 @Test
public void testExecuteNonBlocking() throws Exception { public void testExecuteNonBlocking() throws Exception {
newInstances(); StopWatch watch = new StopWatch();
watch.start();
analysis.executeNonBlocking(); 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(); analysis.waitForTermination();
assertTrue(configuration.delay.finished); assertTrue(analysis.getConfiguration().delay.finished);
} }
@Test @Test
public void testExecuteBlocking() { public void testExecuteBlocking() {
StopWatch watch = new StopWatch(); StopWatch watch = new StopWatch();
newInstances();
watch.start(); watch.start();
analysis.executeBlocking(); analysis.executeBlocking();
watch.end(); watch.end();
assertTrue(watch.getDurationInNs() >= 500000000);
}
private void newInstances() { assertThat(TimeUnit.NANOSECONDS.toMillis(watch.getDurationInNs()) + 1, is(greaterThanOrEqualTo(DELAY_IN_MS)));
configuration = new TestConfig();
analysis = new Analysis(configuration);
} }
private class TestConfig extends AnalysisConfiguration { private static class TestConfig extends AnalysisConfiguration {
final IPipeFactory intraFact = PIPE_FACTORY_REGISTRY.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false); final IPipeFactory intraFact = PIPE_FACTORY_REGISTRY.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false);
public final DelayAndTerminate delay; public final DelayAndTerminate delay;
public TestConfig() { public TestConfig() {
final InitialElementProducer<String> init = new InitialElementProducer<String>("Hello"); final InitialElementProducer<String> init = new InitialElementProducer<String>("Hello");
delay = new DelayAndTerminate(); delay = new DelayAndTerminate(DELAY_IN_MS);
intraFact.create(init.getOutputPort(), delay.getInputPort()); intraFact.create(init.getOutputPort(), delay.getInputPort());
addThreadableStage(init); addThreadableStage(init);
} }
} }
private class DelayAndTerminate extends AbstractConsumerStage<String> { private static class DelayAndTerminate extends AbstractConsumerStage<String> {
private final long delayInMs;
public boolean finished; public boolean finished;
public DelayAndTerminate(final long delayInMs) {
super();
this.delayInMs = delayInMs;
}
@Override @Override
protected void execute(final String element) { protected void execute(final String element) {
try { try {
Thread.sleep(500); Thread.sleep(delayInMs);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
finished = true; finished = true;
......
...@@ -18,6 +18,7 @@ package teetime.framework.exceptionHandling; ...@@ -18,6 +18,7 @@ package teetime.framework.exceptionHandling;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import teetime.framework.Analysis; import teetime.framework.Analysis;
...@@ -49,6 +50,7 @@ public class ExceptionHandling { ...@@ -49,6 +50,7 @@ public class ExceptionHandling {
* where it checks if it should be terminated. * where it checks if it should be terminated.
*/ */
@Test(timeout = 30000) @Test(timeout = 30000)
@Ignore
public void forAFewTimes() { public void forAFewTimes() {
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
newInstances(); newInstances();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment