1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
22 |
|
} |
23 |
|
|
24 |
|
/** |
25 |
< |
* |
25 |
> |
* A SynchronousQueue is both empty and full |
26 |
|
*/ |
27 |
|
public void testEmptyFull() { |
28 |
|
SynchronousQueue q = new SynchronousQueue(); |
29 |
|
assertTrue(q.isEmpty()); |
30 |
|
assertEquals(0, q.size()); |
31 |
|
assertEquals(0, q.remainingCapacity()); |
32 |
< |
assertFalse(q.offer(new Integer(3))); |
32 |
> |
assertFalse(q.offer(zero)); |
33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
< |
* |
36 |
> |
* A fair SynchronousQueue is both empty and full |
37 |
> |
*/ |
38 |
> |
public void testFairEmptyFull() { |
39 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
40 |
> |
assertTrue(q.isEmpty()); |
41 |
> |
assertEquals(0, q.size()); |
42 |
> |
assertEquals(0, q.remainingCapacity()); |
43 |
> |
assertFalse(q.offer(zero)); |
44 |
> |
} |
45 |
> |
|
46 |
> |
/** |
47 |
> |
* offer(null) throws NPE |
48 |
|
*/ |
49 |
|
public void testOfferNull() { |
50 |
|
try { |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* |
58 |
> |
* add(null) throws NPE |
59 |
> |
*/ |
60 |
> |
public void testAddNull() { |
61 |
> |
try { |
62 |
> |
SynchronousQueue q = new SynchronousQueue(); |
63 |
> |
q.add(null); |
64 |
> |
shouldThrow(); |
65 |
> |
} catch (NullPointerException success) { } |
66 |
> |
} |
67 |
> |
|
68 |
> |
/** |
69 |
> |
* offer fails if no active taker |
70 |
|
*/ |
71 |
|
public void testOffer() { |
72 |
|
SynchronousQueue q = new SynchronousQueue(); |
73 |
< |
assertFalse(q.offer(new Integer(1))); |
73 |
> |
assertFalse(q.offer(one)); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* |
77 |
> |
* add throws ISE if no active taker |
78 |
|
*/ |
79 |
|
public void testAdd() { |
80 |
|
try { |
81 |
|
SynchronousQueue q = new SynchronousQueue(); |
82 |
|
assertEquals(0, q.remainingCapacity()); |
83 |
< |
q.add(new Integer(0)); |
83 |
> |
q.add(one); |
84 |
> |
shouldThrow(); |
85 |
|
} catch (IllegalStateException success){ |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* |
90 |
> |
* addAll(null) throws NPE |
91 |
|
*/ |
92 |
|
public void testAddAll1() { |
93 |
|
try { |
97 |
|
} |
98 |
|
catch (NullPointerException success) {} |
99 |
|
} |
100 |
+ |
|
101 |
|
/** |
102 |
< |
* |
102 |
> |
* addAll(this) throws IAE |
103 |
> |
*/ |
104 |
> |
public void testAddAllSelf() { |
105 |
> |
try { |
106 |
> |
SynchronousQueue q = new SynchronousQueue(); |
107 |
> |
q.addAll(q); |
108 |
> |
shouldThrow(); |
109 |
> |
} |
110 |
> |
catch (IllegalArgumentException success) {} |
111 |
> |
} |
112 |
> |
|
113 |
> |
/** |
114 |
> |
* addAll of a collection with null elements throws NPE |
115 |
|
*/ |
116 |
|
public void testAddAll2() { |
117 |
|
try { |
123 |
|
catch (NullPointerException success) {} |
124 |
|
} |
125 |
|
/** |
126 |
< |
* |
126 |
> |
* addAll throws ISE if no active taker |
127 |
|
*/ |
128 |
|
public void testAddAll4() { |
129 |
|
try { |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* |
141 |
> |
* put(null) throws NPE |
142 |
|
*/ |
143 |
|
public void testPutNull() { |
144 |
|
try { |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* |
157 |
> |
* put blocks interruptibly if no active taker |
158 |
|
*/ |
159 |
|
public void testBlockingPut() { |
160 |
|
Thread t = new Thread(new Runnable() { |
161 |
|
public void run() { |
162 |
|
try { |
163 |
|
SynchronousQueue q = new SynchronousQueue(); |
164 |
< |
q.put(new Integer(0)); |
164 |
> |
q.put(zero); |
165 |
|
threadShouldThrow(); |
166 |
|
} catch (InterruptedException ie){ |
167 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* |
181 |
> |
* put blocks waiting for take |
182 |
|
*/ |
183 |
|
public void testPutWithTake() { |
184 |
|
final SynchronousQueue q = new SynchronousQueue(); |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
< |
* |
216 |
> |
* timed offer times out if elements not taken |
217 |
|
*/ |
218 |
|
public void testTimedOffer() { |
219 |
|
final SynchronousQueue q = new SynchronousQueue(); |
240 |
|
|
241 |
|
|
242 |
|
/** |
243 |
< |
* |
243 |
> |
* take blocks interruptibly when empty |
244 |
|
*/ |
245 |
|
public void testTakeFromEmpty() { |
246 |
|
final SynchronousQueue q = new SynchronousQueue(); |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
+ |
|
266 |
+ |
/** |
267 |
+ |
* put blocks interruptibly if no active taker |
268 |
+ |
*/ |
269 |
+ |
public void testFairBlockingPut() { |
270 |
+ |
Thread t = new Thread(new Runnable() { |
271 |
+ |
public void run() { |
272 |
+ |
try { |
273 |
+ |
SynchronousQueue q = new SynchronousQueue(true); |
274 |
+ |
q.put(zero); |
275 |
+ |
threadShouldThrow(); |
276 |
+ |
} catch (InterruptedException ie){ |
277 |
+ |
} |
278 |
+ |
}}); |
279 |
+ |
t.start(); |
280 |
+ |
try { |
281 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
282 |
+ |
t.interrupt(); |
283 |
+ |
t.join(); |
284 |
+ |
} |
285 |
+ |
catch (InterruptedException ie) { |
286 |
+ |
unexpectedException(); |
287 |
+ |
} |
288 |
+ |
} |
289 |
+ |
|
290 |
|
/** |
291 |
< |
* |
291 |
> |
* put blocks waiting for take |
292 |
> |
*/ |
293 |
> |
public void testFairPutWithTake() { |
294 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
295 |
> |
Thread t = new Thread(new Runnable() { |
296 |
> |
public void run() { |
297 |
> |
int added = 0; |
298 |
> |
try { |
299 |
> |
q.put(new Object()); |
300 |
> |
++added; |
301 |
> |
q.put(new Object()); |
302 |
> |
++added; |
303 |
> |
q.put(new Object()); |
304 |
> |
++added; |
305 |
> |
q.put(new Object()); |
306 |
> |
++added; |
307 |
> |
threadShouldThrow(); |
308 |
> |
} catch (InterruptedException e){ |
309 |
> |
assertTrue(added >= 1); |
310 |
> |
} |
311 |
> |
} |
312 |
> |
}); |
313 |
> |
try { |
314 |
> |
t.start(); |
315 |
> |
Thread.sleep(SHORT_DELAY_MS); |
316 |
> |
q.take(); |
317 |
> |
Thread.sleep(SHORT_DELAY_MS); |
318 |
> |
t.interrupt(); |
319 |
> |
t.join(); |
320 |
> |
} catch (Exception e){ |
321 |
> |
unexpectedException(); |
322 |
> |
} |
323 |
> |
} |
324 |
> |
|
325 |
> |
/** |
326 |
> |
* timed offer times out if elements not taken |
327 |
> |
*/ |
328 |
> |
public void testFairTimedOffer() { |
329 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
330 |
> |
Thread t = new Thread(new Runnable() { |
331 |
> |
public void run() { |
332 |
> |
try { |
333 |
> |
|
334 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
> |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
336 |
> |
threadShouldThrow(); |
337 |
> |
} catch (InterruptedException success){} |
338 |
> |
} |
339 |
> |
}); |
340 |
> |
|
341 |
> |
try { |
342 |
> |
t.start(); |
343 |
> |
Thread.sleep(SMALL_DELAY_MS); |
344 |
> |
t.interrupt(); |
345 |
> |
t.join(); |
346 |
> |
} catch (Exception e){ |
347 |
> |
unexpectedException(); |
348 |
> |
} |
349 |
> |
} |
350 |
> |
|
351 |
> |
|
352 |
> |
/** |
353 |
> |
* take blocks interruptibly when empty |
354 |
> |
*/ |
355 |
> |
public void testFairTakeFromEmpty() { |
356 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
357 |
> |
Thread t = new Thread(new Runnable() { |
358 |
> |
public void run() { |
359 |
> |
try { |
360 |
> |
q.take(); |
361 |
> |
threadShouldThrow(); |
362 |
> |
} catch (InterruptedException success){ } |
363 |
> |
} |
364 |
> |
}); |
365 |
> |
try { |
366 |
> |
t.start(); |
367 |
> |
Thread.sleep(SHORT_DELAY_MS); |
368 |
> |
t.interrupt(); |
369 |
> |
t.join(); |
370 |
> |
} catch (Exception e){ |
371 |
> |
unexpectedException(); |
372 |
> |
} |
373 |
> |
} |
374 |
> |
|
375 |
> |
/** |
376 |
> |
* poll fails unless active taker |
377 |
|
*/ |
378 |
|
public void testPoll() { |
379 |
|
SynchronousQueue q = new SynchronousQueue(); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
< |
* |
384 |
> |
* timed pool with zero timeout times out if no active taker |
385 |
|
*/ |
386 |
|
public void testTimedPoll0() { |
387 |
|
try { |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* |
396 |
> |
* timed pool with nonzero timeout times out if no active taker |
397 |
|
*/ |
398 |
|
public void testTimedPoll() { |
399 |
|
try { |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* |
408 |
> |
* Interrupted timed poll throws InterruptedException instead of |
409 |
> |
* returning timeout status |
410 |
|
*/ |
411 |
|
public void testInterruptedTimedPoll() { |
412 |
|
Thread t = new Thread(new Runnable() { |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* |
432 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
433 |
> |
* on interruption throws |
434 |
|
*/ |
435 |
|
public void testTimedPollWithOffer() { |
436 |
|
final SynchronousQueue q = new SynchronousQueue(); |
447 |
|
try { |
448 |
|
t.start(); |
449 |
|
Thread.sleep(SMALL_DELAY_MS); |
450 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
450 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
451 |
> |
t.interrupt(); |
452 |
> |
t.join(); |
453 |
> |
} catch (Exception e){ |
454 |
> |
unexpectedException(); |
455 |
> |
} |
456 |
> |
} |
457 |
> |
|
458 |
> |
/** |
459 |
> |
* Interrupted timed poll throws InterruptedException instead of |
460 |
> |
* returning timeout status |
461 |
> |
*/ |
462 |
> |
public void testFairInterruptedTimedPoll() { |
463 |
> |
Thread t = new Thread(new Runnable() { |
464 |
> |
public void run() { |
465 |
> |
try { |
466 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
467 |
> |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
> |
} catch (InterruptedException success){ |
469 |
> |
} |
470 |
> |
}}); |
471 |
> |
t.start(); |
472 |
> |
try { |
473 |
> |
Thread.sleep(SHORT_DELAY_MS); |
474 |
> |
t.interrupt(); |
475 |
> |
t.join(); |
476 |
> |
} |
477 |
> |
catch (InterruptedException ie) { |
478 |
> |
unexpectedException(); |
479 |
> |
} |
480 |
> |
} |
481 |
> |
|
482 |
> |
/** |
483 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
484 |
> |
* on interruption throws |
485 |
> |
*/ |
486 |
> |
public void testFairTimedPollWithOffer() { |
487 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
488 |
> |
Thread t = new Thread(new Runnable() { |
489 |
> |
public void run() { |
490 |
> |
try { |
491 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
492 |
> |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
> |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
> |
threadShouldThrow(); |
495 |
> |
} catch (InterruptedException success) { } |
496 |
> |
} |
497 |
> |
}); |
498 |
> |
try { |
499 |
> |
t.start(); |
500 |
> |
Thread.sleep(SMALL_DELAY_MS); |
501 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
502 |
|
t.interrupt(); |
503 |
|
t.join(); |
504 |
|
} catch (Exception e){ |
508 |
|
|
509 |
|
|
510 |
|
/** |
511 |
< |
* |
511 |
> |
* peek returns null |
512 |
|
*/ |
513 |
|
public void testPeek() { |
514 |
|
SynchronousQueue q = new SynchronousQueue(); |
516 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
< |
* |
519 |
> |
* element throws NSEE |
520 |
|
*/ |
521 |
|
public void testElement() { |
522 |
|
SynchronousQueue q = new SynchronousQueue(); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* |
531 |
> |
* remove throws NSEE if no active taker |
532 |
|
*/ |
533 |
|
public void testRemove() { |
534 |
|
SynchronousQueue q = new SynchronousQueue(); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
< |
* |
543 |
> |
* remove(x) returns false |
544 |
|
*/ |
545 |
|
public void testRemoveElement() { |
546 |
|
SynchronousQueue q = new SynchronousQueue(); |
547 |
< |
assertFalse(q.remove(new Integer(0))); |
547 |
> |
assertFalse(q.remove(zero)); |
548 |
|
assertTrue(q.isEmpty()); |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* |
552 |
> |
* contains returns false |
553 |
|
*/ |
554 |
|
public void testContains() { |
555 |
|
SynchronousQueue q = new SynchronousQueue(); |
556 |
< |
assertFalse(q.contains(new Integer(0))); |
556 |
> |
assertFalse(q.contains(zero)); |
557 |
|
} |
558 |
|
|
559 |
|
/** |
560 |
< |
* |
560 |
> |
* clear ensures isEmpty |
561 |
|
*/ |
562 |
|
public void testClear() { |
563 |
|
SynchronousQueue q = new SynchronousQueue(); |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
< |
* |
569 |
> |
* containsAll returns false unless empty |
570 |
|
*/ |
571 |
|
public void testContainsAll() { |
572 |
|
SynchronousQueue q = new SynchronousQueue(); |
573 |
|
Integer[] empty = new Integer[0]; |
574 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
574 |
> |
assertTrue(q.containsAll(Arrays.asList(empty))); |
575 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
576 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
< |
* |
580 |
> |
* retainAll returns false |
581 |
|
*/ |
582 |
|
public void testRetainAll() { |
583 |
|
SynchronousQueue q = new SynchronousQueue(); |
584 |
|
Integer[] empty = new Integer[0]; |
585 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
586 |
< |
q.retainAll(Arrays.asList(ints)); |
587 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
585 |
> |
assertFalse(q.retainAll(Arrays.asList(empty))); |
586 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
587 |
> |
assertFalse(q.retainAll(Arrays.asList(ints))); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* |
591 |
> |
* removeAll returns false |
592 |
|
*/ |
593 |
|
public void testRemoveAll() { |
594 |
|
SynchronousQueue q = new SynchronousQueue(); |
595 |
|
Integer[] empty = new Integer[0]; |
596 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
597 |
< |
q.removeAll(Arrays.asList(ints)); |
596 |
> |
assertFalse(q.removeAll(Arrays.asList(empty))); |
597 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
598 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
599 |
|
} |
600 |
|
|
601 |
|
|
602 |
|
/** |
603 |
< |
* |
603 |
> |
* toArray is empty |
604 |
|
*/ |
605 |
|
public void testToArray() { |
606 |
|
SynchronousQueue q = new SynchronousQueue(); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
< |
* |
612 |
> |
* toArray(a) is nulled at position 0 |
613 |
|
*/ |
614 |
|
public void testToArray2() { |
615 |
|
SynchronousQueue q = new SynchronousQueue(); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* |
621 |
> |
* toArray(null) throws NPE |
622 |
> |
*/ |
623 |
> |
public void testToArray_BadArg() { |
624 |
> |
try { |
625 |
> |
SynchronousQueue q = new SynchronousQueue(); |
626 |
> |
Object o[] = q.toArray(null); |
627 |
> |
shouldThrow(); |
628 |
> |
} catch(NullPointerException success){} |
629 |
> |
} |
630 |
> |
|
631 |
> |
|
632 |
> |
/** |
633 |
> |
* iterator does not traverse any elements |
634 |
|
*/ |
635 |
|
public void testIterator() { |
636 |
|
SynchronousQueue q = new SynchronousQueue(); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* |
647 |
> |
* iterator remove throws ISE |
648 |
|
*/ |
649 |
|
public void testIteratorRemove() { |
650 |
|
SynchronousQueue q = new SynchronousQueue(); |
657 |
|
} |
658 |
|
|
659 |
|
/** |
660 |
< |
* |
660 |
> |
* toString returns a non-null string |
661 |
|
*/ |
662 |
|
public void testToString() { |
663 |
|
SynchronousQueue q = new SynchronousQueue(); |
664 |
|
String s = q.toString(); |
665 |
< |
assertTrue(s != null); |
665 |
> |
assertNotNull(s); |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
|
/** |
670 |
< |
* |
670 |
> |
* offer transfers elements across Executor tasks |
671 |
|
*/ |
672 |
|
public void testOfferInExecutor() { |
673 |
|
final SynchronousQueue q = new SynchronousQueue(); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* |
707 |
> |
* poll retrieves elements across Executor threads |
708 |
|
*/ |
709 |
|
public void testPollInExecutor() { |
497 |
– |
|
710 |
|
final SynchronousQueue q = new SynchronousQueue(); |
499 |
– |
|
711 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
– |
|
712 |
|
executor.execute(new Runnable() { |
713 |
|
public void run() { |
714 |
|
threadAssertNull(q.poll()); |
735 |
|
}); |
736 |
|
|
737 |
|
joinPool(executor); |
528 |
– |
|
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
< |
* |
741 |
> |
* a deserialized serialized queue is usable |
742 |
|
*/ |
743 |
|
public void testSerialization() { |
744 |
|
SynchronousQueue q = new SynchronousQueue(); |
759 |
|
} |
760 |
|
} |
761 |
|
|
762 |
+ |
/** |
763 |
+ |
* drainTo(null) throws NPE |
764 |
+ |
*/ |
765 |
+ |
public void testDrainToNull() { |
766 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
767 |
+ |
try { |
768 |
+ |
q.drainTo(null); |
769 |
+ |
shouldThrow(); |
770 |
+ |
} catch(NullPointerException success) { |
771 |
+ |
} |
772 |
+ |
} |
773 |
+ |
|
774 |
+ |
/** |
775 |
+ |
* drainTo(this) throws IAE |
776 |
+ |
*/ |
777 |
+ |
public void testDrainToSelf() { |
778 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
779 |
+ |
try { |
780 |
+ |
q.drainTo(q); |
781 |
+ |
shouldThrow(); |
782 |
+ |
} catch(IllegalArgumentException success) { |
783 |
+ |
} |
784 |
+ |
} |
785 |
+ |
|
786 |
+ |
/** |
787 |
+ |
* drainTo(c) of empty queue doesn't transfer elements |
788 |
+ |
*/ |
789 |
+ |
public void testDrainTo() { |
790 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
791 |
+ |
ArrayList l = new ArrayList(); |
792 |
+ |
q.drainTo(l); |
793 |
+ |
assertEquals(q.size(), 0); |
794 |
+ |
assertEquals(l.size(), 0); |
795 |
+ |
} |
796 |
+ |
|
797 |
+ |
/** |
798 |
+ |
* drainTo empties queue, unblocking a waiting put. |
799 |
+ |
*/ |
800 |
+ |
public void testDrainToWithActivePut() { |
801 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
802 |
+ |
Thread t = new Thread(new Runnable() { |
803 |
+ |
public void run() { |
804 |
+ |
try { |
805 |
+ |
q.put(new Integer(1)); |
806 |
+ |
} catch (InterruptedException ie){ |
807 |
+ |
threadUnexpectedException(); |
808 |
+ |
} |
809 |
+ |
} |
810 |
+ |
}); |
811 |
+ |
try { |
812 |
+ |
t.start(); |
813 |
+ |
ArrayList l = new ArrayList(); |
814 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
815 |
+ |
q.drainTo(l); |
816 |
+ |
assertTrue(l.size() <= 1); |
817 |
+ |
if (l.size() > 0) |
818 |
+ |
assertEquals(l.get(0), new Integer(1)); |
819 |
+ |
t.join(); |
820 |
+ |
assertTrue(l.size() <= 1); |
821 |
+ |
} catch(Exception e){ |
822 |
+ |
unexpectedException(); |
823 |
+ |
} |
824 |
+ |
} |
825 |
+ |
|
826 |
+ |
/** |
827 |
+ |
* drainTo(null, n) throws NPE |
828 |
+ |
*/ |
829 |
+ |
public void testDrainToNullN() { |
830 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
831 |
+ |
try { |
832 |
+ |
q.drainTo(null, 0); |
833 |
+ |
shouldThrow(); |
834 |
+ |
} catch(NullPointerException success) { |
835 |
+ |
} |
836 |
+ |
} |
837 |
+ |
|
838 |
+ |
/** |
839 |
+ |
* drainTo(this, n) throws IAE |
840 |
+ |
*/ |
841 |
+ |
public void testDrainToSelfN() { |
842 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
843 |
+ |
try { |
844 |
+ |
q.drainTo(q, 0); |
845 |
+ |
shouldThrow(); |
846 |
+ |
} catch(IllegalArgumentException success) { |
847 |
+ |
} |
848 |
+ |
} |
849 |
+ |
|
850 |
+ |
/** |
851 |
+ |
* drainTo(c, n) empties up to n elements of queue into c |
852 |
+ |
*/ |
853 |
+ |
public void testDrainToN() { |
854 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
855 |
+ |
Thread t1 = new Thread(new Runnable() { |
856 |
+ |
public void run() { |
857 |
+ |
try { |
858 |
+ |
q.put(one); |
859 |
+ |
} catch (InterruptedException ie){ |
860 |
+ |
threadUnexpectedException(); |
861 |
+ |
} |
862 |
+ |
} |
863 |
+ |
}); |
864 |
+ |
Thread t2 = new Thread(new Runnable() { |
865 |
+ |
public void run() { |
866 |
+ |
try { |
867 |
+ |
q.put(two); |
868 |
+ |
} catch (InterruptedException ie){ |
869 |
+ |
threadUnexpectedException(); |
870 |
+ |
} |
871 |
+ |
} |
872 |
+ |
}); |
873 |
+ |
|
874 |
+ |
try { |
875 |
+ |
t1.start(); |
876 |
+ |
t2.start(); |
877 |
+ |
ArrayList l = new ArrayList(); |
878 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
879 |
+ |
q.drainTo(l, 1); |
880 |
+ |
assertTrue(l.size() == 1); |
881 |
+ |
q.drainTo(l, 1); |
882 |
+ |
assertTrue(l.size() == 2); |
883 |
+ |
assertTrue(l.contains(one)); |
884 |
+ |
assertTrue(l.contains(two)); |
885 |
+ |
t1.join(); |
886 |
+ |
t2.join(); |
887 |
+ |
} catch(Exception e){ |
888 |
+ |
unexpectedException(); |
889 |
+ |
} |
890 |
+ |
} |
891 |
+ |
|
892 |
+ |
|
893 |
|
} |