Skip to content

Commit 940455b

Browse files
committed
#8414: fix drainTo when head == tail but the queue isn't empty
Signed-off-by: Ludovic Orban <[email protected]>
1 parent a846f4f commit 940455b

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

jetty-util/src/main/java/org/eclipse/jetty/util/BlockingArrayQueue.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,19 @@ public int drainTo(Collection<? super E> c, int maxElements)
487487
_headLock.lock();
488488
try
489489
{
490+
if (_size.get() == 0)
491+
return 0;
492+
490493
final int head = _indexes[HEAD_OFFSET];
491494
final int tail = _indexes[TAIL_OFFSET];
492495
final int capacity = _elements.length;
493496

494497
int i = head;
495-
while (i != tail && elements < maxElements)
498+
while (elements < maxElements)
496499
{
500+
if (i == tail && elements > 0)
501+
break;
502+
497503
elements++;
498504
c.add((E)_elements[i]);
499505
++i;

jetty-util/src/test/java/org/eclipse/jetty/util/BlockingArrayQueueTest.java

+38-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.junit.jupiter.api.Test;
3434

3535
import static org.awaitility.Awaitility.await;
36+
import static org.hamcrest.CoreMatchers.is;
3637
import static org.hamcrest.MatcherAssert.assertThat;
3738
import static org.junit.jupiter.api.Assertions.assertEquals;
3839
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -43,7 +44,7 @@
4344
public class BlockingArrayQueueTest
4445
{
4546
@Test
46-
public void testWrap() throws Exception
47+
public void testWrap()
4748
{
4849
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(3);
4950

@@ -83,7 +84,7 @@ public void testWrap() throws Exception
8384
}
8485

8586
@Test
86-
public void testRemove() throws Exception
87+
public void testRemove()
8788
{
8889
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(3, 3);
8990

@@ -105,7 +106,7 @@ public void testRemove() throws Exception
105106
}
106107

107108
@Test
108-
public void testLimit() throws Exception
109+
public void testLimit()
109110
{
110111
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(1, 0, 1);
111112

@@ -118,7 +119,7 @@ public void testLimit() throws Exception
118119
}
119120

120121
@Test
121-
public void testGrow() throws Exception
122+
public void testGrow()
122123
{
123124
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(3, 2);
124125
assertEquals(3, queue.getCapacity());
@@ -344,7 +345,7 @@ public void testRemoveObjectFromEmptyQueue()
344345
}
345346

346347
@Test
347-
public void testRemoveObjectWithWrappedTail() throws Exception
348+
public void testRemoveObjectWithWrappedTail()
348349
{
349350
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(6);
350351
// Wrap the tail
@@ -366,7 +367,7 @@ public void testRemoveObjectWithWrappedTail() throws Exception
366367
}
367368

368369
@Test
369-
public void testRemoveObject() throws Exception
370+
public void testRemoveObject()
370371
{
371372
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(4, 0, 4);
372373

@@ -400,7 +401,7 @@ public void testRemoveObject() throws Exception
400401
}
401402

402403
@Test
403-
public void testRemoveWithMaxCapacityOne() throws Exception
404+
public void testRemoveWithMaxCapacityOne()
404405
{
405406
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(1);
406407

@@ -413,7 +414,7 @@ public void testRemoveWithMaxCapacityOne() throws Exception
413414
}
414415

415416
@Test
416-
public void testIteratorWithModification() throws Exception
417+
public void testIteratorWithModification()
417418
{
418419
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(4, 0, 4);
419420
int count = queue.getMaxCapacity() - 1;
@@ -435,7 +436,7 @@ public void testIteratorWithModification() throws Exception
435436
}
436437

437438
@Test
438-
public void testListIterator() throws Exception
439+
public void testListIterator()
439440
{
440441
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(4, 0, 4);
441442
String element1 = "A";
@@ -469,7 +470,7 @@ public void testListIterator() throws Exception
469470
}
470471

471472
@Test
472-
public void testListIteratorWithWrappedHead() throws Exception
473+
public void testListIteratorWithWrappedHead()
473474
{
474475
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(4, 0, 4);
475476
// This sequence of offers and polls wraps the head around the array
@@ -509,7 +510,7 @@ public void testListIteratorWithWrappedHead() throws Exception
509510
}
510511

511512
@Test
512-
public void testDrainTo() throws Exception
513+
public void testDrainTo()
513514
{
514515
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>();
515516
queue.add("one");
@@ -530,4 +531,30 @@ public void testDrainTo() throws Exception
530531
assertThat(queue.size(), Matchers.is(0));
531532
assertThat(queue, Matchers.empty());
532533
}
534+
535+
@Test
536+
public void testDrainToAtDefaultGrowthSize()
537+
{
538+
BlockingArrayQueue<Integer> queue = new BlockingArrayQueue<>();
539+
for (int i = 0; i < BlockingArrayQueue.DEFAULT_GROWTH * 2; i++)
540+
{
541+
queue.add(i);
542+
}
543+
544+
List<Integer> list = new ArrayList<>();
545+
assertThat(queue.drainTo(list), is(BlockingArrayQueue.DEFAULT_GROWTH * 2));
546+
assertThat(list.size(), is(BlockingArrayQueue.DEFAULT_GROWTH * 2));
547+
assertThat(queue.size(), is(0));
548+
}
549+
550+
@Test
551+
public void testDrainToAtZeroSize()
552+
{
553+
BlockingArrayQueue<Integer> queue = new BlockingArrayQueue<>();
554+
555+
List<Integer> list = new ArrayList<>();
556+
assertThat(queue.drainTo(list), is(0));
557+
assertThat(list.size(), is(0));
558+
assertThat(queue.size(), is(0));
559+
}
533560
}

0 commit comments

Comments
 (0)