BAEL-4033 LinkedBlockingQueue vs ConcurrentLinkedQueue (#9281)
* BAEL-4033 Added unit test cases for LinkedBlockingQueue and ConcurrentLinkedQueue * BAEL-4033 Updated unit test class names due to PMD rule violations * Moved files to another module based on the editor review
This commit is contained in:
parent
c395a43472
commit
73762873f6
@ -23,7 +23,12 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
@ -42,6 +47,8 @@
|
||||
<properties>
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<guava.version>28.2-jre</guava.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,66 @@
|
||||
package com.baeldung.concurrent.queue;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
@FixMethodOrder
|
||||
public class TestConcurrentLinkedQueue {
|
||||
|
||||
@Test
|
||||
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
|
||||
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(elements);
|
||||
assertThat(concurrentLinkedQueue).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenQueueReturnsNull() throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
|
||||
executorService.submit(() -> assertNull("Retrieve object is null", concurrentLinkedQueue.poll()));
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProducerOffersElementInQueue_WhenConsumerPollsQueue_ThenItRetrievesElement() throws Exception {
|
||||
int element = 1;
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
|
||||
Runnable offerTask = () -> concurrentLinkedQueue.offer(element);
|
||||
|
||||
Callable<Integer> pollTask = () -> {
|
||||
while (concurrentLinkedQueue.peek() != null) {
|
||||
return concurrentLinkedQueue.poll()
|
||||
.intValue();
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
executorService.submit(offerTask);
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
|
||||
Future<Integer> returnedElement = executorService.submit(pollTask);
|
||||
assertThat(returnedElement.get()
|
||||
.intValue(), is(equalTo(element)));
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.baeldung.concurrent.queue;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
@FixMethodOrder
|
||||
public class TestLinkedBlockingQueue {
|
||||
|
||||
@Test
|
||||
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
|
||||
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>(elements);
|
||||
assertThat(linkedBlockingQueue).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenThreadBlocks() throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
linkedBlockingQueue.take();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProducerPutsElementInQueue_WhenConsumerAccessQueue_ThenItRetrieve() {
|
||||
int element = 10;
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
|
||||
Runnable putTask = () -> {
|
||||
try {
|
||||
linkedBlockingQueue.put(element);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
Callable<Integer> takeTask = () -> {
|
||||
try {
|
||||
return linkedBlockingQueue.take();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
executorService.submit(putTask);
|
||||
Future<Integer> returnElement = executorService.submit(takeTask);
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
assertThat(returnElement.get()
|
||||
.intValue(), is(equalTo(element)));
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user