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()); + } + +}