Updated tests for BAEL-1262 (#2917)

* updating test code for BAEL-1262

* some small change
This commit is contained in:
Mohamed Sanaulla 2017-10-30 19:52:49 +03:00 committed by maibin
parent 5a961c52f8
commit a193fbb53f
1 changed files with 42 additions and 14 deletions

View File

@ -8,7 +8,9 @@ import java.util.concurrent.Future;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -18,22 +20,34 @@ public class RunnableVsThreadTest {
private static Logger log = private static Logger log =
LoggerFactory.getLogger(RunnableVsThreadTest.class); LoggerFactory.getLogger(RunnableVsThreadTest.class);
private static ExecutorService executorService;
@BeforeClass
public static void setup() {
executorService = Executors.newCachedThreadPool();
}
@Test @Test
public void givenARunnable_whenRunIt_thenResult() throws Exception{ public void givenARunnable_whenRunIt_thenResult() throws Exception{
Thread thread = new Thread(new SimpleRunnable( Thread thread = new Thread(new SimpleRunnable(
"SimpleRunnable executed using Thread")); "SimpleRunnable executed using Thread"));
thread.start(); thread.start();
thread.join(); thread.join();
}
ExecutorService executorService =
Executors.newCachedThreadPool(); @Test
public void givenARunnable_whenSubmitToES_thenResult() throws Exception{
executorService.submit(new SimpleRunnable( executorService.submit(new SimpleRunnable(
"SimpleRunnable executed using ExecutorService")).get(); "SimpleRunnable executed using ExecutorService")).get();
}
@Test
public void givenARunnableLambda_whenSubmitToES_thenResult()
throws Exception{
executorService.submit(()-> executorService.submit(()->
log.info("Lambda runnable executed!!!")).get(); log.info("Lambda runnable executed!!!")).get();
executorService.shutdown();
} }
@Test @Test
@ -42,26 +56,39 @@ public class RunnableVsThreadTest {
"SimpleThread executed using Thread"); "SimpleThread executed using Thread");
thread.start(); thread.start();
thread.join(); thread.join();
}
@Test
public void givenAThread_whenSubmitToES_thenResult() throws Exception{
ExecutorService executorService =
Executors.newCachedThreadPool();
executorService.submit(new SimpleThread( executorService.submit(new SimpleThread(
"SimpleThread executed using ExecutorService")).get(); "SimpleThread executed using ExecutorService")).get();
} }
@Test @Test
public void givenACallable_whenRunIt_thenResult() throws Exception { public void givenACallable_whenSubmitToES_thenResult() throws Exception {
ExecutorService executorService =
Executors.newCachedThreadPool();
Future<Integer> future = executorService.submit(new SimpleCallable()); Future<Integer> future = executorService.submit(
new SimpleCallable());
log.info("Result from callable: {}", future.get()); log.info("Result from callable: {}", future.get());
}
@Test
public void givenACallableAsLambda_whenSubmitToES_thenResult()
throws Exception {
future = executorService.submit(() -> { Future<Integer> future = executorService.submit(() -> {
return RandomUtils.nextInt(0, 100); return RandomUtils.nextInt(0, 100);
}); });
log.info("Result from callable: {}", future.get()); log.info("Result from callable: {}", future.get());
}
@AfterClass
public static void tearDown() {
if ( executorService != null && !executorService.isShutdown()) {
executorService.shutdown();
}
} }
} }
@ -107,4 +134,5 @@ class SimpleCallable implements Callable<Integer> {
return RandomUtils.nextInt(0, 100); return RandomUtils.nextInt(0, 100);
} }
} }