diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java index 73c2aacf8a..e545b9e9f3 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityLinkedListImpl.java @@ -295,14 +295,8 @@ public class PriorityLinkedListImpl implements PriorityLinkedList { lastIter.remove(); - // This next statement would be the equivalent of: - // if (index == highestPriority && levels[index].size() == 0) - // However we have to keep checking all the previous levels - // otherwise we would cache a max that will not exist - // what would make us eventually having hasNext() returning false - // as a bug - // Part of the fix for HORNETQ-705 - for (int i = index; i >= 0 && levels[index].size() == 0; i--) { + // If the last message in the current priority is removed then find the next highest + for (int i = index; i >= 0 && levels[i].size() == 0; i--) { highestPriority = i; } diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/list/PriorityLinkedListTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/list/PriorityLinkedListTest.java index c55e0bf2b8..a7c524ecd7 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/list/PriorityLinkedListTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/list/PriorityLinkedListTest.java @@ -881,6 +881,18 @@ public final class PriorityLinkedListTest { iter.remove(); } + @Test + public void testMixupIterator3() { + list.addTail(b, 4); + list.addTail(c, 9); + LinkedListIterator iter = list.iterator(); + assertTrue(iter.hasNext()); + assertEquals(c, iter.next()); + iter.remove(); + list.addTail(a, 0); + assertTrue(iter.hasNext()); + assertEquals(b, iter.next()); + } @Test public void testPeek() {