Testing multithreading (#9193)

This commit is contained in:
Kumar Chandrakant 2020-04-28 09:48:00 +05:30 committed by GitHub
parent 068523493b
commit 57519b9dae
12 changed files with 90 additions and 135 deletions

View File

@ -4,4 +4,5 @@
### Relevant Articles: ### Relevant Articles:
- [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex) - [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex)
- [Testing Multi-Threaded Code in Java] (https://www.baeldung.com/java-testing-multithreaded)

View File

@ -16,6 +16,38 @@
<relativePath>../../parent-java</relativePath> <relativePath>../../parent-java</relativePath>
</parent> </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> <build>
<finalName>core-java-concurrency-2</finalName> <finalName>core-java-concurrency-2</finalName>
<resources> <resources>
@ -24,6 +56,51 @@
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
<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> </build>
<properties>
<javac.target>1.8</javac.target>
</properties>
</project> </project>

View File

@ -1,5 +1,6 @@
package com.baeldung.concurrent; package com.baeldung.concurrent;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import edu.umd.cs.mtc.MultithreadedTestCase; import edu.umd.cs.mtc.MultithreadedTestCase;
@ -25,9 +26,10 @@ public class MyCounterMultithreadedTCUnitTest extends MultithreadedTestCase {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void finish() { public void finish() {
assertEquals(2, counter.getCount()); assertEquals(2, counter.getCount());
} }
@Ignore
@Test @Test
public void testCounter() throws Throwable { public void testCounter() throws Throwable {
TestFramework.runManyTimes(new MyCounterMultithreadedTCUnitTest(), 1000); TestFramework.runManyTimes(new MyCounterMultithreadedTCUnitTest(), 1000);

View File

@ -6,6 +6,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class MyCounterSimpleUnitTest { public class MyCounterSimpleUnitTest {
@ -18,7 +19,8 @@ public class MyCounterSimpleUnitTest {
assertEquals(500, counter.getCount()); assertEquals(500, counter.getCount());
} }
// @Test @Ignore
@Test
public void testCounterWithConcurrency() throws InterruptedException { public void testCounterWithConcurrency() throws InterruptedException {
int numberOfThreads = 100; int numberOfThreads = 100;
ExecutorService service = Executors.newFixedThreadPool(10); ExecutorService service = Executors.newFixedThreadPool(10);
@ -34,7 +36,8 @@ public class MyCounterSimpleUnitTest {
assertEquals(numberOfThreads, counter.getCount()); assertEquals(numberOfThreads, counter.getCount());
} }
// @Test @Ignore
@Test
public void testSummationWithConcurrencyAndWait() throws InterruptedException { public void testSummationWithConcurrencyAndWait() throws InterruptedException {
int numberOfThreads = 2; int numberOfThreads = 2;
ExecutorService service = Executors.newFixedThreadPool(10); ExecutorService service = Executors.newFixedThreadPool(10);

View File

@ -3,6 +3,7 @@ package com.baeldung.concurrent;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -20,6 +21,7 @@ public class MyCounterTempusFugitUnitTest {
private static MyCounter counter = new MyCounter(); private static MyCounter counter = new MyCounter();
@Ignore
@Test @Test
@Concurrent(count = 2) @Concurrent(count = 2)
@Repeating(repetition = 10) @Repeating(repetition = 10)

View File

@ -2,6 +2,7 @@ package com.baeldung.concurrent;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import com.google.testing.threadtester.AnnotatedTestRunner; import com.google.testing.threadtester.AnnotatedTestRunner;
@ -34,6 +35,7 @@ public class MyCounterThreadWeaverUnitTest {
assertEquals(2, counter.getCount()); assertEquals(2, counter.getCount());
} }
@Ignore
@Test @Test
public void testCounter() { public void testCounter() {
new AnnotatedTestRunner().runTests(this.getClass(), MyCounter.class); new AnnotatedTestRunner().runTests(this.getClass(), MyCounter.class);

View File

@ -1,7 +0,0 @@
=========
## Core Java Concurrency Testing Examples
### Relevant Articles:
- [Testing Multi-Threaded Code in Java](https://www.baeldung.com/java-testing-multithreaded)

View File

@ -1,93 +0,0 @@
<?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>

View File

@ -1,19 +0,0 @@
<?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>

View File

@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear