From 75860a063e73b98a2a11575cefa52f5ffdbb7a0b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 2 Feb 2017 23:15:23 +0100 Subject: [PATCH 1/2] Added a test class which shows the PriorityBlockingQueue being used --- .../PriorityBlockingQueueUnitTest.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java new file mode 100644 index 0000000000..f417b6b1a8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java @@ -0,0 +1,57 @@ +package com.baeldung.concurrent.priorityblockingqueue; + +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.util.Lists.newArrayList; + +public class PriorityBlockingQueueUnitTest { + + @Test + public void shouldOrderQueue_whenPolling() throws InterruptedException { + PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); + ArrayList polledElements = new ArrayList<>(); + + queue.add(1); + queue.add(5); + queue.add(2); + queue.add(3); + queue.add(4); + + polledElements.add(queue.poll()); + polledElements.add(queue.poll()); + polledElements.add(queue.poll()); + polledElements.add(queue.poll()); + polledElements.add(queue.poll()); + + assertThat(polledElements).containsExactly(1, 2, 3, 4, 5); + } + + @Test + public void shouldBlockThread_WhenPollingEmptyQueue() throws InterruptedException { + PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); + + final Thread thread = new Thread(() -> { + System.out.println("Polling..."); + while (true) { + try { + Integer poll = queue.take(); + System.out.println("Polled: " + poll); + } catch (InterruptedException e) {} + } + }); + thread.start(); + + Thread.sleep(TimeUnit.SECONDS.toMillis(5)); + System.out.println("Adding to queue"); + + queue.addAll(newArrayList(1, 5, 6, 1, 2, 6, 7)); + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + } +} From 318aeb7aa38d310b61745a74e35aa2cd33de6358 Mon Sep 17 00:00:00 2001 From: Predrag Maric Date: Fri, 3 Feb 2017 23:59:34 +0100 Subject: [PATCH 2/2] BAEL-616 Renamed tests --- .../priorityblockingqueue/PriorityBlockingQueueUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java index f417b6b1a8..063e07d859 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java @@ -14,7 +14,7 @@ import static org.assertj.core.util.Lists.newArrayList; public class PriorityBlockingQueueUnitTest { @Test - public void shouldOrderQueue_whenPolling() throws InterruptedException { + public void givenUnorderedValues_whenPolling_thenShouldOrderQueue() throws InterruptedException { PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); ArrayList polledElements = new ArrayList<>(); @@ -34,7 +34,7 @@ public class PriorityBlockingQueueUnitTest { } @Test - public void shouldBlockThread_WhenPollingEmptyQueue() throws InterruptedException { + public void whenPollingEmptyQueue_thenShouldBlockThread() throws InterruptedException { PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); final Thread thread = new Thread(() -> {