Split or move core-java-modules/core-java-concurrency-basic module (#7799)

This commit is contained in:
Catalin Burcea
2019-09-23 16:55:02 +03:00
committed by Josh Cummings
parent dac59fbec2
commit 6073d2d451
30 changed files with 114 additions and 42 deletions

View File

@@ -1,37 +0,0 @@
package com.baeldung.concurrent.synchronize;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
public class BaeldungSychronizedBlockUnitTest {
@Test
public void givenMultiThread_whenBlockSync() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedBlocks synchronizedBlocks = new BaeldungSynchronizedBlocks();
IntStream.range(0, 1000)
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
service.awaitTermination(500, TimeUnit.MILLISECONDS);
assertEquals(1000, synchronizedBlocks.getCount());
}
@Test
public void givenMultiThread_whenStaticSyncBlock() throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
service.awaitTermination(500, TimeUnit.MILLISECONDS);
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());
}
}

View File

@@ -1,51 +0,0 @@
package com.baeldung.concurrent.synchronize;
import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
public class BaeldungSynchronizeMethodsUnitTest {
@Test
@Ignore
public void givenMultiThread_whenNonSyncMethod() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
IntStream.range(0, 1000)
.forEach(count -> service.submit(method::calculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, method.getSum());
}
@Test
public void givenMultiThread_whenMethodSync() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
IntStream.range(0, 1000)
.forEach(count -> service.submit(method::synchronisedCalculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, method.getSyncSum());
}
@Test
public void givenMultiThread_whenStaticSyncMethod() throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
.forEach(count -> service.submit(BaeldungSynchronizedMethods::syncStaticCalculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, BaeldungSynchronizedMethods.staticSum);
}
}

View File

@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.CounterCallable;
import com.baeldung.concurrent.threadsafety.services.Counter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class CounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {

View File

@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExtrinsicLockCounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class ExtrinsicLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {

View File

@@ -1,13 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.mathutils.MathUtils;
import org.junit.Test;
import java.math.BigInteger;
import static org.assertj.core.api.Assertions.assertThat;
public class MathUtilsTest {
public class MathUtilsUnitTest {
@Test
public void whenCalledFactorialMethod_thenCorrect() {
assertThat(MathUtils.factorial(2)).isEqualTo(2);
assertThat(MathUtils.factorial(2)).isEqualTo(new BigInteger("2"));
}
}

View File

@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.MessageServiceCallable;
import com.baeldung.concurrent.threadsafety.services.MessageService;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class MessageServiceTest {
import static org.assertj.core.api.Assertions.assertThat;
public class MessageServiceUnitTest {
@Test
public void whenCalledgetMessage_thenCorrect() throws Exception {

View File

@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class ReentrantLockCounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class ReentrantLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {

View File

@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class ReentrantReadWriteLockCounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class ReentrantReadWriteLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
@@ -16,9 +18,9 @@ public class ReentrantReadWriteLockCounterTest {
ReentrantReadWriteLockCounter counter = new ReentrantReadWriteLockCounter();
Future<Integer> future1 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
Future<Integer> future2 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
assertThat(future1.get()).isEqualTo(1);
assertThat(future2.get()).isEqualTo(2);
assertThat(future1.get()).isEqualTo(1);
}
}

View File

@@ -1,66 +0,0 @@
package com.baeldung.concurrent.waitandnotify;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class NetworkIntegrationTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private String expected;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@Before
public void setUpExpectedOutput() {
StringWriter expectedStringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
printWriter.println("First packet");
printWriter.println("Second packet");
printWriter.println("Third packet");
printWriter.println("Fourth packet");
printWriter.close();
expected = expectedStringWriter.toString();
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
@Test
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
Data data = new Data();
Thread sender = new Thread(new Sender(data));
Thread receiver = new Thread(new Receiver(data));
sender.start();
receiver.start();
//wait for sender and receiver to finish before we test against expected
try {
sender.join();
receiver.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
assertEquals(expected, outContent.toString());
}
}