From 0a5a54f6d09f85dbc35298647bc3eaaed5d177a2 Mon Sep 17 00:00:00 2001 From: pedrolopes Date: Thu, 22 Dec 2022 19:18:05 -0300 Subject: [PATCH] BAEL-5946: Fix unit tests (#13187) * consistent read unit test * unit test to show consistency in writing to the same key of map * testing writes at different map key * fix to bugs on test 1 and 2 * removing repeated tests --- .../ConcurrentHashMapUnitTest.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrenthashmap/ConcurrentHashMapUnitTest.java b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrenthashmap/ConcurrentHashMapUnitTest.java index 6a6aab868e..876c2f9c01 100644 --- a/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrenthashmap/ConcurrentHashMapUnitTest.java +++ b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrenthashmap/ConcurrentHashMapUnitTest.java @@ -14,11 +14,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class ConcurrentHashMapUnitTest { - private Map frequencyMap; + private Map frequencyMap = new ConcurrentHashMap<>(); @BeforeEach public void setup() { - frequencyMap = new ConcurrentHashMap<>(); frequencyMap.put(0, 0); frequencyMap.put(1, 0); frequencyMap.put(2, 0); @@ -35,24 +34,22 @@ public class ConcurrentHashMapUnitTest { Runnable writeAfter1Sec = () -> frequencyMap.computeIfPresent(1, (k, v) -> { sleep(1); - return frequencyMap.get(k) + 1; + return v + 1; }); Callable readNow = () -> frequencyMap.get(1); - Callable readAfter1001Ms = () -> { - TimeUnit.MILLISECONDS.sleep(1001); + Callable readAfter2sec = () -> { + sleep(2); return frequencyMap.get(1); }; threadExecutor.submit(writeAfter1Sec); - List> results = threadExecutor.invokeAll(asList(readNow, readAfter1001Ms)); + List> results = threadExecutor.invokeAll(asList(readNow, readAfter2sec)); assertEquals(0, results.get(0).get()); assertEquals(1, results.get(1).get()); - if (threadExecutor.awaitTermination(2, TimeUnit.SECONDS)) { - threadExecutor.shutdown(); - } + threadExecutor.shutdown(); } @Test @@ -61,47 +58,45 @@ public class ConcurrentHashMapUnitTest { Callable writeAfter5Sec = () -> frequencyMap.computeIfPresent(1, (k, v) -> { sleep(5); - return frequencyMap.get(k) + 1; + return v + 1; }); Callable writeAfter1Sec = () -> frequencyMap.computeIfPresent(1, (k, v) -> { sleep(1); - return frequencyMap.get(k) + 1; + return v + 1; }); - List> results = threadExecutor.invokeAll(asList(writeAfter5Sec, writeAfter1Sec)); + Future result1 = threadExecutor.submit(writeAfter5Sec); + sleep(1); + Future result2 = threadExecutor.submit(writeAfter1Sec); - assertEquals(1, results.get(0).get()); - assertEquals(2, results.get(1).get()); + assertEquals(1, result1.get()); + assertEquals(2, result2.get()); - if (threadExecutor.awaitTermination(2, TimeUnit.SECONDS)) { - threadExecutor.shutdown(); - } + threadExecutor.shutdown(); } @Test - public void givenOneThreadIsWriting_whenAnotherThreadWritesAtDifferentKey_thenNotWaitAndGetCorrectValue() throws Exception { + public void givenOneThreadIsWriting_whenAnotherThreadWritesAtDifferentKey_thenNotWait() throws InterruptedException { ExecutorService threadExecutor = Executors.newFixedThreadPool(2); Callable writeAfter5Sec = () -> frequencyMap.computeIfPresent(1, (k, v) -> { sleep(5); - return frequencyMap.get(k) + 1; + return v + 1; }); AtomicLong time = new AtomicLong(System.currentTimeMillis()); Callable writeAfter1Sec = () -> frequencyMap.computeIfPresent(2, (k, v) -> { sleep(1); time.set((System.currentTimeMillis() - time.get()) / 1000); - return frequencyMap.get(k) + 1; + return v + 1; }); threadExecutor.invokeAll(asList(writeAfter5Sec, writeAfter1Sec)); assertEquals(1, time.get()); - if (threadExecutor.awaitTermination(2, TimeUnit.SECONDS)) { - threadExecutor.shutdown(); - } + threadExecutor.shutdown(); } private static void sleep(int timeout) {