Skip to content

Commit 647896c

Browse files
authored
BAEL-4033 LinkedBlockingQueue vs ConcurrentLinkedQueue (eugenp#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
1 parent 3b733e1 commit 647896c

3 files changed

Lines changed: 155 additions & 1 deletion

File tree

core-java-modules/core-java-concurrency-collections-2/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
<artifactId>jmh-generator-annprocess</artifactId>
2424
<version>${jmh.version}</version>
2525
</dependency>
26-
26+
<dependency>
27+
<groupId>org.assertj</groupId>
28+
<artifactId>assertj-core</artifactId>
29+
<version>${assertj.version}</version>
30+
<scope>test</scope>
31+
</dependency>
2732
</dependencies>
2833
<build>
2934
<sourceDirectory>src</sourceDirectory>
@@ -42,6 +47,8 @@
4247
<properties>
4348
<jmh.version>1.21</jmh.version>
4449
<guava.version>28.2-jre</guava.version>
50+
<!-- testing -->
51+
<assertj.version>3.6.1</assertj.version>
4552
</properties>
4653

4754
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.concurrent.queue;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertNull;
7+
import static org.junit.Assert.assertThat;
8+
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
import java.util.concurrent.Callable;
12+
import java.util.concurrent.ConcurrentLinkedQueue;
13+
import java.util.concurrent.ExecutorService;
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.Future;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import org.junit.FixMethodOrder;
19+
import org.junit.Test;
20+
21+
@FixMethodOrder
22+
public class TestConcurrentLinkedQueue {
23+
24+
@Test
25+
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
26+
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
27+
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(elements);
28+
assertThat(concurrentLinkedQueue).containsExactly(1, 2, 3, 4, 5);
29+
}
30+
31+
@Test
32+
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenQueueReturnsNull() throws InterruptedException {
33+
ExecutorService executorService = Executors.newFixedThreadPool(1);
34+
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
35+
executorService.submit(() -> assertNull("Retrieve object is null", concurrentLinkedQueue.poll()));
36+
TimeUnit.SECONDS.sleep(1);
37+
executorService.awaitTermination(1, TimeUnit.SECONDS);
38+
executorService.shutdown();
39+
}
40+
41+
@Test
42+
public void givenProducerOffersElementInQueue_WhenConsumerPollsQueue_ThenItRetrievesElement() throws Exception {
43+
int element = 1;
44+
45+
ExecutorService executorService = Executors.newFixedThreadPool(2);
46+
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
47+
Runnable offerTask = () -> concurrentLinkedQueue.offer(element);
48+
49+
Callable<Integer> pollTask = () -> {
50+
while (concurrentLinkedQueue.peek() != null) {
51+
return concurrentLinkedQueue.poll()
52+
.intValue();
53+
}
54+
return null;
55+
};
56+
57+
executorService.submit(offerTask);
58+
TimeUnit.SECONDS.sleep(1);
59+
60+
Future<Integer> returnedElement = executorService.submit(pollTask);
61+
assertThat(returnedElement.get()
62+
.intValue(), is(equalTo(element)));
63+
executorService.awaitTermination(1, TimeUnit.SECONDS);
64+
executorService.shutdown();
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.concurrent.queue;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.LinkedBlockingQueue;
15+
import java.util.concurrent.TimeUnit;
16+
17+
import org.junit.FixMethodOrder;
18+
import org.junit.Test;
19+
20+
@FixMethodOrder
21+
public class TestLinkedBlockingQueue {
22+
23+
@Test
24+
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
25+
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
26+
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>(elements);
27+
assertThat(linkedBlockingQueue).containsExactly(1, 2, 3, 4, 5);
28+
}
29+
30+
@Test
31+
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenThreadBlocks() throws InterruptedException {
32+
ExecutorService executorService = Executors.newFixedThreadPool(1);
33+
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
34+
executorService.submit(() -> {
35+
try {
36+
linkedBlockingQueue.take();
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
});
41+
TimeUnit.SECONDS.sleep(1);
42+
executorService.awaitTermination(1, TimeUnit.SECONDS);
43+
executorService.shutdown();
44+
}
45+
46+
@Test
47+
public void givenProducerPutsElementInQueue_WhenConsumerAccessQueue_ThenItRetrieve() {
48+
int element = 10;
49+
ExecutorService executorService = Executors.newFixedThreadPool(2);
50+
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
51+
Runnable putTask = () -> {
52+
try {
53+
linkedBlockingQueue.put(element);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
};
58+
59+
Callable<Integer> takeTask = () -> {
60+
try {
61+
return linkedBlockingQueue.take();
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
65+
return null;
66+
};
67+
68+
executorService.submit(putTask);
69+
Future<Integer> returnElement = executorService.submit(takeTask);
70+
try {
71+
TimeUnit.SECONDS.sleep(1);
72+
assertThat(returnElement.get()
73+
.intValue(), is(equalTo(element)));
74+
executorService.awaitTermination(1, TimeUnit.SECONDS);
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
79+
executorService.shutdown();
80+
}
81+
}

0 commit comments

Comments
 (0)