11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
+ |
|
15 |
+ |
public static class Unbounded extends BlockingQueueTest { |
16 |
+ |
protected BlockingQueue emptyCollection() { |
17 |
+ |
return new LinkedBlockingDeque(); |
18 |
+ |
} |
19 |
+ |
} |
20 |
+ |
|
21 |
+ |
public static class Bounded extends BlockingQueueTest { |
22 |
+ |
protected BlockingQueue emptyCollection() { |
23 |
+ |
return new LinkedBlockingDeque(20); |
24 |
+ |
} |
25 |
+ |
} |
26 |
+ |
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run (suite()); |
28 |
> |
junit.textui.TestRunner.run(suite()); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
32 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
32 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
33 |
> |
new Unbounded().testSuite(), |
34 |
> |
new Bounded().testSuite()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
86 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
87 |
|
q.offerFirst(null); |
88 |
|
shouldThrow(); |
89 |
< |
} catch (NullPointerException success) { |
75 |
< |
} |
89 |
> |
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
113 |
|
public void testPollFirst() { |
114 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
115 |
|
for (int i = 0; i < SIZE; ++i) { |
116 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
116 |
> |
assertEquals(i, q.pollFirst()); |
117 |
|
} |
118 |
|
assertNull(q.pollFirst()); |
119 |
|
} |
124 |
|
public void testPollLast() { |
125 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
126 |
|
for (int i = SIZE-1; i >= 0; --i) { |
127 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
127 |
> |
assertEquals(i, q.pollLast()); |
128 |
|
} |
129 |
|
assertNull(q.pollLast()); |
130 |
|
} |
135 |
|
public void testPeekFirst() { |
136 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
137 |
|
for (int i = 0; i < SIZE; ++i) { |
138 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
139 |
< |
q.pollFirst(); |
138 |
> |
assertEquals(i, q.peekFirst()); |
139 |
> |
assertEquals(i, q.pollFirst()); |
140 |
|
assertTrue(q.peekFirst() == null || |
141 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
141 |
> |
!q.peekFirst().equals(i)); |
142 |
|
} |
143 |
|
assertNull(q.peekFirst()); |
144 |
|
} |
149 |
|
public void testPeek() { |
150 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
151 |
|
for (int i = 0; i < SIZE; ++i) { |
152 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
153 |
< |
q.pollFirst(); |
152 |
> |
assertEquals(i, q.peek()); |
153 |
> |
assertEquals(i, q.pollFirst()); |
154 |
|
assertTrue(q.peek() == null || |
155 |
< |
i != ((Integer)q.peek()).intValue()); |
155 |
> |
!q.peek().equals(i)); |
156 |
|
} |
157 |
|
assertNull(q.peek()); |
158 |
|
} |
163 |
|
public void testPeekLast() { |
164 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
165 |
|
for (int i = SIZE-1; i >= 0; --i) { |
166 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
167 |
< |
q.pollLast(); |
166 |
> |
assertEquals(i, q.peekLast()); |
167 |
> |
assertEquals(i, q.pollLast()); |
168 |
|
assertTrue(q.peekLast() == null || |
169 |
< |
i != ((Integer)q.peekLast()).intValue()); |
169 |
> |
!q.peekLast().equals(i)); |
170 |
|
} |
171 |
|
assertNull(q.peekLast()); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
175 |
> |
* getFirst() returns first element, or throws NSEE if empty |
176 |
|
*/ |
177 |
|
public void testFirstElement() { |
178 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
179 |
|
for (int i = 0; i < SIZE; ++i) { |
180 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
181 |
< |
q.pollFirst(); |
180 |
> |
assertEquals(i, q.getFirst()); |
181 |
> |
assertEquals(i, q.pollFirst()); |
182 |
|
} |
183 |
|
try { |
184 |
|
q.getFirst(); |
185 |
|
shouldThrow(); |
186 |
< |
} |
187 |
< |
catch (NoSuchElementException success) {} |
186 |
> |
} catch (NoSuchElementException success) {} |
187 |
> |
assertNull(q.peekFirst()); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* getLast returns next element, or throws NSEE if empty |
191 |
> |
* getLast() returns last element, or throws NSEE if empty |
192 |
|
*/ |
193 |
|
public void testLastElement() { |
194 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
195 |
|
for (int i = SIZE-1; i >= 0; --i) { |
196 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
197 |
< |
q.pollLast(); |
196 |
> |
assertEquals(i, q.getLast()); |
197 |
> |
assertEquals(i, q.pollLast()); |
198 |
|
} |
199 |
|
try { |
200 |
|
q.getLast(); |
201 |
|
shouldThrow(); |
202 |
< |
} |
189 |
< |
catch (NoSuchElementException success) {} |
202 |
> |
} catch (NoSuchElementException success) {} |
203 |
|
assertNull(q.peekLast()); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* removeFirst removes next element, or throws NSEE if empty |
207 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
208 |
|
*/ |
209 |
|
public void testRemoveFirst() { |
210 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
211 |
|
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
212 |
> |
assertEquals(i, q.removeFirst()); |
213 |
|
} |
214 |
|
try { |
215 |
|
q.removeFirst(); |
216 |
|
shouldThrow(); |
217 |
< |
} catch (NoSuchElementException success) { |
217 |
> |
} catch (NoSuchElementException success) {} |
218 |
> |
assertNull(q.peekFirst()); |
219 |
> |
} |
220 |
> |
|
221 |
> |
/** |
222 |
> |
* removeLast() removes last element, or throws NSEE if empty |
223 |
> |
*/ |
224 |
> |
public void testRemoveLast() { |
225 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
226 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
227 |
> |
assertEquals(i, q.removeLast()); |
228 |
|
} |
229 |
+ |
try { |
230 |
+ |
q.removeLast(); |
231 |
+ |
shouldThrow(); |
232 |
+ |
} catch (NoSuchElementException success) {} |
233 |
+ |
assertNull(q.peekLast()); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
239 |
|
public void testRemove() { |
240 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
241 |
|
for (int i = 0; i < SIZE; ++i) { |
242 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
242 |
> |
assertEquals(i, q.remove()); |
243 |
|
} |
244 |
|
try { |
245 |
|
q.remove(); |
246 |
|
shouldThrow(); |
247 |
< |
} catch (NoSuchElementException success) { |
220 |
< |
} |
247 |
> |
} catch (NoSuchElementException success) {} |
248 |
|
} |
249 |
|
|
250 |
|
/** |
284 |
|
LinkedBlockingDeque q = populatedDeque(3); |
285 |
|
q.pollLast(); |
286 |
|
q.addFirst(four); |
287 |
< |
assertEquals(four,q.peekFirst()); |
287 |
> |
assertSame(four, q.peekFirst()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
294 |
|
LinkedBlockingDeque q = populatedDeque(3); |
295 |
|
q.pollLast(); |
296 |
|
q.addLast(four); |
297 |
< |
assertEquals(four,q.peekLast()); |
297 |
> |
assertSame(four, q.peekLast()); |
298 |
|
} |
299 |
|
|
300 |
|
|
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* Constructor throws IAE if capacity argument nonpositive |
311 |
> |
* Constructor throws IAE if capacity argument nonpositive |
312 |
|
*/ |
313 |
|
public void testConstructor2() { |
314 |
|
try { |
315 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
316 |
|
shouldThrow(); |
317 |
< |
} |
291 |
< |
catch (IllegalArgumentException success) {} |
317 |
> |
} catch (IllegalArgumentException success) {} |
318 |
|
} |
319 |
|
|
320 |
|
/** |
324 |
|
try { |
325 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
326 |
|
shouldThrow(); |
327 |
< |
} |
302 |
< |
catch (NullPointerException success) {} |
327 |
> |
} catch (NullPointerException success) {} |
328 |
|
} |
329 |
|
|
330 |
|
/** |
335 |
|
Integer[] ints = new Integer[SIZE]; |
336 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
337 |
|
shouldThrow(); |
338 |
< |
} |
314 |
< |
catch (NullPointerException success) {} |
338 |
> |
} catch (NullPointerException success) {} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
348 |
|
ints[i] = new Integer(i); |
349 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
350 |
|
shouldThrow(); |
351 |
< |
} |
328 |
< |
catch (NullPointerException success) {} |
351 |
> |
} catch (NullPointerException success) {} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
|
* Deque contains all elements of collection used to initialize |
356 |
|
*/ |
357 |
|
public void testConstructor6() { |
358 |
< |
try { |
359 |
< |
Integer[] ints = new Integer[SIZE]; |
360 |
< |
for (int i = 0; i < SIZE; ++i) |
361 |
< |
ints[i] = new Integer(i); |
362 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
363 |
< |
for (int i = 0; i < SIZE; ++i) |
341 |
< |
assertEquals(ints[i], q.poll()); |
342 |
< |
} |
343 |
< |
finally {} |
358 |
> |
Integer[] ints = new Integer[SIZE]; |
359 |
> |
for (int i = 0; i < SIZE; ++i) |
360 |
> |
ints[i] = new Integer(i); |
361 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
362 |
> |
for (int i = 0; i < SIZE; ++i) |
363 |
> |
assertEquals(ints[i], q.poll()); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
403 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
404 |
|
q.offer(null); |
405 |
|
shouldThrow(); |
406 |
< |
} catch (NullPointerException success) { } |
406 |
> |
} catch (NullPointerException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
414 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
415 |
|
q.add(null); |
416 |
|
shouldThrow(); |
417 |
< |
} catch (NullPointerException success) { } |
417 |
> |
} catch (NullPointerException success) {} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
425 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
426 |
|
q.push(null); |
427 |
|
shouldThrow(); |
428 |
< |
} catch (NullPointerException success) { } |
428 |
> |
} catch (NullPointerException success) {} |
429 |
|
} |
430 |
|
|
431 |
|
/** |
441 |
|
} |
442 |
|
assertEquals(0, q.remainingCapacity()); |
443 |
|
q.push(new Integer(SIZE)); |
444 |
< |
} catch (IllegalStateException success) { |
445 |
< |
} |
444 |
> |
shouldThrow(); |
445 |
> |
} catch (IllegalStateException success) {} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
452 |
|
LinkedBlockingDeque q = populatedDeque(3); |
453 |
|
q.pollLast(); |
454 |
|
q.push(four); |
455 |
< |
assertEquals(four,q.peekFirst()); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
|
} |
457 |
|
|
458 |
|
|
462 |
|
public void testPop() { |
463 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
465 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
465 |
> |
assertEquals(i, q.pop()); |
466 |
|
} |
467 |
|
try { |
468 |
|
q.pop(); |
469 |
|
shouldThrow(); |
470 |
< |
} catch (NoSuchElementException success) { |
451 |
< |
} |
470 |
> |
} catch (NoSuchElementException success) {} |
471 |
|
} |
472 |
|
|
473 |
|
|
491 |
|
} |
492 |
|
assertEquals(0, q.remainingCapacity()); |
493 |
|
q.add(new Integer(SIZE)); |
494 |
< |
} catch (IllegalStateException success) { |
495 |
< |
} |
494 |
> |
shouldThrow(); |
495 |
> |
} catch (IllegalStateException success) {} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
503 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
504 |
|
q.addAll(null); |
505 |
|
shouldThrow(); |
506 |
< |
} |
488 |
< |
catch (NullPointerException success) {} |
506 |
> |
} catch (NullPointerException success) {} |
507 |
|
} |
508 |
|
|
509 |
|
/** |
514 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
515 |
|
q.addAll(q); |
516 |
|
shouldThrow(); |
517 |
< |
} |
500 |
< |
catch (IllegalArgumentException success) {} |
517 |
> |
} catch (IllegalArgumentException success) {} |
518 |
|
} |
519 |
|
|
520 |
|
/** |
526 |
|
Integer[] ints = new Integer[SIZE]; |
527 |
|
q.addAll(Arrays.asList(ints)); |
528 |
|
shouldThrow(); |
529 |
< |
} |
513 |
< |
catch (NullPointerException success) {} |
529 |
> |
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
+ |
|
532 |
|
/** |
533 |
|
* addAll of a collection with any null elements throws NPE after |
534 |
|
* possibly adding some elements |
541 |
|
ints[i] = new Integer(i); |
542 |
|
q.addAll(Arrays.asList(ints)); |
543 |
|
shouldThrow(); |
544 |
< |
} |
528 |
< |
catch (NullPointerException success) {} |
544 |
> |
} catch (NullPointerException success) {} |
545 |
|
} |
546 |
+ |
|
547 |
|
/** |
548 |
|
* addAll throws ISE if not enough room |
549 |
|
*/ |
555 |
|
ints[i] = new Integer(i); |
556 |
|
q.addAll(Arrays.asList(ints)); |
557 |
|
shouldThrow(); |
558 |
< |
} |
542 |
< |
catch (IllegalStateException success) {} |
558 |
> |
} catch (IllegalStateException success) {} |
559 |
|
} |
560 |
+ |
|
561 |
|
/** |
562 |
|
* Deque contains all elements, in traversal order, of successful addAll |
563 |
|
*/ |
564 |
|
public void testAddAll5() { |
565 |
< |
try { |
566 |
< |
Integer[] empty = new Integer[0]; |
567 |
< |
Integer[] ints = new Integer[SIZE]; |
568 |
< |
for (int i = 0; i < SIZE; ++i) |
569 |
< |
ints[i] = new Integer(i); |
570 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
571 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
572 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
573 |
< |
for (int i = 0; i < SIZE; ++i) |
557 |
< |
assertEquals(ints[i], q.poll()); |
558 |
< |
} |
559 |
< |
finally {} |
565 |
> |
Integer[] empty = new Integer[0]; |
566 |
> |
Integer[] ints = new Integer[SIZE]; |
567 |
> |
for (int i = 0; i < SIZE; ++i) |
568 |
> |
ints[i] = new Integer(i); |
569 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
571 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
572 |
> |
for (int i = 0; i < SIZE; ++i) |
573 |
> |
assertEquals(ints[i], q.poll()); |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
|
/** |
578 |
|
* put(null) throws NPE |
579 |
|
*/ |
580 |
< |
public void testPutNull() { |
580 |
> |
public void testPutNull() throws InterruptedException { |
581 |
|
try { |
582 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
583 |
|
q.put(null); |
584 |
|
shouldThrow(); |
585 |
< |
} |
586 |
< |
catch (NullPointerException success) { |
573 |
< |
} |
574 |
< |
catch (InterruptedException ie) { |
575 |
< |
unexpectedException(); |
576 |
< |
} |
577 |
< |
} |
585 |
> |
} catch (NullPointerException success) {} |
586 |
> |
} |
587 |
|
|
588 |
|
/** |
589 |
|
* all elements successfully put are contained |
590 |
|
*/ |
591 |
< |
public void testPut() { |
592 |
< |
try { |
593 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
594 |
< |
for (int i = 0; i < SIZE; ++i) { |
595 |
< |
Integer I = new Integer(i); |
596 |
< |
q.put(I); |
588 |
< |
assertTrue(q.contains(I)); |
589 |
< |
} |
590 |
< |
assertEquals(0, q.remainingCapacity()); |
591 |
< |
} |
592 |
< |
catch (InterruptedException ie) { |
593 |
< |
unexpectedException(); |
591 |
> |
public void testPut() throws InterruptedException { |
592 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
593 |
> |
for (int i = 0; i < SIZE; ++i) { |
594 |
> |
Integer I = new Integer(i); |
595 |
> |
q.put(I); |
596 |
> |
assertTrue(q.contains(I)); |
597 |
|
} |
598 |
+ |
assertEquals(0, q.remainingCapacity()); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
|
* put blocks interruptibly if full |
603 |
|
*/ |
604 |
< |
public void testBlockingPut() { |
605 |
< |
Thread t = new Thread(new Runnable() { |
606 |
< |
public void run() { |
607 |
< |
int added = 0; |
608 |
< |
try { |
609 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
610 |
< |
for (int i = 0; i < SIZE; ++i) { |
611 |
< |
q.put(new Integer(i)); |
612 |
< |
++added; |
613 |
< |
} |
614 |
< |
q.put(new Integer(SIZE)); |
615 |
< |
threadShouldThrow(); |
616 |
< |
} catch (InterruptedException ie) { |
617 |
< |
threadAssertEquals(added, SIZE); |
618 |
< |
} |
619 |
< |
}}); |
620 |
< |
t.start(); |
621 |
< |
try { |
622 |
< |
Thread.sleep(SHORT_DELAY_MS); |
623 |
< |
t.interrupt(); |
620 |
< |
t.join(); |
621 |
< |
} |
622 |
< |
catch (InterruptedException ie) { |
623 |
< |
unexpectedException(); |
624 |
< |
} |
604 |
> |
public void testBlockingPut() throws InterruptedException { |
605 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
606 |
> |
Thread t = new Thread(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
for (int i = 0; i < SIZE; ++i) |
609 |
> |
q.put(i); |
610 |
> |
assertEquals(SIZE, q.size()); |
611 |
> |
assertEquals(0, q.remainingCapacity()); |
612 |
> |
try { |
613 |
> |
q.put(99); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (InterruptedException success) {} |
616 |
> |
}}); |
617 |
> |
|
618 |
> |
t.start(); |
619 |
> |
Thread.sleep(SHORT_DELAY_MS); |
620 |
> |
t.interrupt(); |
621 |
> |
t.join(); |
622 |
> |
assertEquals(SIZE, q.size()); |
623 |
> |
assertEquals(0, q.remainingCapacity()); |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* put blocks waiting for take when full |
628 |
|
*/ |
629 |
< |
public void testPutWithTake() { |
630 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
631 |
< |
Thread t = new Thread(new Runnable() { |
632 |
< |
public void run() { |
633 |
< |
int added = 0; |
634 |
< |
try { |
635 |
< |
q.put(new Object()); |
636 |
< |
++added; |
637 |
< |
q.put(new Object()); |
638 |
< |
++added; |
639 |
< |
q.put(new Object()); |
640 |
< |
++added; |
641 |
< |
q.put(new Object()); |
642 |
< |
++added; |
643 |
< |
threadShouldThrow(); |
644 |
< |
} catch (InterruptedException e) { |
645 |
< |
threadAssertTrue(added >= 2); |
646 |
< |
} |
647 |
< |
} |
648 |
< |
}); |
649 |
< |
try { |
651 |
< |
t.start(); |
652 |
< |
Thread.sleep(SHORT_DELAY_MS); |
653 |
< |
q.take(); |
654 |
< |
t.interrupt(); |
655 |
< |
t.join(); |
656 |
< |
} catch (Exception e) { |
657 |
< |
unexpectedException(); |
658 |
< |
} |
629 |
> |
public void testPutWithTake() throws InterruptedException { |
630 |
> |
final int capacity = 2; |
631 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
632 |
> |
Thread t = new Thread(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
for (int i = 0; i < capacity + 1; i++) |
635 |
> |
q.put(i); |
636 |
> |
try { |
637 |
> |
q.put(99); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (InterruptedException success) {} |
640 |
> |
}}); |
641 |
> |
|
642 |
> |
t.start(); |
643 |
> |
Thread.sleep(SHORT_DELAY_MS); |
644 |
> |
assertEquals(q.remainingCapacity(), 0); |
645 |
> |
assertEquals(0, q.take()); |
646 |
> |
Thread.sleep(SHORT_DELAY_MS); |
647 |
> |
t.interrupt(); |
648 |
> |
t.join(); |
649 |
> |
assertEquals(q.remainingCapacity(), 0); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
|
* timed offer times out if full and elements not taken |
654 |
|
*/ |
655 |
< |
public void testTimedOffer() { |
655 |
> |
public void testTimedOffer() throws InterruptedException { |
656 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
657 |
< |
Thread t = new Thread(new Runnable() { |
658 |
< |
public void run() { |
659 |
< |
try { |
660 |
< |
q.put(new Object()); |
661 |
< |
q.put(new Object()); |
662 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
663 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
664 |
< |
threadShouldThrow(); |
665 |
< |
} catch (InterruptedException success) {} |
666 |
< |
} |
676 |
< |
}); |
657 |
> |
Thread t = new Thread(new CheckedRunnable() { |
658 |
> |
public void realRun() throws InterruptedException { |
659 |
> |
q.put(new Object()); |
660 |
> |
q.put(new Object()); |
661 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
662 |
> |
try { |
663 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
664 |
> |
shouldThrow(); |
665 |
> |
} catch (InterruptedException success) {} |
666 |
> |
}}); |
667 |
|
|
668 |
< |
try { |
669 |
< |
t.start(); |
670 |
< |
Thread.sleep(SMALL_DELAY_MS); |
671 |
< |
t.interrupt(); |
682 |
< |
t.join(); |
683 |
< |
} catch (Exception e) { |
684 |
< |
unexpectedException(); |
685 |
< |
} |
668 |
> |
t.start(); |
669 |
> |
Thread.sleep(SMALL_DELAY_MS); |
670 |
> |
t.interrupt(); |
671 |
> |
t.join(); |
672 |
|
} |
673 |
|
|
674 |
|
/** |
675 |
|
* take retrieves elements in FIFO order |
676 |
|
*/ |
677 |
< |
public void testTake() { |
678 |
< |
try { |
679 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
680 |
< |
for (int i = 0; i < SIZE; ++i) { |
695 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
696 |
< |
} |
697 |
< |
} catch (InterruptedException e) { |
698 |
< |
unexpectedException(); |
677 |
> |
public void testTake() throws InterruptedException { |
678 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
679 |
> |
for (int i = 0; i < SIZE; ++i) { |
680 |
> |
assertEquals(i, q.take()); |
681 |
|
} |
682 |
|
} |
683 |
|
|
684 |
|
/** |
685 |
|
* take blocks interruptibly when empty |
686 |
|
*/ |
687 |
< |
public void testTakeFromEmpty() { |
687 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
688 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
689 |
< |
Thread t = new Thread(new Runnable() { |
690 |
< |
public void run() { |
691 |
< |
try { |
692 |
< |
q.take(); |
693 |
< |
threadShouldThrow(); |
694 |
< |
} catch (InterruptedException success) { } |
695 |
< |
} |
696 |
< |
}); |
697 |
< |
try { |
716 |
< |
t.start(); |
717 |
< |
Thread.sleep(SHORT_DELAY_MS); |
718 |
< |
t.interrupt(); |
719 |
< |
t.join(); |
720 |
< |
} catch (Exception e) { |
721 |
< |
unexpectedException(); |
722 |
< |
} |
689 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
690 |
> |
public void realRun() throws InterruptedException { |
691 |
> |
q.take(); |
692 |
> |
}}; |
693 |
> |
|
694 |
> |
t.start(); |
695 |
> |
Thread.sleep(SHORT_DELAY_MS); |
696 |
> |
t.interrupt(); |
697 |
> |
t.join(); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
|
* Take removes existing elements until empty, then blocks interruptibly |
702 |
|
*/ |
703 |
< |
public void testBlockingTake() { |
704 |
< |
Thread t = new Thread(new Runnable() { |
705 |
< |
public void run() { |
706 |
< |
try { |
707 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
708 |
< |
for (int i = 0; i < SIZE; ++i) { |
709 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
710 |
< |
} |
711 |
< |
q.take(); |
712 |
< |
threadShouldThrow(); |
713 |
< |
} catch (InterruptedException success) { |
714 |
< |
} |
715 |
< |
}}); |
716 |
< |
t.start(); |
717 |
< |
try { |
718 |
< |
Thread.sleep(SHORT_DELAY_MS); |
719 |
< |
t.interrupt(); |
745 |
< |
t.join(); |
746 |
< |
} |
747 |
< |
catch (InterruptedException ie) { |
748 |
< |
unexpectedException(); |
749 |
< |
} |
703 |
> |
public void testBlockingTake() throws InterruptedException { |
704 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
705 |
> |
Thread t = new Thread(new CheckedRunnable() { |
706 |
> |
public void realRun() throws InterruptedException { |
707 |
> |
for (int i = 0; i < SIZE; ++i) { |
708 |
> |
assertEquals(i, q.take()); |
709 |
> |
} |
710 |
> |
try { |
711 |
> |
q.take(); |
712 |
> |
shouldThrow(); |
713 |
> |
} catch (InterruptedException success) {} |
714 |
> |
}}); |
715 |
> |
|
716 |
> |
t.start(); |
717 |
> |
Thread.sleep(SHORT_DELAY_MS); |
718 |
> |
t.interrupt(); |
719 |
> |
t.join(); |
720 |
|
} |
721 |
|
|
722 |
|
|
726 |
|
public void testPoll() { |
727 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
728 |
|
for (int i = 0; i < SIZE; ++i) { |
729 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
729 |
> |
assertEquals(i, q.poll()); |
730 |
|
} |
731 |
|
assertNull(q.poll()); |
732 |
|
} |
734 |
|
/** |
735 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
736 |
|
*/ |
737 |
< |
public void testTimedPoll0() { |
738 |
< |
try { |
739 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
740 |
< |
for (int i = 0; i < SIZE; ++i) { |
771 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
772 |
< |
} |
773 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
774 |
< |
} catch (InterruptedException e) { |
775 |
< |
unexpectedException(); |
737 |
> |
public void testTimedPoll0() throws InterruptedException { |
738 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
739 |
> |
for (int i = 0; i < SIZE; ++i) { |
740 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
741 |
|
} |
742 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
743 |
|
} |
744 |
|
|
745 |
|
/** |
746 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
747 |
|
*/ |
748 |
< |
public void testTimedPoll() { |
749 |
< |
try { |
750 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
751 |
< |
for (int i = 0; i < SIZE; ++i) { |
786 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
787 |
< |
} |
788 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
789 |
< |
} catch (InterruptedException e) { |
790 |
< |
unexpectedException(); |
748 |
> |
public void testTimedPoll() throws InterruptedException { |
749 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
750 |
> |
for (int i = 0; i < SIZE; ++i) { |
751 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
752 |
|
} |
753 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
|
* Interrupted timed poll throws InterruptedException instead of |
758 |
|
* returning timeout status |
759 |
|
*/ |
760 |
< |
public void testInterruptedTimedPoll() { |
761 |
< |
Thread t = new Thread(new Runnable() { |
762 |
< |
public void run() { |
763 |
< |
try { |
764 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
765 |
< |
for (int i = 0; i < SIZE; ++i) { |
804 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
805 |
< |
} |
806 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
807 |
< |
} catch (InterruptedException success) { |
808 |
< |
} |
809 |
< |
}}); |
810 |
< |
t.start(); |
811 |
< |
try { |
812 |
< |
Thread.sleep(SHORT_DELAY_MS); |
813 |
< |
t.interrupt(); |
814 |
< |
t.join(); |
815 |
< |
} |
816 |
< |
catch (InterruptedException ie) { |
817 |
< |
unexpectedException(); |
818 |
< |
} |
819 |
< |
} |
820 |
< |
|
821 |
< |
/** |
822 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
823 |
< |
* on interruption throws |
824 |
< |
*/ |
825 |
< |
public void testTimedPollWithOffer() { |
826 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
827 |
< |
Thread t = new Thread(new Runnable() { |
828 |
< |
public void run() { |
829 |
< |
try { |
830 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
831 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
832 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
833 |
< |
threadShouldThrow(); |
834 |
< |
} catch (InterruptedException success) { } |
760 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
761 |
> |
Thread t = new Thread(new CheckedRunnable() { |
762 |
> |
public void realRun() throws InterruptedException { |
763 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
764 |
> |
for (int i = 0; i < SIZE; ++i) { |
765 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
766 |
|
} |
767 |
< |
}); |
768 |
< |
try { |
769 |
< |
t.start(); |
770 |
< |
Thread.sleep(SMALL_DELAY_MS); |
771 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
841 |
< |
t.interrupt(); |
842 |
< |
t.join(); |
843 |
< |
} catch (Exception e) { |
844 |
< |
unexpectedException(); |
845 |
< |
} |
846 |
< |
} |
767 |
> |
try { |
768 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
769 |
> |
shouldThrow(); |
770 |
> |
} catch (InterruptedException success) {} |
771 |
> |
}}); |
772 |
|
|
773 |
+ |
t.start(); |
774 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
775 |
+ |
t.interrupt(); |
776 |
+ |
t.join(); |
777 |
+ |
} |
778 |
|
|
779 |
|
/** |
780 |
|
* putFirst(null) throws NPE |
781 |
|
*/ |
782 |
< |
public void testPutFirstNull() { |
782 |
> |
public void testPutFirstNull() throws InterruptedException { |
783 |
|
try { |
784 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
785 |
|
q.putFirst(null); |
786 |
|
shouldThrow(); |
787 |
< |
} |
858 |
< |
catch (NullPointerException success) { |
859 |
< |
} |
860 |
< |
catch (InterruptedException ie) { |
861 |
< |
unexpectedException(); |
862 |
< |
} |
787 |
> |
} catch (NullPointerException success) {} |
788 |
|
} |
789 |
|
|
790 |
|
/** |
791 |
|
* all elements successfully putFirst are contained |
792 |
|
*/ |
793 |
< |
public void testPutFirst() { |
794 |
< |
try { |
795 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
796 |
< |
for (int i = 0; i < SIZE; ++i) { |
797 |
< |
Integer I = new Integer(i); |
798 |
< |
q.putFirst(I); |
874 |
< |
assertTrue(q.contains(I)); |
875 |
< |
} |
876 |
< |
assertEquals(0, q.remainingCapacity()); |
793 |
> |
public void testPutFirst() throws InterruptedException { |
794 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
795 |
> |
for (int i = 0; i < SIZE; ++i) { |
796 |
> |
Integer I = new Integer(i); |
797 |
> |
q.putFirst(I); |
798 |
> |
assertTrue(q.contains(I)); |
799 |
|
} |
800 |
< |
catch (InterruptedException ie) { |
879 |
< |
unexpectedException(); |
880 |
< |
} |
800 |
> |
assertEquals(0, q.remainingCapacity()); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* putFirst blocks interruptibly if full |
805 |
|
*/ |
806 |
< |
public void testBlockingPutFirst() { |
807 |
< |
Thread t = new Thread(new Runnable() { |
808 |
< |
public void run() { |
809 |
< |
int added = 0; |
810 |
< |
try { |
811 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
812 |
< |
for (int i = 0; i < SIZE; ++i) { |
813 |
< |
q.putFirst(new Integer(i)); |
814 |
< |
++added; |
815 |
< |
} |
816 |
< |
q.putFirst(new Integer(SIZE)); |
817 |
< |
threadShouldThrow(); |
818 |
< |
} catch (InterruptedException ie) { |
819 |
< |
threadAssertEquals(added, SIZE); |
820 |
< |
} |
821 |
< |
}}); |
822 |
< |
t.start(); |
823 |
< |
try { |
824 |
< |
Thread.sleep(SHORT_DELAY_MS); |
825 |
< |
t.interrupt(); |
906 |
< |
t.join(); |
907 |
< |
} |
908 |
< |
catch (InterruptedException ie) { |
909 |
< |
unexpectedException(); |
910 |
< |
} |
806 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
807 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
808 |
> |
Thread t = new Thread(new CheckedRunnable() { |
809 |
> |
public void realRun() throws InterruptedException { |
810 |
> |
for (int i = 0; i < SIZE; ++i) |
811 |
> |
q.putFirst(i); |
812 |
> |
assertEquals(SIZE, q.size()); |
813 |
> |
assertEquals(0, q.remainingCapacity()); |
814 |
> |
try { |
815 |
> |
q.putFirst(99); |
816 |
> |
shouldThrow(); |
817 |
> |
} catch (InterruptedException success) {} |
818 |
> |
}}); |
819 |
> |
|
820 |
> |
t.start(); |
821 |
> |
Thread.sleep(SHORT_DELAY_MS); |
822 |
> |
t.interrupt(); |
823 |
> |
t.join(); |
824 |
> |
assertEquals(SIZE, q.size()); |
825 |
> |
assertEquals(0, q.remainingCapacity()); |
826 |
|
} |
827 |
|
|
828 |
|
/** |
829 |
|
* putFirst blocks waiting for take when full |
830 |
|
*/ |
831 |
< |
public void testPutFirstWithTake() { |
832 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
833 |
< |
Thread t = new Thread(new Runnable() { |
834 |
< |
public void run() { |
835 |
< |
int added = 0; |
836 |
< |
try { |
837 |
< |
q.putFirst(new Object()); |
838 |
< |
++added; |
839 |
< |
q.putFirst(new Object()); |
840 |
< |
++added; |
841 |
< |
q.putFirst(new Object()); |
842 |
< |
++added; |
843 |
< |
q.putFirst(new Object()); |
844 |
< |
++added; |
845 |
< |
threadShouldThrow(); |
846 |
< |
} catch (InterruptedException e) { |
847 |
< |
threadAssertTrue(added >= 2); |
848 |
< |
} |
849 |
< |
} |
850 |
< |
}); |
851 |
< |
try { |
937 |
< |
t.start(); |
938 |
< |
Thread.sleep(SHORT_DELAY_MS); |
939 |
< |
q.take(); |
940 |
< |
t.interrupt(); |
941 |
< |
t.join(); |
942 |
< |
} catch (Exception e) { |
943 |
< |
unexpectedException(); |
944 |
< |
} |
831 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
832 |
> |
final int capacity = 2; |
833 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
834 |
> |
Thread t = new Thread(new CheckedRunnable() { |
835 |
> |
public void realRun() throws InterruptedException { |
836 |
> |
for (int i = 0; i < capacity + 1; i++) |
837 |
> |
q.putFirst(i); |
838 |
> |
try { |
839 |
> |
q.putFirst(99); |
840 |
> |
shouldThrow(); |
841 |
> |
} catch (InterruptedException success) {} |
842 |
> |
}}); |
843 |
> |
|
844 |
> |
t.start(); |
845 |
> |
Thread.sleep(SHORT_DELAY_MS); |
846 |
> |
assertEquals(q.remainingCapacity(), 0); |
847 |
> |
assertEquals(capacity - 1, q.take()); |
848 |
> |
Thread.sleep(SHORT_DELAY_MS); |
849 |
> |
t.interrupt(); |
850 |
> |
t.join(); |
851 |
> |
assertEquals(q.remainingCapacity(), 0); |
852 |
|
} |
853 |
|
|
854 |
|
/** |
855 |
|
* timed offerFirst times out if full and elements not taken |
856 |
|
*/ |
857 |
< |
public void testTimedOfferFirst() { |
857 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
858 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
859 |
< |
Thread t = new Thread(new Runnable() { |
860 |
< |
public void run() { |
861 |
< |
try { |
862 |
< |
q.putFirst(new Object()); |
863 |
< |
q.putFirst(new Object()); |
864 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
865 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
866 |
< |
threadShouldThrow(); |
867 |
< |
} catch (InterruptedException success) {} |
868 |
< |
} |
962 |
< |
}); |
859 |
> |
Thread t = new Thread(new CheckedRunnable() { |
860 |
> |
public void realRun() throws InterruptedException { |
861 |
> |
q.putFirst(new Object()); |
862 |
> |
q.putFirst(new Object()); |
863 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
864 |
> |
try { |
865 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
866 |
> |
shouldThrow(); |
867 |
> |
} catch (InterruptedException success) {} |
868 |
> |
}}); |
869 |
|
|
870 |
< |
try { |
871 |
< |
t.start(); |
872 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
< |
t.interrupt(); |
968 |
< |
t.join(); |
969 |
< |
} catch (Exception e) { |
970 |
< |
unexpectedException(); |
971 |
< |
} |
870 |
> |
t.start(); |
871 |
> |
Thread.sleep(SMALL_DELAY_MS); |
872 |
> |
t.interrupt(); |
873 |
> |
t.join(); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
|
* take retrieves elements in FIFO order |
878 |
|
*/ |
879 |
< |
public void testTakeFirst() { |
880 |
< |
try { |
881 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
882 |
< |
for (int i = 0; i < SIZE; ++i) { |
981 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
982 |
< |
} |
983 |
< |
} catch (InterruptedException e) { |
984 |
< |
unexpectedException(); |
879 |
> |
public void testTakeFirst() throws InterruptedException { |
880 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
881 |
> |
for (int i = 0; i < SIZE; ++i) { |
882 |
> |
assertEquals(i, q.takeFirst()); |
883 |
|
} |
884 |
|
} |
885 |
|
|
886 |
|
/** |
887 |
|
* takeFirst blocks interruptibly when empty |
888 |
|
*/ |
889 |
< |
public void testTakeFirstFromEmpty() { |
889 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
890 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
891 |
< |
Thread t = new Thread(new Runnable() { |
892 |
< |
public void run() { |
893 |
< |
try { |
894 |
< |
q.takeFirst(); |
895 |
< |
threadShouldThrow(); |
896 |
< |
} catch (InterruptedException success) { } |
897 |
< |
} |
898 |
< |
}); |
899 |
< |
try { |
1002 |
< |
t.start(); |
1003 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1004 |
< |
t.interrupt(); |
1005 |
< |
t.join(); |
1006 |
< |
} catch (Exception e) { |
1007 |
< |
unexpectedException(); |
1008 |
< |
} |
891 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
892 |
> |
public void realRun() throws InterruptedException { |
893 |
> |
q.takeFirst(); |
894 |
> |
}}; |
895 |
> |
|
896 |
> |
t.start(); |
897 |
> |
Thread.sleep(SHORT_DELAY_MS); |
898 |
> |
t.interrupt(); |
899 |
> |
t.join(); |
900 |
|
} |
901 |
|
|
902 |
|
/** |
903 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
904 |
|
*/ |
905 |
< |
public void testBlockingTakeFirst() { |
906 |
< |
Thread t = new Thread(new Runnable() { |
907 |
< |
public void run() { |
908 |
< |
try { |
909 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
910 |
< |
for (int i = 0; i < SIZE; ++i) { |
911 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
912 |
< |
} |
913 |
< |
q.takeFirst(); |
914 |
< |
threadShouldThrow(); |
915 |
< |
} catch (InterruptedException success) { |
916 |
< |
} |
917 |
< |
}}); |
918 |
< |
t.start(); |
919 |
< |
try { |
920 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1030 |
< |
t.interrupt(); |
1031 |
< |
t.join(); |
1032 |
< |
} |
1033 |
< |
catch (InterruptedException ie) { |
1034 |
< |
unexpectedException(); |
1035 |
< |
} |
905 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
906 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
907 |
> |
Thread t = new Thread(new CheckedRunnable() { |
908 |
> |
public void realRun() throws InterruptedException { |
909 |
> |
for (int i = 0; i < SIZE; ++i) |
910 |
> |
assertEquals(i, q.takeFirst()); |
911 |
> |
try { |
912 |
> |
q.takeFirst(); |
913 |
> |
shouldThrow(); |
914 |
> |
} catch (InterruptedException success) {} |
915 |
> |
}}); |
916 |
> |
|
917 |
> |
t.start(); |
918 |
> |
Thread.sleep(SHORT_DELAY_MS); |
919 |
> |
t.interrupt(); |
920 |
> |
t.join(); |
921 |
|
} |
922 |
|
|
923 |
|
|
924 |
|
/** |
925 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
926 |
|
*/ |
927 |
< |
public void testTimedPollFirst0() { |
928 |
< |
try { |
929 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
930 |
< |
for (int i = 0; i < SIZE; ++i) { |
1046 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
1047 |
< |
} |
1048 |
< |
assertNull(q.pollFirst(0, MILLISECONDS)); |
1049 |
< |
} catch (InterruptedException e) { |
1050 |
< |
unexpectedException(); |
927 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
928 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
929 |
> |
for (int i = 0; i < SIZE; ++i) { |
930 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
931 |
|
} |
932 |
+ |
assertNull(q.pollFirst(0, MILLISECONDS)); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
936 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
937 |
|
*/ |
938 |
< |
public void testTimedPollFirst() { |
939 |
< |
try { |
940 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
941 |
< |
for (int i = 0; i < SIZE; ++i) { |
1061 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1062 |
< |
} |
1063 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1064 |
< |
} catch (InterruptedException e) { |
1065 |
< |
unexpectedException(); |
938 |
> |
public void testTimedPollFirst() throws InterruptedException { |
939 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
940 |
> |
for (int i = 0; i < SIZE; ++i) { |
941 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
942 |
|
} |
943 |
+ |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
948 |
|
* returning timeout status |
949 |
|
*/ |
950 |
< |
public void testInterruptedTimedPollFirst() { |
951 |
< |
Thread t = new Thread(new Runnable() { |
952 |
< |
public void run() { |
953 |
< |
try { |
954 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
955 |
< |
for (int i = 0; i < SIZE; ++i) { |
956 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
957 |
< |
} |
958 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
959 |
< |
} catch (InterruptedException success) { |
960 |
< |
} |
961 |
< |
}}); |
962 |
< |
t.start(); |
963 |
< |
try { |
964 |
< |
Thread.sleep(SHORT_DELAY_MS); |
965 |
< |
t.interrupt(); |
966 |
< |
t.join(); |
1090 |
< |
} |
1091 |
< |
catch (InterruptedException ie) { |
1092 |
< |
unexpectedException(); |
1093 |
< |
} |
950 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
951 |
> |
Thread t = new Thread(new CheckedRunnable() { |
952 |
> |
public void realRun() throws InterruptedException { |
953 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
954 |
> |
for (int i = 0; i < SIZE; ++i) { |
955 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
956 |
> |
} |
957 |
> |
try { |
958 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
959 |
> |
shouldThrow(); |
960 |
> |
} catch (InterruptedException success) {} |
961 |
> |
}}); |
962 |
> |
|
963 |
> |
t.start(); |
964 |
> |
Thread.sleep(SHORT_DELAY_MS); |
965 |
> |
t.interrupt(); |
966 |
> |
t.join(); |
967 |
|
} |
968 |
|
|
969 |
|
/** |
970 |
|
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
971 |
|
* on interruption throws |
972 |
|
*/ |
973 |
< |
public void testTimedPollFirstWithOfferFirst() { |
973 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
974 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
975 |
< |
Thread t = new Thread(new Runnable() { |
976 |
< |
public void run() { |
977 |
< |
try { |
978 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
979 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
980 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
981 |
< |
threadShouldThrow(); |
982 |
< |
} catch (InterruptedException success) { } |
983 |
< |
} |
984 |
< |
}); |
985 |
< |
try { |
986 |
< |
t.start(); |
987 |
< |
Thread.sleep(SMALL_DELAY_MS); |
988 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
989 |
< |
t.interrupt(); |
1117 |
< |
t.join(); |
1118 |
< |
} catch (Exception e) { |
1119 |
< |
unexpectedException(); |
1120 |
< |
} |
975 |
> |
Thread t = new Thread(new CheckedRunnable() { |
976 |
> |
public void realRun() throws InterruptedException { |
977 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
978 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
979 |
> |
try { |
980 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
981 |
> |
shouldThrow(); |
982 |
> |
} catch (InterruptedException success) {} |
983 |
> |
}}); |
984 |
> |
|
985 |
> |
t.start(); |
986 |
> |
Thread.sleep(SMALL_DELAY_MS); |
987 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
988 |
> |
t.interrupt(); |
989 |
> |
t.join(); |
990 |
|
} |
991 |
|
|
992 |
|
/** |
993 |
|
* putLast(null) throws NPE |
994 |
|
*/ |
995 |
< |
public void testPutLastNull() { |
995 |
> |
public void testPutLastNull() throws InterruptedException { |
996 |
|
try { |
997 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
998 |
|
q.putLast(null); |
999 |
|
shouldThrow(); |
1000 |
< |
} |
1132 |
< |
catch (NullPointerException success) { |
1133 |
< |
} |
1134 |
< |
catch (InterruptedException ie) { |
1135 |
< |
unexpectedException(); |
1136 |
< |
} |
1000 |
> |
} catch (NullPointerException success) {} |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
|
* all elements successfully putLast are contained |
1005 |
|
*/ |
1006 |
< |
public void testPutLast() { |
1007 |
< |
try { |
1008 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1009 |
< |
for (int i = 0; i < SIZE; ++i) { |
1010 |
< |
Integer I = new Integer(i); |
1011 |
< |
q.putLast(I); |
1148 |
< |
assertTrue(q.contains(I)); |
1149 |
< |
} |
1150 |
< |
assertEquals(0, q.remainingCapacity()); |
1006 |
> |
public void testPutLast() throws InterruptedException { |
1007 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1008 |
> |
for (int i = 0; i < SIZE; ++i) { |
1009 |
> |
Integer I = new Integer(i); |
1010 |
> |
q.putLast(I); |
1011 |
> |
assertTrue(q.contains(I)); |
1012 |
|
} |
1013 |
< |
catch (InterruptedException ie) { |
1153 |
< |
unexpectedException(); |
1154 |
< |
} |
1013 |
> |
assertEquals(0, q.remainingCapacity()); |
1014 |
|
} |
1015 |
|
|
1016 |
|
/** |
1017 |
|
* putLast blocks interruptibly if full |
1018 |
|
*/ |
1019 |
< |
public void testBlockingPutLast() { |
1020 |
< |
Thread t = new Thread(new Runnable() { |
1021 |
< |
public void run() { |
1022 |
< |
int added = 0; |
1023 |
< |
try { |
1024 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1025 |
< |
for (int i = 0; i < SIZE; ++i) { |
1026 |
< |
q.putLast(new Integer(i)); |
1027 |
< |
++added; |
1028 |
< |
} |
1029 |
< |
q.putLast(new Integer(SIZE)); |
1030 |
< |
threadShouldThrow(); |
1031 |
< |
} catch (InterruptedException ie) { |
1032 |
< |
threadAssertEquals(added, SIZE); |
1033 |
< |
} |
1034 |
< |
}}); |
1035 |
< |
t.start(); |
1036 |
< |
try { |
1037 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1038 |
< |
t.interrupt(); |
1180 |
< |
t.join(); |
1181 |
< |
} |
1182 |
< |
catch (InterruptedException ie) { |
1183 |
< |
unexpectedException(); |
1184 |
< |
} |
1019 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1020 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1021 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1022 |
> |
public void realRun() throws InterruptedException { |
1023 |
> |
for (int i = 0; i < SIZE; ++i) |
1024 |
> |
q.putLast(i); |
1025 |
> |
assertEquals(SIZE, q.size()); |
1026 |
> |
assertEquals(0, q.remainingCapacity()); |
1027 |
> |
try { |
1028 |
> |
q.putLast(99); |
1029 |
> |
shouldThrow(); |
1030 |
> |
} catch (InterruptedException success) {} |
1031 |
> |
}}); |
1032 |
> |
|
1033 |
> |
t.start(); |
1034 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1035 |
> |
t.interrupt(); |
1036 |
> |
t.join(); |
1037 |
> |
assertEquals(SIZE, q.size()); |
1038 |
> |
assertEquals(0, q.remainingCapacity()); |
1039 |
|
} |
1040 |
|
|
1041 |
|
/** |
1042 |
|
* putLast blocks waiting for take when full |
1043 |
|
*/ |
1044 |
< |
public void testPutLastWithTake() { |
1045 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1046 |
< |
Thread t = new Thread(new Runnable() { |
1047 |
< |
public void run() { |
1048 |
< |
int added = 0; |
1049 |
< |
try { |
1050 |
< |
q.putLast(new Object()); |
1051 |
< |
++added; |
1052 |
< |
q.putLast(new Object()); |
1053 |
< |
++added; |
1054 |
< |
q.putLast(new Object()); |
1055 |
< |
++added; |
1056 |
< |
q.putLast(new Object()); |
1057 |
< |
++added; |
1058 |
< |
threadShouldThrow(); |
1059 |
< |
} catch (InterruptedException e) { |
1060 |
< |
threadAssertTrue(added >= 2); |
1061 |
< |
} |
1062 |
< |
} |
1063 |
< |
}); |
1064 |
< |
try { |
1211 |
< |
t.start(); |
1212 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1213 |
< |
q.take(); |
1214 |
< |
t.interrupt(); |
1215 |
< |
t.join(); |
1216 |
< |
} catch (Exception e) { |
1217 |
< |
unexpectedException(); |
1218 |
< |
} |
1044 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1045 |
> |
final int capacity = 2; |
1046 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1047 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1048 |
> |
public void realRun() throws InterruptedException { |
1049 |
> |
for (int i = 0; i < capacity + 1; i++) |
1050 |
> |
q.putLast(i); |
1051 |
> |
try { |
1052 |
> |
q.putLast(99); |
1053 |
> |
shouldThrow(); |
1054 |
> |
} catch (InterruptedException success) {} |
1055 |
> |
}}); |
1056 |
> |
|
1057 |
> |
t.start(); |
1058 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1059 |
> |
assertEquals(q.remainingCapacity(), 0); |
1060 |
> |
assertEquals(0, q.take()); |
1061 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1062 |
> |
t.interrupt(); |
1063 |
> |
t.join(); |
1064 |
> |
assertEquals(q.remainingCapacity(), 0); |
1065 |
|
} |
1066 |
|
|
1067 |
|
/** |
1068 |
|
* timed offerLast times out if full and elements not taken |
1069 |
|
*/ |
1070 |
< |
public void testTimedOfferLast() { |
1070 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1071 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1072 |
< |
Thread t = new Thread(new Runnable() { |
1073 |
< |
public void run() { |
1074 |
< |
try { |
1075 |
< |
q.putLast(new Object()); |
1076 |
< |
q.putLast(new Object()); |
1077 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1078 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1079 |
< |
threadShouldThrow(); |
1080 |
< |
} catch (InterruptedException success) {} |
1081 |
< |
} |
1236 |
< |
}); |
1072 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1073 |
> |
public void realRun() throws InterruptedException { |
1074 |
> |
q.putLast(new Object()); |
1075 |
> |
q.putLast(new Object()); |
1076 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1077 |
> |
try { |
1078 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1079 |
> |
shouldThrow(); |
1080 |
> |
} catch (InterruptedException success) {} |
1081 |
> |
}}); |
1082 |
|
|
1083 |
< |
try { |
1084 |
< |
t.start(); |
1085 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1086 |
< |
t.interrupt(); |
1242 |
< |
t.join(); |
1243 |
< |
} catch (Exception e) { |
1244 |
< |
unexpectedException(); |
1245 |
< |
} |
1083 |
> |
t.start(); |
1084 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1085 |
> |
t.interrupt(); |
1086 |
> |
t.join(); |
1087 |
|
} |
1088 |
|
|
1089 |
|
/** |
1090 |
|
* takeLast retrieves elements in FIFO order |
1091 |
|
*/ |
1092 |
< |
public void testTakeLast() { |
1093 |
< |
try { |
1094 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1095 |
< |
for (int i = 0; i < SIZE; ++i) { |
1255 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1256 |
< |
} |
1257 |
< |
} catch (InterruptedException e) { |
1258 |
< |
unexpectedException(); |
1092 |
> |
public void testTakeLast() throws InterruptedException { |
1093 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1094 |
> |
for (int i = 0; i < SIZE; ++i) { |
1095 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1099 |
|
/** |
1100 |
|
* takeLast blocks interruptibly when empty |
1101 |
|
*/ |
1102 |
< |
public void testTakeLastFromEmpty() { |
1102 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1103 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1104 |
< |
Thread t = new Thread(new Runnable() { |
1105 |
< |
public void run() { |
1106 |
< |
try { |
1107 |
< |
q.takeLast(); |
1108 |
< |
threadShouldThrow(); |
1109 |
< |
} catch (InterruptedException success) { } |
1110 |
< |
} |
1111 |
< |
}); |
1112 |
< |
try { |
1276 |
< |
t.start(); |
1277 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1278 |
< |
t.interrupt(); |
1279 |
< |
t.join(); |
1280 |
< |
} catch (Exception e) { |
1281 |
< |
unexpectedException(); |
1282 |
< |
} |
1104 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1105 |
> |
public void realRun() throws InterruptedException { |
1106 |
> |
q.takeLast(); |
1107 |
> |
}}; |
1108 |
> |
|
1109 |
> |
t.start(); |
1110 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1111 |
> |
t.interrupt(); |
1112 |
> |
t.join(); |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1116 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1117 |
|
*/ |
1118 |
< |
public void testBlockingTakeLast() { |
1119 |
< |
Thread t = new Thread(new Runnable() { |
1120 |
< |
public void run() { |
1121 |
< |
try { |
1122 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1123 |
< |
for (int i = 0; i < SIZE; ++i) { |
1124 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1125 |
< |
} |
1126 |
< |
q.takeLast(); |
1127 |
< |
threadShouldThrow(); |
1128 |
< |
} catch (InterruptedException success) { |
1299 |
< |
} |
1300 |
< |
}}); |
1301 |
< |
t.start(); |
1302 |
< |
try { |
1303 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1304 |
< |
t.interrupt(); |
1305 |
< |
t.join(); |
1306 |
< |
} |
1307 |
< |
catch (InterruptedException ie) { |
1308 |
< |
unexpectedException(); |
1309 |
< |
} |
1310 |
< |
} |
1118 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1119 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1120 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1121 |
> |
public void realRun() throws InterruptedException { |
1122 |
> |
for (int i = 0; i < SIZE; ++i) |
1123 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1124 |
> |
try { |
1125 |
> |
q.takeLast(); |
1126 |
> |
shouldThrow(); |
1127 |
> |
} catch (InterruptedException success) {} |
1128 |
> |
}}); |
1129 |
|
|
1130 |
+ |
t.start(); |
1131 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1132 |
+ |
t.interrupt(); |
1133 |
+ |
t.join(); |
1134 |
+ |
} |
1135 |
|
|
1136 |
|
/** |
1137 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1138 |
|
*/ |
1139 |
< |
public void testTimedPollLast0() { |
1140 |
< |
try { |
1141 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1142 |
< |
for (int i = 0; i < SIZE; ++i) { |
1320 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1321 |
< |
} |
1322 |
< |
assertNull(q.pollLast(0, MILLISECONDS)); |
1323 |
< |
} catch (InterruptedException e) { |
1324 |
< |
unexpectedException(); |
1139 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1140 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1141 |
> |
for (int i = 0; i < SIZE; ++i) { |
1142 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1143 |
|
} |
1144 |
+ |
assertNull(q.pollLast(0, MILLISECONDS)); |
1145 |
|
} |
1146 |
|
|
1147 |
|
/** |
1148 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1149 |
|
*/ |
1150 |
< |
public void testTimedPollLast() { |
1151 |
< |
try { |
1152 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1153 |
< |
for (int i = 0; i < SIZE; ++i) { |
1335 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1336 |
< |
} |
1337 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1338 |
< |
} catch (InterruptedException e) { |
1339 |
< |
unexpectedException(); |
1150 |
> |
public void testTimedPollLast() throws InterruptedException { |
1151 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1152 |
> |
for (int i = 0; i < SIZE; ++i) { |
1153 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1154 |
|
} |
1155 |
+ |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1156 |
|
} |
1157 |
|
|
1158 |
|
/** |
1159 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1160 |
|
* returning timeout status |
1161 |
|
*/ |
1162 |
< |
public void testInterruptedTimedPollLast() { |
1163 |
< |
Thread t = new Thread(new Runnable() { |
1164 |
< |
public void run() { |
1165 |
< |
try { |
1166 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1167 |
< |
for (int i = 0; i < SIZE; ++i) { |
1168 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1169 |
< |
} |
1170 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1171 |
< |
} catch (InterruptedException success) { |
1172 |
< |
} |
1173 |
< |
}}); |
1162 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1163 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1164 |
> |
public void realRun() throws InterruptedException { |
1165 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1166 |
> |
for (int i = 0; i < SIZE; ++i) { |
1167 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1168 |
> |
} |
1169 |
> |
try { |
1170 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1171 |
> |
shouldThrow(); |
1172 |
> |
} catch (InterruptedException success) {} |
1173 |
> |
}}); |
1174 |
> |
|
1175 |
|
t.start(); |
1176 |
< |
try { |
1177 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1178 |
< |
t.interrupt(); |
1363 |
< |
t.join(); |
1364 |
< |
} |
1365 |
< |
catch (InterruptedException ie) { |
1366 |
< |
unexpectedException(); |
1367 |
< |
} |
1176 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1177 |
> |
t.interrupt(); |
1178 |
> |
t.join(); |
1179 |
|
} |
1180 |
|
|
1181 |
|
/** |
1182 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1183 |
< |
* on interruption throws |
1182 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1183 |
> |
* on interruption throws |
1184 |
|
*/ |
1185 |
< |
public void testTimedPollWithOfferLast() { |
1185 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1186 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1187 |
< |
Thread t = new Thread(new Runnable() { |
1188 |
< |
public void run() { |
1189 |
< |
try { |
1190 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1191 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1192 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1193 |
< |
threadShouldThrow(); |
1194 |
< |
} catch (InterruptedException success) { } |
1195 |
< |
} |
1196 |
< |
}); |
1197 |
< |
try { |
1198 |
< |
t.start(); |
1199 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1200 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1201 |
< |
t.interrupt(); |
1391 |
< |
t.join(); |
1392 |
< |
} catch (Exception e) { |
1393 |
< |
unexpectedException(); |
1394 |
< |
} |
1187 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1188 |
> |
public void realRun() throws InterruptedException { |
1189 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1190 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1191 |
> |
try { |
1192 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1193 |
> |
shouldThrow(); |
1194 |
> |
} catch (InterruptedException success) {} |
1195 |
> |
}}); |
1196 |
> |
|
1197 |
> |
t.start(); |
1198 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1199 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1200 |
> |
t.interrupt(); |
1201 |
> |
t.join(); |
1202 |
|
} |
1203 |
|
|
1204 |
|
|
1208 |
|
public void testElement() { |
1209 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1210 |
|
for (int i = 0; i < SIZE; ++i) { |
1211 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1211 |
> |
assertEquals(i, q.element()); |
1212 |
|
q.poll(); |
1213 |
|
} |
1214 |
|
try { |
1215 |
|
q.element(); |
1216 |
|
shouldThrow(); |
1217 |
< |
} |
1411 |
< |
catch (NoSuchElementException success) {} |
1217 |
> |
} catch (NoSuchElementException success) {} |
1218 |
|
} |
1219 |
|
|
1220 |
|
/** |
1312 |
|
/** |
1313 |
|
* toArray contains all elements |
1314 |
|
*/ |
1315 |
< |
public void testToArray() { |
1315 |
> |
public void testToArray() throws InterruptedException{ |
1316 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1317 |
|
Object[] o = q.toArray(); |
1512 |
– |
try { |
1318 |
|
for (int i = 0; i < o.length; i++) |
1319 |
|
assertEquals(o[i], q.take()); |
1515 |
– |
} catch (InterruptedException e) { |
1516 |
– |
unexpectedException(); |
1517 |
– |
} |
1320 |
|
} |
1321 |
|
|
1322 |
|
/** |
1323 |
|
* toArray(a) contains all elements |
1324 |
|
*/ |
1325 |
< |
public void testToArray2() { |
1325 |
> |
public void testToArray2() throws InterruptedException { |
1326 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1327 |
|
Integer[] ints = new Integer[SIZE]; |
1328 |
|
ints = (Integer[])q.toArray(ints); |
1329 |
< |
try { |
1330 |
< |
for (int i = 0; i < ints.length; i++) |
1529 |
< |
assertEquals(ints[i], q.take()); |
1530 |
< |
} catch (InterruptedException e) { |
1531 |
< |
unexpectedException(); |
1532 |
< |
} |
1329 |
> |
for (int i = 0; i < ints.length; i++) |
1330 |
> |
assertEquals(ints[i], q.take()); |
1331 |
|
} |
1332 |
|
|
1333 |
|
/** |
1334 |
|
* toArray(null) throws NPE |
1335 |
|
*/ |
1336 |
|
public void testToArray_BadArg() { |
1337 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1338 |
|
try { |
1540 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1339 |
|
Object o[] = q.toArray(null); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (NullPointerException success) {} |
1345 |
|
* toArray with incompatible array type throws CCE |
1346 |
|
*/ |
1347 |
|
public void testToArray1_BadArg() { |
1348 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1349 |
|
try { |
1350 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1552 |
< |
Object o[] = q.toArray(new String[10] ); |
1350 |
> |
Object o[] = q.toArray(new String[10]); |
1351 |
|
shouldThrow(); |
1352 |
< |
} catch (ArrayStoreException success) {} |
1352 |
> |
} catch (ArrayStoreException success) {} |
1353 |
|
} |
1354 |
|
|
1355 |
|
|
1356 |
|
/** |
1357 |
|
* iterator iterates through all elements |
1358 |
|
*/ |
1359 |
< |
public void testIterator() { |
1359 |
> |
public void testIterator() throws InterruptedException { |
1360 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1361 |
|
Iterator it = q.iterator(); |
1362 |
< |
try { |
1363 |
< |
while (it.hasNext()) { |
1566 |
< |
assertEquals(it.next(), q.take()); |
1567 |
< |
} |
1568 |
< |
} catch (InterruptedException e) { |
1569 |
< |
unexpectedException(); |
1362 |
> |
while (it.hasNext()) { |
1363 |
> |
assertEquals(it.next(), q.take()); |
1364 |
|
} |
1365 |
|
} |
1366 |
|
|
1367 |
|
/** |
1368 |
|
* iterator.remove removes current element |
1369 |
|
*/ |
1370 |
< |
public void testIteratorRemove () { |
1370 |
> |
public void testIteratorRemove() { |
1371 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1372 |
|
q.add(two); |
1373 |
|
q.add(one); |
1378 |
|
it.remove(); |
1379 |
|
|
1380 |
|
it = q.iterator(); |
1381 |
< |
assertEquals(it.next(), one); |
1382 |
< |
assertEquals(it.next(), three); |
1381 |
> |
assertSame(it.next(), one); |
1382 |
> |
assertSame(it.next(), three); |
1383 |
|
assertFalse(it.hasNext()); |
1384 |
|
} |
1385 |
|
|
1395 |
|
assertEquals(0, q.remainingCapacity()); |
1396 |
|
int k = 0; |
1397 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1398 |
< |
int i = ((Integer)(it.next())).intValue(); |
1605 |
< |
assertEquals(++k, i); |
1398 |
> |
assertEquals(++k, it.next()); |
1399 |
|
} |
1400 |
|
assertEquals(3, k); |
1401 |
|
} |
1403 |
|
/** |
1404 |
|
* Modifications do not cause iterators to fail |
1405 |
|
*/ |
1406 |
< |
public void testWeaklyConsistentIteration () { |
1406 |
> |
public void testWeaklyConsistentIteration() { |
1407 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1408 |
|
q.add(one); |
1409 |
|
q.add(two); |
1410 |
|
q.add(three); |
1411 |
< |
try { |
1412 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1413 |
< |
q.remove(); |
1621 |
< |
it.next(); |
1622 |
< |
} |
1623 |
< |
} |
1624 |
< |
catch (ConcurrentModificationException e) { |
1625 |
< |
unexpectedException(); |
1411 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1412 |
> |
q.remove(); |
1413 |
> |
it.next(); |
1414 |
|
} |
1415 |
|
assertEquals(0, q.size()); |
1416 |
|
} |
1431 |
|
assertFalse(it.hasNext()); |
1432 |
|
try { |
1433 |
|
it.next(); |
1434 |
< |
} catch (NoSuchElementException success) { |
1435 |
< |
} |
1434 |
> |
shouldThrow(); |
1435 |
> |
} catch (NoSuchElementException success) {} |
1436 |
|
} |
1437 |
|
|
1438 |
|
/** |
1446 |
|
q.add(new Integer(1)); |
1447 |
|
int k = 0; |
1448 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1449 |
< |
int i = ((Integer)(it.next())).intValue(); |
1662 |
< |
assertEquals(++k, i); |
1449 |
> |
assertEquals(++k, it.next()); |
1450 |
|
} |
1451 |
|
|
1452 |
|
assertEquals(3, k); |
1459 |
|
/** |
1460 |
|
* descendingIterator.remove removes current element |
1461 |
|
*/ |
1462 |
< |
public void testDescendingIteratorRemove () { |
1462 |
> |
public void testDescendingIteratorRemove() { |
1463 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1464 |
|
for (int iters = 0; iters < 100; ++iters) { |
1465 |
|
q.add(new Integer(3)); |
1499 |
|
q.add(one); |
1500 |
|
q.add(two); |
1501 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1502 |
< |
executor.execute(new Runnable() { |
1503 |
< |
public void run() { |
1504 |
< |
threadAssertFalse(q.offer(three)); |
1505 |
< |
try { |
1506 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1507 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1508 |
< |
} |
1509 |
< |
catch (InterruptedException e) { |
1510 |
< |
threadUnexpectedException(); |
1511 |
< |
} |
1512 |
< |
} |
1513 |
< |
}); |
1727 |
< |
|
1728 |
< |
executor.execute(new Runnable() { |
1729 |
< |
public void run() { |
1730 |
< |
try { |
1731 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1732 |
< |
threadAssertEquals(one, q.take()); |
1733 |
< |
} |
1734 |
< |
catch (InterruptedException e) { |
1735 |
< |
threadUnexpectedException(); |
1736 |
< |
} |
1737 |
< |
} |
1738 |
< |
}); |
1502 |
> |
executor.execute(new CheckedRunnable() { |
1503 |
> |
public void realRun() throws InterruptedException { |
1504 |
> |
assertFalse(q.offer(three)); |
1505 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1506 |
> |
assertEquals(0, q.remainingCapacity()); |
1507 |
> |
}}); |
1508 |
> |
|
1509 |
> |
executor.execute(new CheckedRunnable() { |
1510 |
> |
public void realRun() throws InterruptedException { |
1511 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1512 |
> |
assertSame(one, q.take()); |
1513 |
> |
}}); |
1514 |
|
|
1515 |
|
joinPool(executor); |
1516 |
|
} |
1521 |
|
public void testPollInExecutor() { |
1522 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1523 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1524 |
< |
executor.execute(new Runnable() { |
1525 |
< |
public void run() { |
1526 |
< |
threadAssertNull(q.poll()); |
1527 |
< |
try { |
1528 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1529 |
< |
threadAssertTrue(q.isEmpty()); |
1530 |
< |
} |
1531 |
< |
catch (InterruptedException e) { |
1532 |
< |
threadUnexpectedException(); |
1533 |
< |
} |
1534 |
< |
} |
1535 |
< |
}); |
1761 |
< |
|
1762 |
< |
executor.execute(new Runnable() { |
1763 |
< |
public void run() { |
1764 |
< |
try { |
1765 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1766 |
< |
q.put(one); |
1767 |
< |
} |
1768 |
< |
catch (InterruptedException e) { |
1769 |
< |
threadUnexpectedException(); |
1770 |
< |
} |
1771 |
< |
} |
1772 |
< |
}); |
1524 |
> |
executor.execute(new CheckedRunnable() { |
1525 |
> |
public void realRun() throws InterruptedException { |
1526 |
> |
assertNull(q.poll()); |
1527 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1528 |
> |
assertTrue(q.isEmpty()); |
1529 |
> |
}}); |
1530 |
> |
|
1531 |
> |
executor.execute(new CheckedRunnable() { |
1532 |
> |
public void realRun() throws InterruptedException { |
1533 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1534 |
> |
q.put(one); |
1535 |
> |
}}); |
1536 |
|
|
1537 |
|
joinPool(executor); |
1538 |
|
} |
1540 |
|
/** |
1541 |
|
* A deserialized serialized deque has same elements in same order |
1542 |
|
*/ |
1543 |
< |
public void testSerialization() { |
1543 |
> |
public void testSerialization() throws Exception { |
1544 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1545 |
|
|
1546 |
< |
try { |
1547 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1548 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1549 |
< |
out.writeObject(q); |
1550 |
< |
out.close(); |
1551 |
< |
|
1552 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1553 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1554 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1555 |
< |
assertEquals(q.size(), r.size()); |
1556 |
< |
while (!q.isEmpty()) |
1794 |
< |
assertEquals(q.remove(), r.remove()); |
1795 |
< |
} catch (Exception e) { |
1796 |
< |
unexpectedException(); |
1797 |
< |
} |
1546 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1547 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1548 |
> |
out.writeObject(q); |
1549 |
> |
out.close(); |
1550 |
> |
|
1551 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1552 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1553 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1554 |
> |
assertEquals(q.size(), r.size()); |
1555 |
> |
while (!q.isEmpty()) |
1556 |
> |
assertEquals(q.remove(), r.remove()); |
1557 |
|
} |
1558 |
|
|
1559 |
|
/** |
1564 |
|
try { |
1565 |
|
q.drainTo(null); |
1566 |
|
shouldThrow(); |
1567 |
< |
} catch (NullPointerException success) { |
1809 |
< |
} |
1567 |
> |
} catch (NullPointerException success) {} |
1568 |
|
} |
1569 |
|
|
1570 |
|
/** |
1575 |
|
try { |
1576 |
|
q.drainTo(q); |
1577 |
|
shouldThrow(); |
1578 |
< |
} catch (IllegalArgumentException success) { |
1821 |
< |
} |
1578 |
> |
} catch (IllegalArgumentException success) {} |
1579 |
|
} |
1580 |
|
|
1581 |
|
/** |
1605 |
|
/** |
1606 |
|
* drainTo empties full deque, unblocking a waiting put. |
1607 |
|
*/ |
1608 |
< |
public void testDrainToWithActivePut() { |
1608 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1609 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1610 |
< |
Thread t = new Thread(new Runnable() { |
1611 |
< |
public void run() { |
1612 |
< |
try { |
1613 |
< |
q.put(new Integer(SIZE+1)); |
1614 |
< |
} catch (InterruptedException ie) { |
1615 |
< |
threadUnexpectedException(); |
1616 |
< |
} |
1617 |
< |
} |
1618 |
< |
}); |
1619 |
< |
try { |
1620 |
< |
t.start(); |
1621 |
< |
ArrayList l = new ArrayList(); |
1622 |
< |
q.drainTo(l); |
1866 |
< |
assertTrue(l.size() >= SIZE); |
1867 |
< |
for (int i = 0; i < SIZE; ++i) |
1868 |
< |
assertEquals(l.get(i), new Integer(i)); |
1869 |
< |
t.join(); |
1870 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1871 |
< |
} catch (Exception e) { |
1872 |
< |
unexpectedException(); |
1873 |
< |
} |
1610 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1611 |
> |
public void realRun() throws InterruptedException { |
1612 |
> |
q.put(new Integer(SIZE+1)); |
1613 |
> |
}}); |
1614 |
> |
|
1615 |
> |
t.start(); |
1616 |
> |
ArrayList l = new ArrayList(); |
1617 |
> |
q.drainTo(l); |
1618 |
> |
assertTrue(l.size() >= SIZE); |
1619 |
> |
for (int i = 0; i < SIZE; ++i) |
1620 |
> |
assertEquals(l.get(i), new Integer(i)); |
1621 |
> |
t.join(); |
1622 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1623 |
|
} |
1624 |
|
|
1625 |
|
/** |
1630 |
|
try { |
1631 |
|
q.drainTo(null, 0); |
1632 |
|
shouldThrow(); |
1633 |
< |
} catch (NullPointerException success) { |
1885 |
< |
} |
1633 |
> |
} catch (NullPointerException success) {} |
1634 |
|
} |
1635 |
|
|
1636 |
|
/** |
1641 |
|
try { |
1642 |
|
q.drainTo(q, 0); |
1643 |
|
shouldThrow(); |
1644 |
< |
} catch (IllegalArgumentException success) { |
1897 |
< |
} |
1644 |
> |
} catch (IllegalArgumentException success) {} |
1645 |
|
} |
1646 |
|
|
1647 |
|
/** |