Adding code for article tracked under BAEL-3573. (#8999)
Co-authored-by: CHANDRAKANT Kumar <kumar.chandrakant@soprabanking.com>
This commit is contained in:
parent
b56b71cf5f
commit
b4610e110f
26
core-java-modules/core-java-concurrency-testing/.gitignore
vendored
Normal file
26
core-java-modules/core-java-concurrency-testing/.gitignore
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
0.*
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
.resourceCache
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
||||||
|
|
||||||
|
# Files generated by integration tests
|
||||||
|
*.txt
|
||||||
|
backup-pom.xml
|
||||||
|
/bin/
|
||||||
|
/temp
|
||||||
|
|
||||||
|
#IntelliJ specific
|
||||||
|
.idea/
|
||||||
|
*.iml
|
@ -0,0 +1,7 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Concurrency Testing Examples
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Testing Multi-Threaded Code in Java](https://www.baeldung.com/java-testing-multithreaded)
|
||||||
|
|
93
core-java-modules/core-java-concurrency-testing/pom.xml
Normal file
93
core-java-modules/core-java-concurrency-testing/pom.xml
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-concurrency-testing</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-concurrency-testing</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.thread-weaver</groupId>
|
||||||
|
<artifactId>threadweaver</artifactId>
|
||||||
|
<version>0.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.tempus-fugit</groupId>
|
||||||
|
<artifactId>tempus-fugit</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.multithreadedtc</groupId>
|
||||||
|
<artifactId>multithreadedtc</artifactId>
|
||||||
|
<version>1.01</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jcstress</groupId>
|
||||||
|
<artifactId>jcstress-core</artifactId>
|
||||||
|
<version>0.5</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.1</version>
|
||||||
|
<configuration>
|
||||||
|
<compilerVersion>${javac.target}</compilerVersion>
|
||||||
|
<source>${javac.target}</source>
|
||||||
|
<target>${javac.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>main</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<finalName>jcstress</finalName>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>org.openjdk.jcstress.Main</mainClass>
|
||||||
|
</transformer>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||||
|
<resource>META-INF/TestList</resource>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
public class MyCounter {
|
||||||
|
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public void increment() {
|
||||||
|
int temp = count;
|
||||||
|
count = temp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void incrementWithWait() throws InterruptedException {
|
||||||
|
int temp = count;
|
||||||
|
wait(100);
|
||||||
|
count = temp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN" />
|
||||||
|
<logger name="org.springframework.transaction" level="WARN" />
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
|
||||||
|
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE_INTERESTING;
|
||||||
|
|
||||||
|
import org.openjdk.jcstress.annotations.Actor;
|
||||||
|
import org.openjdk.jcstress.annotations.Arbiter;
|
||||||
|
import org.openjdk.jcstress.annotations.JCStressTest;
|
||||||
|
import org.openjdk.jcstress.annotations.Outcome;
|
||||||
|
import org.openjdk.jcstress.annotations.State;
|
||||||
|
import org.openjdk.jcstress.infra.results.I_Result;
|
||||||
|
|
||||||
|
@JCStressTest
|
||||||
|
@Outcome(id = "1", expect = ACCEPTABLE_INTERESTING, desc = "One update lost.")
|
||||||
|
@Outcome(id = "2", expect = ACCEPTABLE, desc = "Both updates.")
|
||||||
|
@State
|
||||||
|
public class MyCounterJCStressUnitTest {
|
||||||
|
|
||||||
|
private MyCounter counter;
|
||||||
|
|
||||||
|
@Actor
|
||||||
|
public void actor1() {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Actor
|
||||||
|
public void actor2() {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Arbiter
|
||||||
|
public void arbiter(I_Result r) {
|
||||||
|
r.r1 = counter.getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import edu.umd.cs.mtc.MultithreadedTestCase;
|
||||||
|
import edu.umd.cs.mtc.TestFramework;
|
||||||
|
|
||||||
|
public class MyCounterMultithreadedTCUnitTest extends MultithreadedTestCase {
|
||||||
|
|
||||||
|
private MyCounter counter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize() {
|
||||||
|
counter = new MyCounter();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void thread1() throws InterruptedException {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void thread2() throws InterruptedException {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Override
|
||||||
|
public void finish() {
|
||||||
|
assertEquals(2, counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCounter() throws Throwable {
|
||||||
|
TestFramework.runManyTimes(new MyCounterMultithreadedTCUnitTest(), 1000);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MyCounterSimpleUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCounter() {
|
||||||
|
MyCounter counter = new MyCounter();
|
||||||
|
for (int i = 0; i < 500; i++)
|
||||||
|
counter.increment();
|
||||||
|
assertEquals(500, counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
public void testCounterWithConcurrency() throws InterruptedException {
|
||||||
|
int numberOfThreads = 100;
|
||||||
|
ExecutorService service = Executors.newFixedThreadPool(10);
|
||||||
|
CountDownLatch latch = new CountDownLatch(numberOfThreads);
|
||||||
|
MyCounter counter = new MyCounter();
|
||||||
|
for (int i = 0; i < numberOfThreads; i++) {
|
||||||
|
service.execute(() -> {
|
||||||
|
counter.increment();
|
||||||
|
latch.countDown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
latch.await();
|
||||||
|
assertEquals(numberOfThreads, counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
public void testSummationWithConcurrencyAndWait() throws InterruptedException {
|
||||||
|
int numberOfThreads = 2;
|
||||||
|
ExecutorService service = Executors.newFixedThreadPool(10);
|
||||||
|
CountDownLatch latch = new CountDownLatch(numberOfThreads);
|
||||||
|
MyCounter counter = new MyCounter();
|
||||||
|
for (int i = 0; i < numberOfThreads; i++) {
|
||||||
|
service.submit(() -> {
|
||||||
|
try {
|
||||||
|
counter.incrementWithWait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
latch.countDown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
latch.await();
|
||||||
|
assertEquals(numberOfThreads, counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.google.code.tempusfugit.concurrency.ConcurrentRule;
|
||||||
|
import com.google.code.tempusfugit.concurrency.RepeatingRule;
|
||||||
|
import com.google.code.tempusfugit.concurrency.annotations.Concurrent;
|
||||||
|
import com.google.code.tempusfugit.concurrency.annotations.Repeating;
|
||||||
|
|
||||||
|
public class MyCounterTempusFugitUnitTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ConcurrentRule concurrently = new ConcurrentRule();
|
||||||
|
@Rule
|
||||||
|
public RepeatingRule rule = new RepeatingRule();
|
||||||
|
|
||||||
|
private static MyCounter counter = new MyCounter();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Concurrent(count = 2)
|
||||||
|
@Repeating(repetition = 10)
|
||||||
|
public void runsMultipleTimes() {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void annotatedTestRunsMultipleTimes() throws InterruptedException {
|
||||||
|
assertEquals(counter.getCount(), 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.concurrent;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.google.testing.threadtester.AnnotatedTestRunner;
|
||||||
|
import com.google.testing.threadtester.ThreadedAfter;
|
||||||
|
import com.google.testing.threadtester.ThreadedBefore;
|
||||||
|
import com.google.testing.threadtester.ThreadedMain;
|
||||||
|
import com.google.testing.threadtester.ThreadedSecondary;
|
||||||
|
|
||||||
|
public class MyCounterThreadWeaverUnitTest {
|
||||||
|
|
||||||
|
private MyCounter counter;
|
||||||
|
|
||||||
|
@ThreadedBefore
|
||||||
|
public void before() {
|
||||||
|
counter = new MyCounter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ThreadedMain
|
||||||
|
public void mainThread() {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ThreadedSecondary
|
||||||
|
public void secondThread() {
|
||||||
|
counter.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ThreadedAfter
|
||||||
|
public void after() {
|
||||||
|
assertEquals(2, counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCounter() {
|
||||||
|
new AnnotatedTestRunner().runTests(this.getClass(), MyCounter.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
core-java-modules/core-java-concurrency-testing/src/test/resources/.gitignore
vendored
Normal file
13
core-java-modules/core-java-concurrency-testing/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
Loading…
x
Reference in New Issue
Block a user