package teetime.framework;

import org.junit.Test;

import teetime.stage.InitialElementProducer;

public class TerminationTest {

	@Test(timeout = 2000)
	public void doesNotGetStuckInAdd() throws InterruptedException {
		Execution<TerminationConfig> execution = new Execution<TerminationConfig>(new TerminationConfig(1));
		execution.executeNonBlocking();
		Thread.sleep(500);
		execution.abortEventually();
	}

	private class TerminationConfig extends Configuration {
		InitialElementProducer<Integer> init = new InitialElementProducer<Integer>(1, 2, 3, 4, 5, 6);
		DoesNotRetrieveElements sinkStage = new DoesNotRetrieveElements();

		public TerminationConfig(final int capacity) {
			connectPorts(init.getOutputPort(), sinkStage.getInputPort(), capacity);
			addThreadableStage(sinkStage);
		}

	}

	private class DoesNotRetrieveElements extends AbstractConsumerStage<Integer> {

		@Override
		protected void execute(final Integer element) {
			int i = 0;
			while (true) {
				i++;
				if (i > 1) {
					Thread.currentThread().interrupt();
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			}

		}

		@Override
		protected void terminate() {}

	}

}

@@ -32,68 +32,181 @@
 ort 
-org.junit.Test;%0A%0Aimport teetime.stage.InitialElementProducer
+static org.hamcrest.Matchers.greaterThan;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.junit.Assert.assertThat;%0A%0Aimport java.util.Arrays;%0A%0Aimport org.junit.Test
 ;%0A%0Ap
@@ -253,17 +253,17 @@
 meout = 
-2
+3
 000)%0A%09pu
@@ -325,16 +325,78 @@
 ption %7B%0A
+%09%09TerminationConfig configuration = new TerminationConfig(1);%0A
 %09%09Execut
@@ -467,32 +467,21 @@
 ig%3E(
-new TerminationConfig(1)
+configuration
 );%0A%09
@@ -528,17 +528,17 @@
 d.sleep(
-5
+1
 00);%0A%09%09e
@@ -565,16 +565,108 @@
 ally();%0A
+%09%09assertThat(configuration.sinkStage.time - 450, is(greaterThan(configuration.init.time)));%0A
 %09%7D%0A%0A%09pri
@@ -1047,24 +1047,532 @@
 );%0A%09%09%7D%0A%0A%09%7D%0A%0A
+%09private final class InitialElementProducer%3CT%3E extends AbstractProducerStage%3CT%3E %7B%0A%0A%09%09private final Iterable%3CT%3E elements;%0A%09%09public long time;%0A%0A%09%09public InitialElementProducer(final T... elements) %7B%0A%09%09%09this.elements = Arrays.asList(elements);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected void execute() %7B%0A%09%09%09for (final T element : this.elements) %7B%0A%09%09%09%09this.outputPort.send(element);%0A%09%09%09%7D%0A%09%09%09this.terminate();%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected void terminate() %7B%0A%09%09%09time = System.currentTimeMillis();%0A%09%09%09super.terminate();%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %09private cla
@@ -1640,16 +1640,37 @@
 ger%3E %7B%0A%0A
+%09%09public long time;%0A%0A
 %09%09@Overr
@@ -1772,18 +1772,11 @@
 %09%09%09%09
-if (i %3E 1)
+try
  %7B%0A%09
@@ -1790,48 +1790,94 @@
 ead.
-currentThread().interrupt();%0A%09%09%09%09%09break;
+sleep(500);%0A%09%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09%09// First sleep will throw this
 %0A%09%09%09
@@ -1879,27 +1879,34 @@
 s%0A%09%09%09%09%7D%0A%09%09%09%09
-try
+if (i %3E 1)
  %7B%0A%09%09%09%09%09Thre
@@ -1912,59 +1912,88 @@
 ead.
-sleep(1000);%0A%09%09%09%09%7D catch (InterruptedException e) %7B
+currentThread().interrupt();%0A%09%09%09%09%09time = System.currentTimeMillis();%0A%09%09%09%09%09break;
 %0A%09%09%09

test now somewhat representative