From d5359a58e11adb613f11146a0b558566821da5b4 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Mon, 25 Nov 2019 21:34:15 +0100 Subject: [PATCH] BAEL-19699: Fix core-java-concurrency-basic unit tests --- .../concurrent/threadsafety/CounterUnitTest.java | 9 ++++++--- .../ExtrinsicLockCounterUnitTest.java | 9 ++++++--- .../ReentrantLockCounterUnitTest.java | 11 +++++++---- .../ReentrantReadWriteLockCounterUnitTest.java | 15 +++++++++------ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/CounterUnitTest.java b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/CounterUnitTest.java index 6761b05282..d8f8ff2b7d 100644 --- a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/CounterUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/CounterUnitTest.java @@ -18,8 +18,11 @@ public class CounterUnitTest { Counter counter = new Counter(); Future future1 = (Future) executorService.submit(new CounterCallable(counter)); Future future2 = (Future) executorService.submit(new CounterCallable(counter)); - - assertThat(future1.get()).isEqualTo(1); - assertThat(future2.get()).isEqualTo(2); + + // Just to make sure both are completed + future1.get(); + future2.get(); + + assertThat(counter.getCounter()).isEqualTo(2); } } diff --git a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ExtrinsicLockCounterUnitTest.java b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ExtrinsicLockCounterUnitTest.java index 2dcb846bf4..0fc3dd3a54 100644 --- a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ExtrinsicLockCounterUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ExtrinsicLockCounterUnitTest.java @@ -18,8 +18,11 @@ public class ExtrinsicLockCounterUnitTest { ExtrinsicLockCounter counter = new ExtrinsicLockCounter(); Future future1 = (Future) executorService.submit(new ExtrinsicLockCounterCallable(counter)); Future future2 = (Future) executorService.submit(new ExtrinsicLockCounterCallable(counter)); - - assertThat(future1.get()).isEqualTo(1); - assertThat(future2.get()).isEqualTo(2); + + // Just to make sure both are completed + future1.get(); + future2.get(); + + assertThat(counter.getCounter()).isEqualTo(2); } } diff --git a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantLockCounterUnitTest.java b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantLockCounterUnitTest.java index 330d2d4c86..9932aa9bbc 100644 --- a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantLockCounterUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantLockCounterUnitTest.java @@ -11,15 +11,18 @@ import java.util.concurrent.Future; import static org.assertj.core.api.Assertions.assertThat; public class ReentrantLockCounterUnitTest { - + @Test public void whenCalledIncrementCounter_thenCorrect() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(2); ReentrantLockCounter counter = new ReentrantLockCounter(); Future future1 = (Future) executorService.submit(new ReentrantLockCounterCallable(counter)); Future future2 = (Future) executorService.submit(new ReentrantLockCounterCallable(counter)); - - assertThat(future1.get()).isEqualTo(1); - assertThat(future2.get()).isEqualTo(2); + + // Just to make sure both are completed + future1.get(); + future2.get(); + + assertThat(counter.getCounter()).isEqualTo(2); } } diff --git a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantReadWriteLockCounterUnitTest.java b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantReadWriteLockCounterUnitTest.java index 71fad2c87f..c82cd39074 100644 --- a/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantReadWriteLockCounterUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic/src/test/java/com/baeldung/concurrent/threadsafety/ReentrantReadWriteLockCounterUnitTest.java @@ -11,16 +11,19 @@ import java.util.concurrent.Future; import static org.assertj.core.api.Assertions.assertThat; public class ReentrantReadWriteLockCounterUnitTest { - + @Test public void whenCalledIncrementCounter_thenCorrect() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(2); ReentrantReadWriteLockCounter counter = new ReentrantReadWriteLockCounter(); - Future future1 = (Future) executorService.submit(new ReentranReadWriteLockCounterCallable(counter)); - Future future2 = (Future) executorService.submit(new ReentranReadWriteLockCounterCallable(counter)); + Future future1 = (Future) executorService.submit(new ReentranReadWriteLockCounterCallable(counter)); + Future future2 = (Future) executorService.submit(new ReentranReadWriteLockCounterCallable(counter)); - assertThat(future2.get()).isEqualTo(2); - assertThat(future1.get()).isEqualTo(1); + // Just to make sure both are completed + future1.get(); + future2.get(); + + assertThat(counter.getCounter()).isEqualTo(2); } - + }