Skip to content

Commit 6421286

Browse files
deep20jainzhendrikse
authored andcommitted
BAEL-1029 - deep20jain@gmail.com - Addressing review comment (eugenp#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
1 parent 292d9f8 commit 6421286

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

core-java/src/test/java/com/baeldung/concurrent/atomic/CounterTest.java renamed to core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,10 @@
77
import java.util.concurrent.TimeUnit;
88
import java.util.stream.IntStream;
99

10-
import org.junit.Ignore;
1110
import org.junit.Test;
1211

13-
public class CounterTest {
12+
public class ThreadSafeCounterTest {
1413

15-
/**
16-
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling
17-
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will
18-
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads
19-
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this
20-
* test by adding Ignore annotation.
21-
*/
22-
@Test
23-
@Ignore
24-
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException {
25-
ExecutorService service = Executors.newFixedThreadPool(3);
26-
UnsafeCounter unsafeCounter = new UnsafeCounter();
27-
28-
IntStream.range(0, 1000)
29-
.forEach(count -> service.submit(unsafeCounter::increment));
30-
service.awaitTermination(100, TimeUnit.MILLISECONDS);
31-
32-
assertEquals(1000, unsafeCounter.getValue());
33-
}
34-
3514
@Test
3615
public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException {
3716
ExecutorService service = Executors.newFixedThreadPool(3);
@@ -55,4 +34,5 @@ public void givenMultiThread_whenSafeCounterWithoutLockIncrement() throws Interr
5534

5635
assertEquals(1000, safeCounter.getValue());
5736
}
37+
5838
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.concurrent.atomic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.stream.IntStream;
9+
10+
import org.junit.Test;
11+
12+
/**
13+
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling
14+
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will
15+
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads
16+
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this
17+
* test from build by adding this in manual test
18+
*/
19+
public class ThreadUnsafeCounterManualTest {
20+
21+
@Test
22+
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException {
23+
ExecutorService service = Executors.newFixedThreadPool(3);
24+
UnsafeCounter unsafeCounter = new UnsafeCounter();
25+
26+
IntStream.range(0, 1000)
27+
.forEach(count -> service.submit(unsafeCounter::increment));
28+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
29+
30+
assertEquals(1000, unsafeCounter.getValue());
31+
}
32+
33+
}

0 commit comments

Comments
 (0)