1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NoSuchElementException; |
13 |
> |
import java.util.concurrent.BlockingDeque; |
14 |
> |
import java.util.concurrent.BlockingQueue; |
15 |
> |
import java.util.concurrent.CountDownLatch; |
16 |
> |
import java.util.concurrent.Executors; |
17 |
> |
import java.util.concurrent.ExecutorService; |
18 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
19 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
|
import java.io.*; |
21 |
|
|
22 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
23 |
+ |
|
24 |
+ |
public static class Unbounded extends BlockingQueueTest { |
25 |
+ |
protected BlockingQueue emptyCollection() { |
26 |
+ |
return new LinkedBlockingDeque(); |
27 |
+ |
} |
28 |
+ |
} |
29 |
+ |
|
30 |
+ |
public static class Bounded extends BlockingQueueTest { |
31 |
+ |
protected BlockingQueue emptyCollection() { |
32 |
+ |
return new LinkedBlockingDeque(20); |
33 |
+ |
} |
34 |
+ |
} |
35 |
+ |
|
36 |
|
public static void main(String[] args) { |
37 |
< |
junit.textui.TestRunner.run (suite()); |
37 |
> |
junit.textui.TestRunner.run(suite()); |
38 |
|
} |
39 |
|
|
40 |
|
public static Test suite() { |
41 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
41 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
42 |
> |
new Unbounded().testSuite(), |
43 |
> |
new Bounded().testSuite()); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
|
* Create a deque of given size containing consecutive |
48 |
|
* Integers 0 ... n. |
49 |
|
*/ |
50 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
51 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
50 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
51 |
> |
LinkedBlockingDeque<Integer> q = |
52 |
> |
new LinkedBlockingDeque<Integer>(n); |
53 |
|
assertTrue(q.isEmpty()); |
54 |
|
for (int i = 0; i < n; i++) |
55 |
|
assertTrue(q.offer(new Integer(i))); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* offer(null) throws NPE |
92 |
> |
* offerFirst(null) throws NullPointerException |
93 |
|
*/ |
94 |
|
public void testOfferFirstNull() { |
95 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
96 |
|
try { |
71 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
97 |
|
q.offerFirst(null); |
98 |
|
shouldThrow(); |
99 |
|
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
+ |
* offerLast(null) throws NullPointerException |
104 |
+ |
*/ |
105 |
+ |
public void testOfferLastNull() { |
106 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
107 |
+ |
try { |
108 |
+ |
q.offerLast(null); |
109 |
+ |
shouldThrow(); |
110 |
+ |
} catch (NullPointerException success) {} |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
/** |
114 |
|
* OfferFirst succeeds |
115 |
|
*/ |
116 |
|
public void testOfferFirst() { |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* pollFirst succeeds unless empty |
132 |
> |
* pollFirst succeeds unless empty |
133 |
|
*/ |
134 |
|
public void testPollFirst() { |
135 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
< |
* pollLast succeeds unless empty |
143 |
> |
* pollLast succeeds unless empty |
144 |
|
*/ |
145 |
|
public void testPollLast() { |
146 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* peekFirst returns next element, or null if empty |
154 |
> |
* peekFirst returns next element, or null if empty |
155 |
|
*/ |
156 |
|
public void testPeekFirst() { |
157 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* peek returns next element, or null if empty |
168 |
> |
* peek returns next element, or null if empty |
169 |
|
*/ |
170 |
|
public void testPeek() { |
171 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* peekLast returns next element, or null if empty |
182 |
> |
* peekLast returns next element, or null if empty |
183 |
|
*/ |
184 |
|
public void testPeekLast() { |
185 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
196 |
> |
* getFirst() returns first element, or throws NSEE if empty |
197 |
|
*/ |
198 |
|
public void testFirstElement() { |
199 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* getLast returns next element, or throws NSEE if empty |
212 |
> |
* getLast() returns last element, or throws NSEE if empty |
213 |
|
*/ |
214 |
|
public void testLastElement() { |
215 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
< |
* removeFirst removes next element, or throws NSEE if empty |
228 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
229 |
|
*/ |
230 |
|
public void testRemoveFirst() { |
231 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* removeLast removes last element, or throws NSEE if empty |
243 |
> |
* removeLast() removes last element, or throws NSEE if empty |
244 |
|
*/ |
245 |
|
public void testRemoveLast() { |
246 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* remove removes next element, or throws NSEE if empty |
258 |
> |
* remove removes next element, or throws NSEE if empty |
259 |
|
*/ |
260 |
|
public void testRemove() { |
261 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
305 |
|
LinkedBlockingDeque q = populatedDeque(3); |
306 |
|
q.pollLast(); |
307 |
|
q.addFirst(four); |
308 |
< |
assertEquals(four,q.peekFirst()); |
308 |
> |
assertSame(four, q.peekFirst()); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
315 |
|
LinkedBlockingDeque q = populatedDeque(3); |
316 |
|
q.pollLast(); |
317 |
|
q.addLast(four); |
318 |
< |
assertEquals(four,q.peekLast()); |
318 |
> |
assertSame(four, q.peekLast()); |
319 |
|
} |
320 |
|
|
285 |
– |
|
321 |
|
/** |
322 |
|
* A new deque has the indicated capacity, or Integer.MAX_VALUE if |
323 |
|
* none given |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Constructor throws IAE if capacity argument nonpositive |
331 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
332 |
|
*/ |
333 |
|
public void testConstructor2() { |
334 |
|
try { |
335 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
335 |
> |
new LinkedBlockingDeque(0); |
336 |
|
shouldThrow(); |
337 |
|
} catch (IllegalArgumentException success) {} |
338 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
< |
* Initializing from null Collection throws NPE |
341 |
> |
* Initializing from null Collection throws NullPointerException |
342 |
|
*/ |
343 |
|
public void testConstructor3() { |
344 |
|
try { |
345 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
345 |
> |
new LinkedBlockingDeque(null); |
346 |
|
shouldThrow(); |
347 |
|
} catch (NullPointerException success) {} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* Initializing from Collection of null elements throws NPE |
351 |
> |
* Initializing from Collection of null elements throws NullPointerException |
352 |
|
*/ |
353 |
|
public void testConstructor4() { |
354 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
355 |
|
try { |
356 |
< |
Integer[] ints = new Integer[SIZE]; |
321 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
356 |
> |
new LinkedBlockingDeque(elements); |
357 |
|
shouldThrow(); |
358 |
|
} catch (NullPointerException success) {} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* Initializing from Collection with some null elements throws NPE |
362 |
> |
* Initializing from Collection with some null elements throws |
363 |
> |
* NullPointerException |
364 |
|
*/ |
365 |
|
public void testConstructor5() { |
366 |
+ |
Integer[] ints = new Integer[SIZE]; |
367 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
368 |
+ |
ints[i] = i; |
369 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
370 |
|
try { |
371 |
< |
Integer[] ints = new Integer[SIZE]; |
332 |
< |
for (int i = 0; i < SIZE-1; ++i) |
333 |
< |
ints[i] = new Integer(i); |
334 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
371 |
> |
new LinkedBlockingDeque(elements); |
372 |
|
shouldThrow(); |
373 |
|
} catch (NullPointerException success) {} |
374 |
|
} |
379 |
|
public void testConstructor6() { |
380 |
|
Integer[] ints = new Integer[SIZE]; |
381 |
|
for (int i = 0; i < SIZE; ++i) |
382 |
< |
ints[i] = new Integer(i); |
382 |
> |
ints[i] = i; |
383 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
384 |
|
for (int i = 0; i < SIZE; ++i) |
385 |
|
assertEquals(ints[i], q.poll()); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
384 |
– |
* offer(null) throws NPE |
385 |
– |
*/ |
386 |
– |
public void testOfferNull() { |
387 |
– |
try { |
388 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
389 |
– |
q.offer(null); |
390 |
– |
shouldThrow(); |
391 |
– |
} catch (NullPointerException success) {} |
392 |
– |
} |
393 |
– |
|
394 |
– |
/** |
395 |
– |
* add(null) throws NPE |
396 |
– |
*/ |
397 |
– |
public void testAddNull() { |
398 |
– |
try { |
399 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
400 |
– |
q.add(null); |
401 |
– |
shouldThrow(); |
402 |
– |
} catch (NullPointerException success) {} |
403 |
– |
} |
404 |
– |
|
405 |
– |
/** |
421 |
|
* push(null) throws NPE |
422 |
|
*/ |
423 |
|
public void testPushNull() { |
452 |
|
LinkedBlockingDeque q = populatedDeque(3); |
453 |
|
q.pollLast(); |
454 |
|
q.push(four); |
455 |
< |
assertEquals(four,q.peekFirst()); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
|
} |
457 |
|
|
443 |
– |
|
458 |
|
/** |
459 |
< |
* pop removes next element, or throws NSEE if empty |
459 |
> |
* pop removes next element, or throws NSEE if empty |
460 |
|
*/ |
461 |
|
public void testPop() { |
462 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
469 |
|
} catch (NoSuchElementException success) {} |
470 |
|
} |
471 |
|
|
458 |
– |
|
472 |
|
/** |
473 |
|
* Offer succeeds if not full; fails if full |
474 |
|
*/ |
482 |
|
* add succeeds if not full; throws ISE if full |
483 |
|
*/ |
484 |
|
public void testAdd() { |
485 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
486 |
+ |
for (int i = 0; i < SIZE; ++i) |
487 |
+ |
assertTrue(q.add(new Integer(i))); |
488 |
+ |
assertEquals(0, q.remainingCapacity()); |
489 |
|
try { |
473 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
474 |
– |
for (int i = 0; i < SIZE; ++i) { |
475 |
– |
assertTrue(q.add(new Integer(i))); |
476 |
– |
} |
477 |
– |
assertEquals(0, q.remainingCapacity()); |
490 |
|
q.add(new Integer(SIZE)); |
491 |
|
shouldThrow(); |
492 |
|
} catch (IllegalStateException success) {} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
484 |
– |
* addAll(null) throws NPE |
485 |
– |
*/ |
486 |
– |
public void testAddAll1() { |
487 |
– |
try { |
488 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
489 |
– |
q.addAll(null); |
490 |
– |
shouldThrow(); |
491 |
– |
} catch (NullPointerException success) {} |
492 |
– |
} |
493 |
– |
|
494 |
– |
/** |
496 |
|
* addAll(this) throws IAE |
497 |
|
*/ |
498 |
|
public void testAddAllSelf() { |
499 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
500 |
|
try { |
499 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
501 |
|
q.addAll(q); |
502 |
|
shouldThrow(); |
503 |
|
} catch (IllegalArgumentException success) {} |
504 |
|
} |
505 |
|
|
506 |
|
/** |
506 |
– |
* addAll of a collection with null elements throws NPE |
507 |
– |
*/ |
508 |
– |
public void testAddAll2() { |
509 |
– |
try { |
510 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
511 |
– |
Integer[] ints = new Integer[SIZE]; |
512 |
– |
q.addAll(Arrays.asList(ints)); |
513 |
– |
shouldThrow(); |
514 |
– |
} catch (NullPointerException success) {} |
515 |
– |
} |
516 |
– |
/** |
507 |
|
* addAll of a collection with any null elements throws NPE after |
508 |
|
* possibly adding some elements |
509 |
|
*/ |
510 |
|
public void testAddAll3() { |
511 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
512 |
+ |
Integer[] ints = new Integer[SIZE]; |
513 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
514 |
+ |
ints[i] = new Integer(i); |
515 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
516 |
|
try { |
517 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
523 |
< |
Integer[] ints = new Integer[SIZE]; |
524 |
< |
for (int i = 0; i < SIZE-1; ++i) |
525 |
< |
ints[i] = new Integer(i); |
526 |
< |
q.addAll(Arrays.asList(ints)); |
517 |
> |
q.addAll(elements); |
518 |
|
shouldThrow(); |
519 |
|
} catch (NullPointerException success) {} |
520 |
|
} |
521 |
+ |
|
522 |
|
/** |
523 |
< |
* addAll throws ISE if not enough room |
523 |
> |
* addAll throws IllegalStateException if not enough room |
524 |
|
*/ |
525 |
|
public void testAddAll4() { |
526 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1); |
527 |
+ |
Integer[] ints = new Integer[SIZE]; |
528 |
+ |
for (int i = 0; i < SIZE; ++i) |
529 |
+ |
ints[i] = new Integer(i); |
530 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
531 |
|
try { |
532 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
536 |
< |
Integer[] ints = new Integer[SIZE]; |
537 |
< |
for (int i = 0; i < SIZE; ++i) |
538 |
< |
ints[i] = new Integer(i); |
539 |
< |
q.addAll(Arrays.asList(ints)); |
532 |
> |
q.addAll(elements); |
533 |
|
shouldThrow(); |
534 |
|
} catch (IllegalStateException success) {} |
535 |
|
} |
549 |
|
assertEquals(ints[i], q.poll()); |
550 |
|
} |
551 |
|
|
559 |
– |
|
560 |
– |
/** |
561 |
– |
* put(null) throws NPE |
562 |
– |
*/ |
563 |
– |
public void testPutNull() throws InterruptedException { |
564 |
– |
try { |
565 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
566 |
– |
q.put(null); |
567 |
– |
shouldThrow(); |
568 |
– |
} catch (NullPointerException success) {} |
569 |
– |
} |
570 |
– |
|
552 |
|
/** |
553 |
|
* all elements successfully put are contained |
554 |
|
*/ |
567 |
|
*/ |
568 |
|
public void testBlockingPut() throws InterruptedException { |
569 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
< |
Thread t = new Thread(new CheckedRunnable() { |
570 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
571 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
572 |
|
public void realRun() throws InterruptedException { |
573 |
|
for (int i = 0; i < SIZE; ++i) |
574 |
|
q.put(i); |
575 |
|
assertEquals(SIZE, q.size()); |
576 |
|
assertEquals(0, q.remainingCapacity()); |
577 |
+ |
|
578 |
+ |
Thread.currentThread().interrupt(); |
579 |
|
try { |
580 |
|
q.put(99); |
581 |
|
shouldThrow(); |
582 |
|
} catch (InterruptedException success) {} |
583 |
+ |
assertFalse(Thread.interrupted()); |
584 |
+ |
|
585 |
+ |
pleaseInterrupt.countDown(); |
586 |
+ |
try { |
587 |
+ |
q.put(99); |
588 |
+ |
shouldThrow(); |
589 |
+ |
} catch (InterruptedException success) {} |
590 |
+ |
assertFalse(Thread.interrupted()); |
591 |
|
}}); |
592 |
|
|
593 |
< |
t.start(); |
594 |
< |
Thread.sleep(SHORT_DELAY_MS); |
593 |
> |
await(pleaseInterrupt); |
594 |
> |
assertThreadStaysAlive(t); |
595 |
|
t.interrupt(); |
596 |
< |
t.join(); |
596 |
> |
awaitTermination(t); |
597 |
|
assertEquals(SIZE, q.size()); |
598 |
|
assertEquals(0, q.remainingCapacity()); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
< |
* put blocks waiting for take when full |
602 |
> |
* put blocks interruptibly waiting for take when full |
603 |
|
*/ |
604 |
|
public void testPutWithTake() throws InterruptedException { |
605 |
|
final int capacity = 2; |
606 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
607 |
< |
Thread t = new Thread(new CheckedRunnable() { |
607 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
608 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
609 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
610 |
|
public void realRun() throws InterruptedException { |
611 |
< |
for (int i = 0; i < capacity + 1; i++) |
611 |
> |
for (int i = 0; i < capacity; i++) |
612 |
|
q.put(i); |
613 |
+ |
pleaseTake.countDown(); |
614 |
+ |
q.put(86); |
615 |
+ |
|
616 |
+ |
pleaseInterrupt.countDown(); |
617 |
|
try { |
618 |
|
q.put(99); |
619 |
|
shouldThrow(); |
620 |
|
} catch (InterruptedException success) {} |
621 |
+ |
assertFalse(Thread.interrupted()); |
622 |
|
}}); |
623 |
|
|
624 |
< |
t.start(); |
626 |
< |
Thread.sleep(SHORT_DELAY_MS); |
624 |
> |
await(pleaseTake); |
625 |
|
assertEquals(q.remainingCapacity(), 0); |
626 |
|
assertEquals(0, q.take()); |
627 |
< |
Thread.sleep(SHORT_DELAY_MS); |
627 |
> |
|
628 |
> |
await(pleaseInterrupt); |
629 |
> |
assertThreadStaysAlive(t); |
630 |
|
t.interrupt(); |
631 |
< |
t.join(); |
631 |
> |
awaitTermination(t); |
632 |
|
assertEquals(q.remainingCapacity(), 0); |
633 |
|
} |
634 |
|
|
637 |
|
*/ |
638 |
|
public void testTimedOffer() throws InterruptedException { |
639 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
640 |
< |
Thread t = new Thread(new CheckedRunnable() { |
640 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
641 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
642 |
|
public void realRun() throws InterruptedException { |
643 |
|
q.put(new Object()); |
644 |
|
q.put(new Object()); |
645 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
645 |
> |
long startTime = System.nanoTime(); |
646 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
647 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
648 |
> |
pleaseInterrupt.countDown(); |
649 |
|
try { |
650 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
650 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
651 |
|
shouldThrow(); |
652 |
|
} catch (InterruptedException success) {} |
653 |
|
}}); |
654 |
|
|
655 |
< |
t.start(); |
656 |
< |
Thread.sleep(SMALL_DELAY_MS); |
655 |
> |
await(pleaseInterrupt); |
656 |
> |
assertThreadStaysAlive(t); |
657 |
|
t.interrupt(); |
658 |
< |
t.join(); |
658 |
> |
awaitTermination(t); |
659 |
|
} |
660 |
|
|
661 |
|
/** |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
< |
* take blocks interruptibly when empty |
669 |
< |
*/ |
670 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
671 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
672 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
673 |
< |
public void realRun() throws InterruptedException { |
674 |
< |
q.take(); |
675 |
< |
}}; |
676 |
< |
|
677 |
< |
t.start(); |
678 |
< |
Thread.sleep(SHORT_DELAY_MS); |
679 |
< |
t.interrupt(); |
680 |
< |
t.join(); |
681 |
< |
} |
682 |
< |
|
683 |
< |
/** |
684 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
672 |
> |
* take removes existing elements until empty, then blocks interruptibly |
673 |
|
*/ |
674 |
|
public void testBlockingTake() throws InterruptedException { |
675 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
676 |
< |
Thread t = new Thread(new CheckedRunnable() { |
676 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
677 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
678 |
|
public void realRun() throws InterruptedException { |
679 |
|
for (int i = 0; i < SIZE; ++i) { |
680 |
|
assertEquals(i, q.take()); |
681 |
|
} |
682 |
+ |
|
683 |
+ |
Thread.currentThread().interrupt(); |
684 |
+ |
try { |
685 |
+ |
q.take(); |
686 |
+ |
shouldThrow(); |
687 |
+ |
} catch (InterruptedException success) {} |
688 |
+ |
assertFalse(Thread.interrupted()); |
689 |
+ |
|
690 |
+ |
pleaseInterrupt.countDown(); |
691 |
|
try { |
692 |
|
q.take(); |
693 |
|
shouldThrow(); |
694 |
|
} catch (InterruptedException success) {} |
695 |
+ |
assertFalse(Thread.interrupted()); |
696 |
|
}}); |
697 |
|
|
698 |
< |
t.start(); |
699 |
< |
Thread.sleep(SHORT_DELAY_MS); |
698 |
> |
await(pleaseInterrupt); |
699 |
> |
assertThreadStaysAlive(t); |
700 |
|
t.interrupt(); |
701 |
< |
t.join(); |
701 |
> |
awaitTermination(t); |
702 |
|
} |
703 |
|
|
705 |
– |
|
704 |
|
/** |
705 |
|
* poll succeeds unless empty |
706 |
|
*/ |
729 |
|
public void testTimedPoll() throws InterruptedException { |
730 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
731 |
|
for (int i = 0; i < SIZE; ++i) { |
732 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
733 |
< |
} |
734 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
732 |
> |
long startTime = System.nanoTime(); |
733 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
734 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
735 |
> |
} |
736 |
> |
long startTime = System.nanoTime(); |
737 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
738 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
739 |
> |
checkEmpty(q); |
740 |
|
} |
741 |
|
|
742 |
|
/** |
744 |
|
* returning timeout status |
745 |
|
*/ |
746 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
747 |
< |
Thread t = new Thread(new CheckedRunnable() { |
747 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
748 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
749 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
750 |
|
public void realRun() throws InterruptedException { |
746 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
751 |
|
for (int i = 0; i < SIZE; ++i) { |
752 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
752 |
> |
long t0 = System.nanoTime(); |
753 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
754 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
755 |
|
} |
756 |
+ |
long t0 = System.nanoTime(); |
757 |
+ |
aboutToWait.countDown(); |
758 |
|
try { |
759 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
759 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
760 |
|
shouldThrow(); |
761 |
< |
} catch (InterruptedException success) {} |
762 |
< |
}}); |
763 |
< |
|
756 |
< |
t.start(); |
757 |
< |
Thread.sleep(SHORT_DELAY_MS); |
758 |
< |
t.interrupt(); |
759 |
< |
t.join(); |
760 |
< |
} |
761 |
< |
|
762 |
< |
/** |
763 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
764 |
< |
* on interruption throws |
765 |
< |
*/ |
766 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
767 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
768 |
< |
Thread t = new Thread(new CheckedRunnable() { |
769 |
< |
public void realRun() throws InterruptedException { |
770 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
771 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
772 |
< |
try { |
773 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
774 |
< |
shouldThrow(); |
775 |
< |
} catch (InterruptedException success) {} |
761 |
> |
} catch (InterruptedException success) { |
762 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
763 |
> |
} |
764 |
|
}}); |
765 |
|
|
766 |
< |
t.start(); |
767 |
< |
Thread.sleep(SMALL_DELAY_MS); |
780 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
766 |
> |
aboutToWait.await(); |
767 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
768 |
|
t.interrupt(); |
769 |
< |
t.join(); |
769 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
770 |
> |
checkEmpty(q); |
771 |
|
} |
772 |
|
|
785 |
– |
|
773 |
|
/** |
774 |
|
* putFirst(null) throws NPE |
775 |
|
*/ |
776 |
< |
public void testPutFirstNull() throws InterruptedException { |
776 |
> |
public void testPutFirstNull() throws InterruptedException { |
777 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
|
try { |
791 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
779 |
|
q.putFirst(null); |
780 |
|
shouldThrow(); |
781 |
|
} catch (NullPointerException success) {} |
782 |
< |
} |
782 |
> |
} |
783 |
|
|
784 |
|
/** |
785 |
|
* all elements successfully putFirst are contained |
786 |
|
*/ |
787 |
< |
public void testPutFirst() throws InterruptedException { |
788 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
789 |
< |
for (int i = 0; i < SIZE; ++i) { |
790 |
< |
Integer I = new Integer(i); |
791 |
< |
q.putFirst(I); |
792 |
< |
assertTrue(q.contains(I)); |
793 |
< |
} |
794 |
< |
assertEquals(0, q.remainingCapacity()); |
787 |
> |
public void testPutFirst() throws InterruptedException { |
788 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
789 |
> |
for (int i = 0; i < SIZE; ++i) { |
790 |
> |
Integer I = new Integer(i); |
791 |
> |
q.putFirst(I); |
792 |
> |
assertTrue(q.contains(I)); |
793 |
> |
} |
794 |
> |
assertEquals(0, q.remainingCapacity()); |
795 |
|
} |
796 |
|
|
797 |
|
/** |
799 |
|
*/ |
800 |
|
public void testBlockingPutFirst() throws InterruptedException { |
801 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
802 |
< |
Thread t = new Thread(new CheckedRunnable() { |
802 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
803 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
804 |
|
public void realRun() throws InterruptedException { |
805 |
|
for (int i = 0; i < SIZE; ++i) |
806 |
|
q.putFirst(i); |
807 |
|
assertEquals(SIZE, q.size()); |
808 |
|
assertEquals(0, q.remainingCapacity()); |
809 |
+ |
|
810 |
+ |
Thread.currentThread().interrupt(); |
811 |
+ |
try { |
812 |
+ |
q.putFirst(99); |
813 |
+ |
shouldThrow(); |
814 |
+ |
} catch (InterruptedException success) {} |
815 |
+ |
assertFalse(Thread.interrupted()); |
816 |
+ |
|
817 |
+ |
pleaseInterrupt.countDown(); |
818 |
|
try { |
819 |
|
q.putFirst(99); |
820 |
|
shouldThrow(); |
821 |
|
} catch (InterruptedException success) {} |
822 |
+ |
assertFalse(Thread.interrupted()); |
823 |
|
}}); |
824 |
|
|
825 |
< |
t.start(); |
826 |
< |
Thread.sleep(SHORT_DELAY_MS); |
825 |
> |
await(pleaseInterrupt); |
826 |
> |
assertThreadStaysAlive(t); |
827 |
|
t.interrupt(); |
828 |
< |
t.join(); |
828 |
> |
awaitTermination(t); |
829 |
|
assertEquals(SIZE, q.size()); |
830 |
|
assertEquals(0, q.remainingCapacity()); |
831 |
|
} |
832 |
|
|
833 |
|
/** |
834 |
< |
* putFirst blocks waiting for take when full |
834 |
> |
* putFirst blocks interruptibly waiting for take when full |
835 |
|
*/ |
836 |
|
public void testPutFirstWithTake() throws InterruptedException { |
837 |
|
final int capacity = 2; |
838 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
839 |
< |
Thread t = new Thread(new CheckedRunnable() { |
839 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
840 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
841 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
842 |
|
public void realRun() throws InterruptedException { |
843 |
< |
for (int i = 0; i < capacity + 1; i++) |
843 |
> |
for (int i = 0; i < capacity; i++) |
844 |
|
q.putFirst(i); |
845 |
+ |
pleaseTake.countDown(); |
846 |
+ |
q.putFirst(86); |
847 |
+ |
|
848 |
+ |
pleaseInterrupt.countDown(); |
849 |
|
try { |
850 |
|
q.putFirst(99); |
851 |
|
shouldThrow(); |
852 |
|
} catch (InterruptedException success) {} |
853 |
+ |
assertFalse(Thread.interrupted()); |
854 |
|
}}); |
855 |
|
|
856 |
< |
t.start(); |
852 |
< |
Thread.sleep(SHORT_DELAY_MS); |
856 |
> |
await(pleaseTake); |
857 |
|
assertEquals(q.remainingCapacity(), 0); |
858 |
|
assertEquals(capacity - 1, q.take()); |
859 |
< |
Thread.sleep(SHORT_DELAY_MS); |
859 |
> |
|
860 |
> |
await(pleaseInterrupt); |
861 |
> |
assertThreadStaysAlive(t); |
862 |
|
t.interrupt(); |
863 |
< |
t.join(); |
863 |
> |
awaitTermination(t); |
864 |
|
assertEquals(q.remainingCapacity(), 0); |
865 |
|
} |
866 |
|
|
869 |
|
*/ |
870 |
|
public void testTimedOfferFirst() throws InterruptedException { |
871 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
872 |
< |
Thread t = new Thread(new CheckedRunnable() { |
872 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
873 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
874 |
|
public void realRun() throws InterruptedException { |
875 |
|
q.putFirst(new Object()); |
876 |
|
q.putFirst(new Object()); |
877 |
< |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
877 |
> |
long startTime = System.nanoTime(); |
878 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
879 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
880 |
> |
pleaseInterrupt.countDown(); |
881 |
|
try { |
882 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
882 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
883 |
|
shouldThrow(); |
884 |
|
} catch (InterruptedException success) {} |
885 |
|
}}); |
886 |
|
|
887 |
< |
t.start(); |
888 |
< |
Thread.sleep(SMALL_DELAY_MS); |
887 |
> |
await(pleaseInterrupt); |
888 |
> |
assertThreadStaysAlive(t); |
889 |
|
t.interrupt(); |
890 |
< |
t.join(); |
890 |
> |
awaitTermination(t); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
901 |
|
} |
902 |
|
|
903 |
|
/** |
904 |
< |
* takeFirst blocks interruptibly when empty |
904 |
> |
* takeFirst() blocks interruptibly when empty |
905 |
|
*/ |
906 |
< |
public void testTakeFirstFromEmpty() throws InterruptedException { |
907 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
908 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
909 |
< |
public void realRun() throws InterruptedException { |
910 |
< |
q.takeFirst(); |
911 |
< |
}}; |
906 |
> |
public void testTakeFirstFromEmptyBlocksInterruptibly() { |
907 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
908 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
909 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
910 |
> |
public void realRun() { |
911 |
> |
threadStarted.countDown(); |
912 |
> |
try { |
913 |
> |
q.takeFirst(); |
914 |
> |
shouldThrow(); |
915 |
> |
} catch (InterruptedException success) {} |
916 |
> |
assertFalse(Thread.interrupted()); |
917 |
> |
}}); |
918 |
|
|
919 |
< |
t.start(); |
920 |
< |
Thread.sleep(SHORT_DELAY_MS); |
919 |
> |
await(threadStarted); |
920 |
> |
assertThreadStaysAlive(t); |
921 |
|
t.interrupt(); |
922 |
< |
t.join(); |
922 |
> |
awaitTermination(t); |
923 |
> |
} |
924 |
> |
|
925 |
> |
/** |
926 |
> |
* takeFirst() throws InterruptedException immediately if interrupted |
927 |
> |
* before waiting |
928 |
> |
*/ |
929 |
> |
public void testTakeFirstFromEmptyAfterInterrupt() { |
930 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
931 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
932 |
> |
public void realRun() { |
933 |
> |
Thread.currentThread().interrupt(); |
934 |
> |
try { |
935 |
> |
q.takeFirst(); |
936 |
> |
shouldThrow(); |
937 |
> |
} catch (InterruptedException success) {} |
938 |
> |
assertFalse(Thread.interrupted()); |
939 |
> |
}}); |
940 |
> |
|
941 |
> |
awaitTermination(t); |
942 |
|
} |
943 |
|
|
944 |
|
/** |
945 |
< |
* TakeFirst removes existing elements until empty, then blocks interruptibly |
945 |
> |
* takeLast() blocks interruptibly when empty |
946 |
> |
*/ |
947 |
> |
public void testTakeLastFromEmptyBlocksInterruptibly() { |
948 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
949 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
950 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
951 |
> |
public void realRun() { |
952 |
> |
threadStarted.countDown(); |
953 |
> |
try { |
954 |
> |
q.takeLast(); |
955 |
> |
shouldThrow(); |
956 |
> |
} catch (InterruptedException success) {} |
957 |
> |
assertFalse(Thread.interrupted()); |
958 |
> |
}}); |
959 |
> |
|
960 |
> |
await(threadStarted); |
961 |
> |
assertThreadStaysAlive(t); |
962 |
> |
t.interrupt(); |
963 |
> |
awaitTermination(t); |
964 |
> |
} |
965 |
> |
|
966 |
> |
/** |
967 |
> |
* takeLast() throws InterruptedException immediately if interrupted |
968 |
> |
* before waiting |
969 |
> |
*/ |
970 |
> |
public void testTakeLastFromEmptyAfterInterrupt() { |
971 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
972 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
> |
public void realRun() { |
974 |
> |
Thread.currentThread().interrupt(); |
975 |
> |
try { |
976 |
> |
q.takeLast(); |
977 |
> |
shouldThrow(); |
978 |
> |
} catch (InterruptedException success) {} |
979 |
> |
assertFalse(Thread.interrupted()); |
980 |
> |
}}); |
981 |
> |
|
982 |
> |
awaitTermination(t); |
983 |
> |
} |
984 |
> |
|
985 |
> |
/** |
986 |
> |
* takeFirst removes existing elements until empty, then blocks interruptibly |
987 |
|
*/ |
988 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
989 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
990 |
< |
Thread t = new Thread(new CheckedRunnable() { |
990 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
991 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
992 |
|
public void realRun() throws InterruptedException { |
993 |
< |
for (int i = 0; i < SIZE; ++i) |
993 |
> |
for (int i = 0; i < SIZE; ++i) { |
994 |
|
assertEquals(i, q.takeFirst()); |
995 |
+ |
} |
996 |
+ |
|
997 |
+ |
Thread.currentThread().interrupt(); |
998 |
+ |
try { |
999 |
+ |
q.takeFirst(); |
1000 |
+ |
shouldThrow(); |
1001 |
+ |
} catch (InterruptedException success) {} |
1002 |
+ |
assertFalse(Thread.interrupted()); |
1003 |
+ |
|
1004 |
+ |
pleaseInterrupt.countDown(); |
1005 |
|
try { |
1006 |
|
q.takeFirst(); |
1007 |
|
shouldThrow(); |
1008 |
|
} catch (InterruptedException success) {} |
1009 |
+ |
assertFalse(Thread.interrupted()); |
1010 |
|
}}); |
1011 |
|
|
1012 |
< |
t.start(); |
1013 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1012 |
> |
await(pleaseInterrupt); |
1013 |
> |
assertThreadStaysAlive(t); |
1014 |
|
t.interrupt(); |
1015 |
< |
t.join(); |
1015 |
> |
awaitTermination(t); |
1016 |
|
} |
1017 |
|
|
930 |
– |
|
1018 |
|
/** |
1019 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
1020 |
|
*/ |
1032 |
|
public void testTimedPollFirst() throws InterruptedException { |
1033 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1034 |
|
for (int i = 0; i < SIZE; ++i) { |
1035 |
< |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1036 |
< |
} |
1037 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1035 |
> |
long startTime = System.nanoTime(); |
1036 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1037 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1038 |
> |
} |
1039 |
> |
long startTime = System.nanoTime(); |
1040 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1041 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1042 |
> |
checkEmpty(q); |
1043 |
|
} |
1044 |
|
|
1045 |
|
/** |
1047 |
|
* returning timeout status |
1048 |
|
*/ |
1049 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1050 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1050 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1051 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1052 |
|
public void realRun() throws InterruptedException { |
1053 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1054 |
|
for (int i = 0; i < SIZE; ++i) { |
1055 |
< |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1055 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1056 |
|
} |
1057 |
+ |
|
1058 |
+ |
Thread.currentThread().interrupt(); |
1059 |
+ |
try { |
1060 |
+ |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1061 |
+ |
shouldThrow(); |
1062 |
+ |
} catch (InterruptedException success) {} |
1063 |
+ |
assertFalse(Thread.interrupted()); |
1064 |
+ |
|
1065 |
+ |
pleaseInterrupt.countDown(); |
1066 |
|
try { |
1067 |
|
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1068 |
|
shouldThrow(); |
1069 |
|
} catch (InterruptedException success) {} |
1070 |
+ |
assertFalse(Thread.interrupted()); |
1071 |
|
}}); |
1072 |
|
|
1073 |
< |
t.start(); |
1074 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1073 |
> |
await(pleaseInterrupt); |
1074 |
> |
assertThreadStaysAlive(t); |
1075 |
|
t.interrupt(); |
1076 |
< |
t.join(); |
1076 |
> |
awaitTermination(t); |
1077 |
|
} |
1078 |
|
|
1079 |
|
/** |
1080 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1081 |
< |
* on interruption throws |
1080 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1081 |
> |
* on interruption throws |
1082 |
|
*/ |
1083 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
1084 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1085 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1085 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1086 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1087 |
|
public void realRun() throws InterruptedException { |
1088 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1088 |
> |
long startTime = System.nanoTime(); |
1089 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1090 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1091 |
> |
|
1092 |
> |
barrier.await(); |
1093 |
> |
|
1094 |
|
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1095 |
+ |
|
1096 |
+ |
Thread.currentThread().interrupt(); |
1097 |
|
try { |
1098 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1099 |
|
shouldThrow(); |
1100 |
|
} catch (InterruptedException success) {} |
1101 |
+ |
|
1102 |
+ |
barrier.await(); |
1103 |
+ |
try { |
1104 |
+ |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1105 |
+ |
shouldThrow(); |
1106 |
+ |
} catch (InterruptedException success) {} |
1107 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1108 |
|
}}); |
1109 |
|
|
1110 |
< |
t.start(); |
1111 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1112 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1110 |
> |
barrier.await(); |
1111 |
> |
long startTime = System.nanoTime(); |
1112 |
> |
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1113 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1114 |
> |
barrier.await(); |
1115 |
> |
assertThreadStaysAlive(t); |
1116 |
|
t.interrupt(); |
1117 |
< |
t.join(); |
1117 |
> |
awaitTermination(t); |
1118 |
|
} |
1119 |
|
|
1120 |
|
/** |
1121 |
|
* putLast(null) throws NPE |
1122 |
|
*/ |
1123 |
< |
public void testPutLastNull() throws InterruptedException { |
1123 |
> |
public void testPutLastNull() throws InterruptedException { |
1124 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1125 |
|
try { |
1004 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1126 |
|
q.putLast(null); |
1127 |
|
shouldThrow(); |
1128 |
|
} catch (NullPointerException success) {} |
1129 |
< |
} |
1129 |
> |
} |
1130 |
|
|
1131 |
|
/** |
1132 |
|
* all elements successfully putLast are contained |
1133 |
|
*/ |
1134 |
< |
public void testPutLast() throws InterruptedException { |
1135 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1136 |
< |
for (int i = 0; i < SIZE; ++i) { |
1137 |
< |
Integer I = new Integer(i); |
1138 |
< |
q.putLast(I); |
1139 |
< |
assertTrue(q.contains(I)); |
1140 |
< |
} |
1141 |
< |
assertEquals(0, q.remainingCapacity()); |
1134 |
> |
public void testPutLast() throws InterruptedException { |
1135 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1136 |
> |
for (int i = 0; i < SIZE; ++i) { |
1137 |
> |
Integer I = new Integer(i); |
1138 |
> |
q.putLast(I); |
1139 |
> |
assertTrue(q.contains(I)); |
1140 |
> |
} |
1141 |
> |
assertEquals(0, q.remainingCapacity()); |
1142 |
|
} |
1143 |
|
|
1144 |
|
/** |
1146 |
|
*/ |
1147 |
|
public void testBlockingPutLast() throws InterruptedException { |
1148 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1149 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1149 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1150 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1151 |
|
public void realRun() throws InterruptedException { |
1152 |
|
for (int i = 0; i < SIZE; ++i) |
1153 |
|
q.putLast(i); |
1154 |
|
assertEquals(SIZE, q.size()); |
1155 |
|
assertEquals(0, q.remainingCapacity()); |
1156 |
+ |
|
1157 |
+ |
Thread.currentThread().interrupt(); |
1158 |
|
try { |
1159 |
|
q.putLast(99); |
1160 |
|
shouldThrow(); |
1161 |
|
} catch (InterruptedException success) {} |
1162 |
+ |
assertFalse(Thread.interrupted()); |
1163 |
+ |
|
1164 |
+ |
pleaseInterrupt.countDown(); |
1165 |
+ |
try { |
1166 |
+ |
q.putLast(99); |
1167 |
+ |
shouldThrow(); |
1168 |
+ |
} catch (InterruptedException success) {} |
1169 |
+ |
assertFalse(Thread.interrupted()); |
1170 |
|
}}); |
1171 |
|
|
1172 |
< |
t.start(); |
1173 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1172 |
> |
await(pleaseInterrupt); |
1173 |
> |
assertThreadStaysAlive(t); |
1174 |
|
t.interrupt(); |
1175 |
< |
t.join(); |
1175 |
> |
awaitTermination(t); |
1176 |
|
assertEquals(SIZE, q.size()); |
1177 |
|
assertEquals(0, q.remainingCapacity()); |
1178 |
|
} |
1179 |
|
|
1180 |
|
/** |
1181 |
< |
* putLast blocks waiting for take when full |
1181 |
> |
* putLast blocks interruptibly waiting for take when full |
1182 |
|
*/ |
1183 |
|
public void testPutLastWithTake() throws InterruptedException { |
1184 |
|
final int capacity = 2; |
1185 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1186 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1186 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
1187 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1188 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1189 |
|
public void realRun() throws InterruptedException { |
1190 |
< |
for (int i = 0; i < capacity + 1; i++) |
1190 |
> |
for (int i = 0; i < capacity; i++) |
1191 |
|
q.putLast(i); |
1192 |
+ |
pleaseTake.countDown(); |
1193 |
+ |
q.putLast(86); |
1194 |
+ |
|
1195 |
+ |
pleaseInterrupt.countDown(); |
1196 |
|
try { |
1197 |
|
q.putLast(99); |
1198 |
|
shouldThrow(); |
1199 |
|
} catch (InterruptedException success) {} |
1200 |
+ |
assertFalse(Thread.interrupted()); |
1201 |
|
}}); |
1202 |
|
|
1203 |
< |
t.start(); |
1065 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1203 |
> |
await(pleaseTake); |
1204 |
|
assertEquals(q.remainingCapacity(), 0); |
1205 |
|
assertEquals(0, q.take()); |
1206 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1206 |
> |
|
1207 |
> |
await(pleaseInterrupt); |
1208 |
> |
assertThreadStaysAlive(t); |
1209 |
|
t.interrupt(); |
1210 |
< |
t.join(); |
1210 |
> |
awaitTermination(t); |
1211 |
|
assertEquals(q.remainingCapacity(), 0); |
1212 |
|
} |
1213 |
|
|
1216 |
|
*/ |
1217 |
|
public void testTimedOfferLast() throws InterruptedException { |
1218 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1219 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1219 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1220 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1221 |
|
public void realRun() throws InterruptedException { |
1222 |
|
q.putLast(new Object()); |
1223 |
|
q.putLast(new Object()); |
1224 |
< |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1224 |
> |
long startTime = System.nanoTime(); |
1225 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1226 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1227 |
> |
pleaseInterrupt.countDown(); |
1228 |
|
try { |
1229 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1229 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1230 |
|
shouldThrow(); |
1231 |
|
} catch (InterruptedException success) {} |
1232 |
|
}}); |
1233 |
|
|
1234 |
< |
t.start(); |
1235 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1234 |
> |
await(pleaseInterrupt); |
1235 |
> |
assertThreadStaysAlive(t); |
1236 |
|
t.interrupt(); |
1237 |
< |
t.join(); |
1237 |
> |
awaitTermination(t); |
1238 |
|
} |
1239 |
|
|
1240 |
|
/** |
1248 |
|
} |
1249 |
|
|
1250 |
|
/** |
1251 |
< |
* takeLast blocks interruptibly when empty |
1108 |
< |
*/ |
1109 |
< |
public void testTakeLastFromEmpty() throws InterruptedException { |
1110 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1111 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1112 |
< |
public void realRun() throws InterruptedException { |
1113 |
< |
q.takeLast(); |
1114 |
< |
}}; |
1115 |
< |
|
1116 |
< |
t.start(); |
1117 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1118 |
< |
t.interrupt(); |
1119 |
< |
t.join(); |
1120 |
< |
} |
1121 |
< |
|
1122 |
< |
/** |
1123 |
< |
* TakeLast removes existing elements until empty, then blocks interruptibly |
1251 |
> |
* takeLast removes existing elements until empty, then blocks interruptibly |
1252 |
|
*/ |
1253 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1254 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1255 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1255 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1256 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1257 |
|
public void realRun() throws InterruptedException { |
1258 |
< |
for (int i = 0; i < SIZE; ++i) |
1259 |
< |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1258 |
> |
for (int i = 0; i < SIZE; ++i) { |
1259 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1260 |
> |
} |
1261 |
> |
|
1262 |
> |
Thread.currentThread().interrupt(); |
1263 |
> |
try { |
1264 |
> |
q.takeLast(); |
1265 |
> |
shouldThrow(); |
1266 |
> |
} catch (InterruptedException success) {} |
1267 |
> |
assertFalse(Thread.interrupted()); |
1268 |
> |
|
1269 |
> |
pleaseInterrupt.countDown(); |
1270 |
|
try { |
1271 |
|
q.takeLast(); |
1272 |
|
shouldThrow(); |
1273 |
|
} catch (InterruptedException success) {} |
1274 |
+ |
assertFalse(Thread.interrupted()); |
1275 |
|
}}); |
1276 |
|
|
1277 |
< |
t.start(); |
1278 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
> |
await(pleaseInterrupt); |
1278 |
> |
assertThreadStaysAlive(t); |
1279 |
|
t.interrupt(); |
1280 |
< |
t.join(); |
1280 |
> |
awaitTermination(t); |
1281 |
|
} |
1282 |
|
|
1283 |
|
/** |
1297 |
|
public void testTimedPollLast() throws InterruptedException { |
1298 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1299 |
|
for (int i = 0; i < SIZE; ++i) { |
1300 |
< |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1301 |
< |
} |
1302 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1300 |
> |
long startTime = System.nanoTime(); |
1301 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1302 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1303 |
> |
} |
1304 |
> |
long startTime = System.nanoTime(); |
1305 |
> |
assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); |
1306 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1307 |
> |
checkEmpty(q); |
1308 |
|
} |
1309 |
|
|
1310 |
|
/** |
1312 |
|
* returning timeout status |
1313 |
|
*/ |
1314 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1315 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1315 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1316 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1317 |
|
public void realRun() throws InterruptedException { |
1318 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1319 |
|
for (int i = 0; i < SIZE; ++i) { |
1320 |
< |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1320 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1321 |
|
} |
1322 |
+ |
|
1323 |
+ |
Thread.currentThread().interrupt(); |
1324 |
|
try { |
1325 |
< |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1325 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1326 |
|
shouldThrow(); |
1327 |
|
} catch (InterruptedException success) {} |
1328 |
+ |
assertFalse(Thread.interrupted()); |
1329 |
+ |
|
1330 |
+ |
pleaseInterrupt.countDown(); |
1331 |
+ |
try { |
1332 |
+ |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1333 |
+ |
shouldThrow(); |
1334 |
+ |
} catch (InterruptedException success) {} |
1335 |
+ |
assertFalse(Thread.interrupted()); |
1336 |
|
}}); |
1337 |
|
|
1338 |
< |
t.start(); |
1339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1338 |
> |
await(pleaseInterrupt); |
1339 |
> |
assertThreadStaysAlive(t); |
1340 |
|
t.interrupt(); |
1341 |
< |
t.join(); |
1341 |
> |
awaitTermination(t); |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** |
1345 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1346 |
< |
* on interruption throws |
1345 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1346 |
> |
* on interruption throws |
1347 |
|
*/ |
1348 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1349 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1350 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1350 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1351 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1352 |
|
public void realRun() throws InterruptedException { |
1353 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1353 |
> |
long startTime = System.nanoTime(); |
1354 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
1355 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1356 |
> |
|
1357 |
> |
barrier.await(); |
1358 |
> |
|
1359 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1360 |
+ |
|
1361 |
+ |
Thread.currentThread().interrupt(); |
1362 |
+ |
try { |
1363 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1364 |
+ |
shouldThrow(); |
1365 |
+ |
} catch (InterruptedException success) {} |
1366 |
+ |
assertFalse(Thread.interrupted()); |
1367 |
+ |
|
1368 |
+ |
barrier.await(); |
1369 |
|
try { |
1370 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1371 |
|
shouldThrow(); |
1372 |
|
} catch (InterruptedException success) {} |
1373 |
+ |
assertFalse(Thread.interrupted()); |
1374 |
|
}}); |
1375 |
|
|
1376 |
< |
t.start(); |
1377 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1378 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1376 |
> |
barrier.await(); |
1377 |
> |
long startTime = System.nanoTime(); |
1378 |
> |
assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); |
1379 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1380 |
> |
|
1381 |
> |
barrier.await(); |
1382 |
> |
assertThreadStaysAlive(t); |
1383 |
|
t.interrupt(); |
1384 |
< |
t.join(); |
1384 |
> |
awaitTermination(t); |
1385 |
|
} |
1386 |
|
|
1211 |
– |
|
1387 |
|
/** |
1388 |
|
* element returns next element, or throws NSEE if empty |
1389 |
|
*/ |
1405 |
|
public void testRemoveElement() { |
1406 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1407 |
|
for (int i = 1; i < SIZE; i+=2) { |
1408 |
< |
assertTrue(q.remove(new Integer(i))); |
1408 |
> |
assertTrue(q.contains(i)); |
1409 |
> |
assertTrue(q.remove(i)); |
1410 |
> |
assertFalse(q.contains(i)); |
1411 |
> |
assertTrue(q.contains(i-1)); |
1412 |
|
} |
1413 |
|
for (int i = 0; i < SIZE; i+=2) { |
1414 |
< |
assertTrue(q.remove(new Integer(i))); |
1415 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1414 |
> |
assertTrue(q.contains(i)); |
1415 |
> |
assertTrue(q.remove(i)); |
1416 |
> |
assertFalse(q.contains(i)); |
1417 |
> |
assertFalse(q.remove(i+1)); |
1418 |
> |
assertFalse(q.contains(i+1)); |
1419 |
|
} |
1420 |
|
assertTrue(q.isEmpty()); |
1421 |
|
} |
1498 |
|
} |
1499 |
|
|
1500 |
|
/** |
1501 |
< |
* toArray contains all elements |
1501 |
> |
* toArray contains all elements in FIFO order |
1502 |
|
*/ |
1503 |
|
public void testToArray() throws InterruptedException{ |
1504 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1505 |
|
Object[] o = q.toArray(); |
1506 |
|
for (int i = 0; i < o.length; i++) |
1507 |
< |
assertEquals(o[i], q.take()); |
1507 |
> |
assertSame(o[i], q.poll()); |
1508 |
|
} |
1509 |
|
|
1510 |
|
/** |
1511 |
< |
* toArray(a) contains all elements |
1511 |
> |
* toArray(a) contains all elements in FIFO order |
1512 |
|
*/ |
1513 |
< |
public void testToArray2() throws InterruptedException { |
1514 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1513 |
> |
public void testToArray2() { |
1514 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1515 |
|
Integer[] ints = new Integer[SIZE]; |
1516 |
< |
ints = (Integer[])q.toArray(ints); |
1516 |
> |
Integer[] array = q.toArray(ints); |
1517 |
> |
assertSame(ints, array); |
1518 |
|
for (int i = 0; i < ints.length; i++) |
1519 |
< |
assertEquals(ints[i], q.take()); |
1519 |
> |
assertSame(ints[i], q.remove()); |
1520 |
|
} |
1521 |
|
|
1522 |
|
/** |
1523 |
< |
* toArray(null) throws NPE |
1342 |
< |
*/ |
1343 |
< |
public void testToArray_BadArg() { |
1344 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1345 |
< |
try { |
1346 |
< |
Object o[] = q.toArray(null); |
1347 |
< |
shouldThrow(); |
1348 |
< |
} catch (NullPointerException success) {} |
1349 |
< |
} |
1350 |
< |
|
1351 |
< |
/** |
1352 |
< |
* toArray with incompatible array type throws CCE |
1523 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1524 |
|
*/ |
1525 |
|
public void testToArray1_BadArg() { |
1526 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1527 |
|
try { |
1528 |
< |
Object o[] = q.toArray(new String[10]); |
1528 |
> |
q.toArray(new String[10]); |
1529 |
|
shouldThrow(); |
1530 |
|
} catch (ArrayStoreException success) {} |
1531 |
|
} |
1532 |
|
|
1362 |
– |
|
1533 |
|
/** |
1534 |
|
* iterator iterates through all elements |
1535 |
|
*/ |
1544 |
|
/** |
1545 |
|
* iterator.remove removes current element |
1546 |
|
*/ |
1547 |
< |
public void testIteratorRemove () { |
1547 |
> |
public void testIteratorRemove() { |
1548 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1549 |
|
q.add(two); |
1550 |
|
q.add(one); |
1555 |
|
it.remove(); |
1556 |
|
|
1557 |
|
it = q.iterator(); |
1558 |
< |
assertEquals(it.next(), one); |
1559 |
< |
assertEquals(it.next(), three); |
1558 |
> |
assertSame(it.next(), one); |
1559 |
> |
assertSame(it.next(), three); |
1560 |
|
assertFalse(it.hasNext()); |
1561 |
|
} |
1562 |
|
|
1393 |
– |
|
1563 |
|
/** |
1564 |
|
* iterator ordering is FIFO |
1565 |
|
*/ |
1579 |
|
/** |
1580 |
|
* Modifications do not cause iterators to fail |
1581 |
|
*/ |
1582 |
< |
public void testWeaklyConsistentIteration () { |
1582 |
> |
public void testWeaklyConsistentIteration() { |
1583 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1584 |
|
q.add(one); |
1585 |
|
q.add(two); |
1591 |
|
assertEquals(0, q.size()); |
1592 |
|
} |
1593 |
|
|
1425 |
– |
|
1594 |
|
/** |
1595 |
< |
* Descending iterator iterates through all elements |
1595 |
> |
* Descending iterator iterates through all elements |
1596 |
|
*/ |
1597 |
|
public void testDescendingIterator() { |
1598 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1611 |
|
} |
1612 |
|
|
1613 |
|
/** |
1614 |
< |
* Descending iterator ordering is reverse FIFO |
1614 |
> |
* Descending iterator ordering is reverse FIFO |
1615 |
|
*/ |
1616 |
|
public void testDescendingIteratorOrdering() { |
1617 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1634 |
|
/** |
1635 |
|
* descendingIterator.remove removes current element |
1636 |
|
*/ |
1637 |
< |
public void testDescendingIteratorRemove () { |
1637 |
> |
public void testDescendingIteratorRemove() { |
1638 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1639 |
|
for (int iters = 0; iters < 100; ++iters) { |
1640 |
|
q.add(new Integer(3)); |
1653 |
|
} |
1654 |
|
} |
1655 |
|
|
1488 |
– |
|
1656 |
|
/** |
1657 |
|
* toString contains toStrings of elements |
1658 |
|
*/ |
1660 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1661 |
|
String s = q.toString(); |
1662 |
|
for (int i = 0; i < SIZE; ++i) { |
1663 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1663 |
> |
assertTrue(s.contains(String.valueOf(i))); |
1664 |
|
} |
1665 |
|
} |
1666 |
|
|
1500 |
– |
|
1667 |
|
/** |
1668 |
|
* offer transfers elements across Executor tasks |
1669 |
|
*/ |
1672 |
|
q.add(one); |
1673 |
|
q.add(two); |
1674 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1675 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1676 |
|
executor.execute(new CheckedRunnable() { |
1677 |
|
public void realRun() throws InterruptedException { |
1678 |
< |
threadAssertFalse(q.offer(three)); |
1679 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1680 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1678 |
> |
assertFalse(q.offer(three)); |
1679 |
> |
threadsStarted.await(); |
1680 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1681 |
> |
assertEquals(0, q.remainingCapacity()); |
1682 |
|
}}); |
1683 |
|
|
1684 |
|
executor.execute(new CheckedRunnable() { |
1685 |
|
public void realRun() throws InterruptedException { |
1686 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1687 |
< |
threadAssertEquals(one, q.take()); |
1686 |
> |
threadsStarted.await(); |
1687 |
> |
assertSame(one, q.take()); |
1688 |
|
}}); |
1689 |
|
|
1690 |
|
joinPool(executor); |
1691 |
|
} |
1692 |
|
|
1693 |
|
/** |
1694 |
< |
* poll retrieves elements across Executor threads |
1694 |
> |
* timed poll retrieves elements across Executor threads |
1695 |
|
*/ |
1696 |
|
public void testPollInExecutor() { |
1697 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1698 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1699 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1700 |
|
executor.execute(new CheckedRunnable() { |
1701 |
|
public void realRun() throws InterruptedException { |
1702 |
< |
threadAssertNull(q.poll()); |
1703 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1704 |
< |
threadAssertTrue(q.isEmpty()); |
1702 |
> |
assertNull(q.poll()); |
1703 |
> |
threadsStarted.await(); |
1704 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1705 |
> |
checkEmpty(q); |
1706 |
|
}}); |
1707 |
|
|
1708 |
|
executor.execute(new CheckedRunnable() { |
1709 |
|
public void realRun() throws InterruptedException { |
1710 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1710 |
> |
threadsStarted.await(); |
1711 |
|
q.put(one); |
1712 |
|
}}); |
1713 |
|
|
1734 |
|
} |
1735 |
|
|
1736 |
|
/** |
1567 |
– |
* drainTo(null) throws NPE |
1568 |
– |
*/ |
1569 |
– |
public void testDrainToNull() { |
1570 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1571 |
– |
try { |
1572 |
– |
q.drainTo(null); |
1573 |
– |
shouldThrow(); |
1574 |
– |
} catch (NullPointerException success) {} |
1575 |
– |
} |
1576 |
– |
|
1577 |
– |
/** |
1578 |
– |
* drainTo(this) throws IAE |
1579 |
– |
*/ |
1580 |
– |
public void testDrainToSelf() { |
1581 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1582 |
– |
try { |
1583 |
– |
q.drainTo(q); |
1584 |
– |
shouldThrow(); |
1585 |
– |
} catch (IllegalArgumentException success) {} |
1586 |
– |
} |
1587 |
– |
|
1588 |
– |
/** |
1737 |
|
* drainTo(c) empties deque into another collection c |
1738 |
|
*/ |
1739 |
|
public void testDrainTo() { |
1778 |
|
} |
1779 |
|
|
1780 |
|
/** |
1781 |
< |
* drainTo(null, n) throws NPE |
1634 |
< |
*/ |
1635 |
< |
public void testDrainToNullN() { |
1636 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1637 |
< |
try { |
1638 |
< |
q.drainTo(null, 0); |
1639 |
< |
shouldThrow(); |
1640 |
< |
} catch (NullPointerException success) {} |
1641 |
< |
} |
1642 |
< |
|
1643 |
< |
/** |
1644 |
< |
* drainTo(this, n) throws IAE |
1645 |
< |
*/ |
1646 |
< |
public void testDrainToSelfN() { |
1647 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1648 |
< |
try { |
1649 |
< |
q.drainTo(q, 0); |
1650 |
< |
shouldThrow(); |
1651 |
< |
} catch (IllegalArgumentException success) {} |
1652 |
< |
} |
1653 |
< |
|
1654 |
< |
/** |
1655 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1781 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1782 |
|
*/ |
1783 |
|
public void testDrainToN() { |
1784 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1787 |
|
assertTrue(q.offer(new Integer(j))); |
1788 |
|
ArrayList l = new ArrayList(); |
1789 |
|
q.drainTo(l, i); |
1790 |
< |
int k = (i < SIZE)? i : SIZE; |
1790 |
> |
int k = (i < SIZE) ? i : SIZE; |
1791 |
|
assertEquals(l.size(), k); |
1792 |
|
assertEquals(q.size(), SIZE-k); |
1793 |
|
for (int j = 0; j < k; ++j) |