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;
* 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;
}
......
......@@ -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());
}
}
wiki @ 0e447457
Subproject commit 162510ff4d2f04011498ba6920aae0c78347c6c8
Subproject commit 0e4474577e1f49bc96e734c286b2d9e0363895e8
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;
......
......@@ -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();
......
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