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.*; |
33 |
|
} |
34 |
|
|
35 |
|
/** |
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() { |
55 |
|
} |
56 |
|
|
57 |
|
/** |
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() { |
97 |
|
} |
98 |
|
catch (NullPointerException success) {} |
99 |
|
} |
100 |
+ |
|
101 |
+ |
/** |
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 |
|
*/ |
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 |
+ |
* 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 |
|
*/ |
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){ |
505 |
+ |
unexpectedException(); |
506 |
+ |
} |
507 |
+ |
} |
508 |
+ |
|
509 |
|
|
510 |
|
/** |
511 |
|
* peek returns null |
618 |
|
} |
619 |
|
|
620 |
|
/** |
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() { |
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 |
|
} |