1. grainednoise
  2. deferred-execution

Commits

grainednoise  committed 333a92c

Some tests added

  • Participants
  • Parent commits f8d7a7a
  • Branches default

Comments (0)

Files changed (3)

File src/test/java/org/grainednoise/deferred/BasicTest.java

View file
  • Ignore whitespace
+package org.grainednoise.deferred;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.grainednoise.deferred.combinators.Functions;
+import org.grainednoise.deferred.combinators.Parallel;
+import org.grainednoise.deferred.combinators.Sequential;
+import org.grainednoise.deferred.core.Tuple;
+import org.grainednoise.deferred.core.Tuple.Tuple2;
+import org.grainednoise.deferred.core.Waiter;
+import org.grainednoise.deferred.functions.Dfunc0;
+import org.grainednoise.deferred.functions.FastFunc0;
+import org.grainednoise.deferred.functions.SlowFunc0;
+import org.grainednoise.deferred.functions.SlowFunc1;
+import org.grainednoise.deferred.util.SimpleDeferredExecutor;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class BasicTest {
+	
+	private static final SlowFunc0<Integer> FUNC_5_SLOW = new SlowFunc0<Integer>() {
+
+		@Override
+		protected Integer calculate() {
+			return 5;
+		}
+	};
+
+	private static final FastFunc0<Integer> FUNC_5_FAST = new FastFunc0<Integer>() {
+		
+		@Override
+		protected Integer calculate() {
+			return 5;
+		}
+	};
+	
+	private static final SlowFunc0<Integer> FUNC_7_SLOW = new SlowFunc0<Integer>() {
+		
+		@Override
+		protected Integer calculate() {
+			return 7;
+		}
+	};
+	
+	private static final FastFunc0<Integer> FUNC_7_FAST = new FastFunc0<Integer>() {
+		
+		@Override
+		protected Integer calculate() {
+			return 7;
+		}
+	};
+	
+	private static final SlowFunc1<Integer, Integer> FUNC_DOUBLE_SLOW = new SlowFunc1<Integer, Integer>() {
+		
+		@Override
+		protected Integer calculate(Integer param) {
+			return param * 2;
+		}
+	};
+	
+	
+	private Executor coreExecutor;
+	private SimpleDeferredExecutor executor;
+
+	@Before
+	public void setup() {
+		coreExecutor = new ThreadPoolExecutor(5, 5, 20, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(20));
+		executor = new SimpleDeferredExecutor(coreExecutor);
+		
+	}
+	
+	@Test
+	public void simpleWait() {
+		Integer result = executor.executeAndWait(Functions.withDummyParameter(FUNC_5_SLOW), null);
+		assertEquals(5, result.intValue());
+	}
+
+	@Test
+	public void simpleDeferred() {
+		Waiter<Integer> waiter = executor.executeAsync(Functions.withDummyParameter(FUNC_5_SLOW), null);
+		assertEquals(5, waiter.waitForValue().intValue());
+	}
+
+	@Test
+	public void simpleDeferredChained() {
+		Dfunc0<Integer> chained = Sequential.chain(FUNC_5_SLOW, FUNC_DOUBLE_SLOW);
+		
+		Waiter<Integer> waiter = executor.executeAsync(Functions.withDummyParameter(chained), null);
+		assertEquals(10, waiter.waitForValue().intValue());
+	}
+	
+	@Test
+	public void simpleDeferredParallel() {
+		Dfunc0<Tuple2<Integer, Integer>> parallel = Parallel.parallel(FUNC_5_SLOW, FUNC_7_SLOW);
+		Waiter<Tuple2<Integer, Integer>> waiter = executor.executeAsync(Functions.withDummyParameter(parallel), null);
+		assertEquals(Tuple.of(5, 7), waiter.waitForValue());
+
+		parallel = Parallel.parallel(FUNC_5_SLOW, FUNC_7_FAST);
+		waiter = executor.executeAsync(Functions.withDummyParameter(parallel), null);
+		assertEquals(Tuple.of(5, 7), waiter.waitForValue());
+
+		parallel = Parallel.parallel(FUNC_5_FAST, FUNC_7_SLOW);
+		waiter = executor.executeAsync(Functions.withDummyParameter(parallel), null);
+		assertEquals(Tuple.of(5, 7), waiter.waitForValue());
+
+		parallel = Parallel.parallel(FUNC_5_FAST, FUNC_7_FAST);
+		waiter = executor.executeAsync(Functions.withDummyParameter(parallel), null);
+		assertEquals(Tuple.of(5, 7), waiter.waitForValue());
+	}
+	
+	
+	
+	
+
+}

File src/test/java/org/grainednoise/deferred/TestHelper.java

View file
  • Ignore whitespace
+package org.grainednoise.deferred;
+
+import org.grainednoise.deferred.core.DefaultWaiter;
+
+public class TestHelper {
+
+	public static final boolean isWaitingInWaiter(Thread thread) {
+		if (thread.getState() != Thread.State.WAITING) {
+			return false;
+		}
+		
+		StackTraceElement[] stackTrace = thread.getStackTrace();
+		
+		for (StackTraceElement elem : stackTrace) {
+			if (elem.getClassName().equals(DefaultWaiter.class.getName()) && elem.getMethodName().equals("waitForValueToBecomeAvailable")) {
+				return true;
+			}
+			
+		}
+		
+		return false;
+	}
+
+}

File src/test/java/org/grainednoise/deferred/TestWait.java

View file
  • Ignore whitespace
 package org.grainednoise.deferred;
 
-import static org.grainednoise.deferred.DeterministicExecutor.*;
+import static org.grainednoise.deferred.TestHelper.*;
 import static org.junit.Assert.*;
 
 import java.lang.Thread.State;
 		
 		thread.join();
 	}
+	
+	
 }