|
| 1 | +package com.baeldung.concurrent.future; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.concurrent.CancellationException; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.Future; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.TimeoutException; |
| 12 | + |
| 13 | +import org.junit.After; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Rule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.rules.TestName; |
| 18 | + |
| 19 | +public class SquareCalculatorUnitTest { |
| 20 | + |
| 21 | + @Rule |
| 22 | + public TestName name = new TestName(); |
| 23 | + |
| 24 | + private long start; |
| 25 | + |
| 26 | + private SquareCalculator squareCalculator; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenExecutorIsSingleThreaded_whenTwoExecutionsAreTriggered_thenRunInSequence() throws InterruptedException, ExecutionException { |
| 30 | + squareCalculator = new SquareCalculator(Executors.newSingleThreadExecutor()); |
| 31 | + |
| 32 | + Future<Integer> result1 = squareCalculator.calculate(4); |
| 33 | + Future<Integer> result2 = squareCalculator.calculate(1000); |
| 34 | + |
| 35 | + while (!result1.isDone() || !result2.isDone()) { |
| 36 | + System.out.println(String.format("Task 1 is %s and Task 2 is %s.", result1.isDone() ? "done" : "not done", result2.isDone() ? "done" : "not done")); |
| 37 | + |
| 38 | + Thread.sleep(300); |
| 39 | + } |
| 40 | + |
| 41 | + assertEquals(16, result1.get().intValue()); |
| 42 | + assertEquals(1000000, result2.get().intValue()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test(expected = TimeoutException.class) |
| 46 | + public void whenGetWithTimeoutLowerThanExecutionTime_thenThrowException() throws InterruptedException, ExecutionException, TimeoutException { |
| 47 | + squareCalculator = new SquareCalculator(Executors.newSingleThreadExecutor()); |
| 48 | + |
| 49 | + Future<Integer> result = squareCalculator.calculate(4); |
| 50 | + |
| 51 | + result.get(500, TimeUnit.MILLISECONDS); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenExecutorIsMultiThreaded_whenTwoExecutionsAreTriggered_thenRunInParallel() throws InterruptedException, ExecutionException { |
| 56 | + squareCalculator = new SquareCalculator(Executors.newFixedThreadPool(2)); |
| 57 | + |
| 58 | + Future<Integer> result1 = squareCalculator.calculate(4); |
| 59 | + Future<Integer> result2 = squareCalculator.calculate(1000); |
| 60 | + |
| 61 | + while (!result1.isDone() || !result2.isDone()) { |
| 62 | + System.out.println(String.format("Task 1 is %s and Task 2 is %s.", result1.isDone() ? "done" : "not done", result2.isDone() ? "done" : "not done")); |
| 63 | + |
| 64 | + Thread.sleep(300); |
| 65 | + } |
| 66 | + |
| 67 | + assertEquals(16, result1.get().intValue()); |
| 68 | + assertEquals(1000000, result2.get().intValue()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test(expected = CancellationException.class) |
| 72 | + public void whenCancelFutureAndCallGet_thenThrowException() throws InterruptedException, ExecutionException, TimeoutException { |
| 73 | + squareCalculator = new SquareCalculator(Executors.newSingleThreadExecutor()); |
| 74 | + |
| 75 | + Future<Integer> result = squareCalculator.calculate(4); |
| 76 | + |
| 77 | + boolean canceled = result.cancel(true); |
| 78 | + |
| 79 | + assertTrue("Future was canceled", canceled); |
| 80 | + assertTrue("Future was canceled", result.isCancelled()); |
| 81 | + |
| 82 | + result.get(); |
| 83 | + } |
| 84 | + |
| 85 | + @Before |
| 86 | + public void start() { |
| 87 | + start = System.currentTimeMillis(); |
| 88 | + } |
| 89 | + |
| 90 | + @After |
| 91 | + public void end() { |
| 92 | + System.out.println(String.format("Test %s took %s ms \n", name.getMethodName(), System.currentTimeMillis() - start)); |
| 93 | + } |
| 94 | +} |
0 commit comments