From 64212862bf2566db43daa44ba0d1c54cf5ac2f9e Mon Sep 17 00:00:00 2001 From: deep20jain Date: Fri, 21 Jul 2017 04:13:49 +0530 Subject: [PATCH] BAEL-1029 - deep20jain@gmail.com - Addressing review comment (#2294) * Adding test classes for java atomic variables * Updating counter with atomic integer * Adding reason for ignoring test * Removing ignore annotation and moving test to manual test * Removing counter test --- ...erTest.java => ThreadSafeCounterTest.java} | 24 ++------------ .../atomic/ThreadUnsafeCounterManualTest.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 22 deletions(-) rename core-java/src/test/java/com/baeldung/concurrent/atomic/{CounterTest.java => ThreadSafeCounterTest.java} (53%) create mode 100644 core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/atomic/CounterTest.java b/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java similarity index 53% rename from core-java/src/test/java/com/baeldung/concurrent/atomic/CounterTest.java rename to core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java index 1612c62513..e9b2e164ae 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/atomic/CounterTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java @@ -7,31 +7,10 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; -import org.junit.Ignore; import org.junit.Test; -public class CounterTest { +public class ThreadSafeCounterTest { - /** - * This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling - * the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will - * less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads - * called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this - * test by adding Ignore annotation. - */ - @Test - @Ignore - public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException { - ExecutorService service = Executors.newFixedThreadPool(3); - UnsafeCounter unsafeCounter = new UnsafeCounter(); - - IntStream.range(0, 1000) - .forEach(count -> service.submit(unsafeCounter::increment)); - service.awaitTermination(100, TimeUnit.MILLISECONDS); - - assertEquals(1000, unsafeCounter.getValue()); - } - @Test public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException { ExecutorService service = Executors.newFixedThreadPool(3); @@ -55,4 +34,5 @@ public class CounterTest { assertEquals(1000, safeCounter.getValue()); } + } diff --git a/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java b/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java new file mode 100644 index 0000000000..cc7cc18bb5 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java @@ -0,0 +1,33 @@ +package com.baeldung.concurrent.atomic; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import org.junit.Test; + +/** + * This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling + * the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will + * less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads + * called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this + * test from build by adding this in manual test + */ +public class ThreadUnsafeCounterManualTest { + + @Test + public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException { + ExecutorService service = Executors.newFixedThreadPool(3); + UnsafeCounter unsafeCounter = new UnsafeCounter(); + + IntStream.range(0, 1000) + .forEach(count -> service.submit(unsafeCounter::increment)); + service.awaitTermination(100, TimeUnit.MILLISECONDS); + + assertEquals(1000, unsafeCounter.getValue()); + } + +}