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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
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() { |
51 |
|
SynchronousQueue q = new SynchronousQueue(); |
52 |
|
q.offer(null); |
53 |
|
shouldThrow(); |
54 |
< |
} catch (NullPointerException success) { } |
54 |
> |
} catch (NullPointerException success) { } |
55 |
|
} |
56 |
|
|
57 |
|
/** |
62 |
|
SynchronousQueue q = new SynchronousQueue(); |
63 |
|
q.add(null); |
64 |
|
shouldThrow(); |
65 |
< |
} catch (NullPointerException success) { } |
65 |
> |
} catch (NullPointerException success) { } |
66 |
|
} |
67 |
|
|
68 |
|
/** |
83 |
|
q.add(one); |
84 |
|
shouldThrow(); |
85 |
|
} catch (IllegalStateException success){ |
86 |
< |
} |
86 |
> |
} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
145 |
|
SynchronousQueue q = new SynchronousQueue(); |
146 |
|
q.put(null); |
147 |
|
shouldThrow(); |
148 |
< |
} |
148 |
> |
} |
149 |
|
catch (NullPointerException success){ |
150 |
< |
} |
150 |
> |
} |
151 |
|
catch (InterruptedException ie) { |
152 |
|
unexpectedException(); |
153 |
|
} |
164 |
|
q.put(zero); |
165 |
|
threadShouldThrow(); |
166 |
|
} catch (InterruptedException ie){ |
167 |
< |
} |
167 |
> |
} |
168 |
|
}}); |
169 |
|
t.start(); |
170 |
< |
try { |
171 |
< |
Thread.sleep(SHORT_DELAY_MS); |
170 |
> |
try { |
171 |
> |
Thread.sleep(SHORT_DELAY_MS); |
172 |
|
t.interrupt(); |
173 |
|
t.join(); |
174 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* put blocks waiting for take |
181 |
> |
* put blocks waiting for take |
182 |
|
*/ |
183 |
|
public void testPutWithTake() { |
184 |
|
final SynchronousQueue q = new SynchronousQueue(); |
227 |
|
} catch (InterruptedException success){} |
228 |
|
} |
229 |
|
}); |
230 |
< |
|
230 |
> |
|
231 |
|
try { |
232 |
|
t.start(); |
233 |
|
Thread.sleep(SMALL_DELAY_MS); |
249 |
|
try { |
250 |
|
q.take(); |
251 |
|
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success){ } |
252 |
> |
} catch (InterruptedException success){ } |
253 |
> |
} |
254 |
> |
}); |
255 |
> |
try { |
256 |
> |
t.start(); |
257 |
> |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
t.interrupt(); |
259 |
> |
t.join(); |
260 |
> |
} catch (Exception e){ |
261 |
> |
unexpectedException(); |
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 { |
389 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
390 |
|
} catch (InterruptedException e){ |
391 |
|
unexpectedException(); |
392 |
< |
} |
392 |
> |
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
401 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
|
} catch (InterruptedException e){ |
403 |
|
unexpectedException(); |
404 |
< |
} |
404 |
> |
} |
405 |
|
} |
406 |
|
|
407 |
|
/** |
415 |
|
SynchronousQueue q = new SynchronousQueue(); |
416 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
417 |
|
} catch (InterruptedException success){ |
418 |
< |
} |
418 |
> |
} |
419 |
|
}}); |
420 |
|
t.start(); |
421 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
421 |
> |
try { |
422 |
> |
Thread.sleep(SHORT_DELAY_MS); |
423 |
|
t.interrupt(); |
424 |
|
t.join(); |
425 |
|
} |
441 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
442 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
443 |
|
threadShouldThrow(); |
444 |
< |
} catch (InterruptedException success) { } |
444 |
> |
} catch (InterruptedException success) { } |
445 |
|
} |
446 |
|
}); |
447 |
|
try { |
453 |
|
} catch (Exception e){ |
454 |
|
unexpectedException(); |
455 |
|
} |
456 |
< |
} |
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 |
|
/** |
536 |
|
q.remove(); |
537 |
|
shouldThrow(); |
538 |
|
} catch (NoSuchElementException success){ |
539 |
< |
} |
539 |
> |
} |
540 |
|
} |
541 |
|
|
542 |
|
/** |
547 |
|
assertFalse(q.remove(zero)); |
548 |
|
assertTrue(q.isEmpty()); |
549 |
|
} |
550 |
< |
|
550 |
> |
|
551 |
|
/** |
552 |
|
* contains returns false |
553 |
|
*/ |
616 |
|
Integer[] ints = new Integer[1]; |
617 |
|
assertNull(ints[0]); |
618 |
|
} |
619 |
< |
|
619 |
> |
|
620 |
|
/** |
621 |
|
* toArray(null) throws NPE |
622 |
|
*/ |
663 |
|
SynchronousQueue q = new SynchronousQueue(); |
664 |
|
String s = q.toString(); |
665 |
|
assertNotNull(s); |
666 |
< |
} |
666 |
> |
} |
667 |
|
|
668 |
|
|
669 |
|
/** |
698 |
|
} |
699 |
|
} |
700 |
|
}); |
701 |
< |
|
701 |
> |
|
702 |
|
joinPool(executor); |
703 |
|
|
704 |
|
} |
733 |
|
} |
734 |
|
} |
735 |
|
}); |
736 |
< |
|
736 |
> |
|
737 |
|
joinPool(executor); |
738 |
|
} |
739 |
|
|
752 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
753 |
|
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
754 |
|
assertEquals(q.size(), r.size()); |
755 |
< |
while (!q.isEmpty()) |
755 |
> |
while (!q.isEmpty()) |
756 |
|
assertEquals(q.remove(), r.remove()); |
757 |
|
} catch(Exception e){ |
758 |
+ |
e.printStackTrace(); |
759 |
|
unexpectedException(); |
760 |
|
} |
761 |
|
} |
762 |
|
|
763 |
|
/** |
764 |
|
* drainTo(null) throws NPE |
765 |
< |
*/ |
765 |
> |
*/ |
766 |
|
public void testDrainToNull() { |
767 |
|
SynchronousQueue q = new SynchronousQueue(); |
768 |
|
try { |
774 |
|
|
775 |
|
/** |
776 |
|
* drainTo(this) throws IAE |
777 |
< |
*/ |
777 |
> |
*/ |
778 |
|
public void testDrainToSelf() { |
779 |
|
SynchronousQueue q = new SynchronousQueue(); |
780 |
|
try { |
786 |
|
|
787 |
|
/** |
788 |
|
* drainTo(c) of empty queue doesn't transfer elements |
789 |
< |
*/ |
789 |
> |
*/ |
790 |
|
public void testDrainTo() { |
791 |
|
SynchronousQueue q = new SynchronousQueue(); |
792 |
|
ArrayList l = new ArrayList(); |
797 |
|
|
798 |
|
/** |
799 |
|
* drainTo empties queue, unblocking a waiting put. |
800 |
< |
*/ |
800 |
> |
*/ |
801 |
|
public void testDrainToWithActivePut() { |
802 |
|
final SynchronousQueue q = new SynchronousQueue(); |
803 |
|
Thread t = new Thread(new Runnable() { |
804 |
|
public void run() { |
805 |
|
try { |
806 |
|
q.put(new Integer(1)); |
807 |
< |
} catch (InterruptedException ie){ |
807 |
> |
} catch (InterruptedException ie){ |
808 |
|
threadUnexpectedException(); |
809 |
|
} |
810 |
|
} |
826 |
|
|
827 |
|
/** |
828 |
|
* drainTo(null, n) throws NPE |
829 |
< |
*/ |
829 |
> |
*/ |
830 |
|
public void testDrainToNullN() { |
831 |
|
SynchronousQueue q = new SynchronousQueue(); |
832 |
|
try { |
838 |
|
|
839 |
|
/** |
840 |
|
* drainTo(this, n) throws IAE |
841 |
< |
*/ |
841 |
> |
*/ |
842 |
|
public void testDrainToSelfN() { |
843 |
|
SynchronousQueue q = new SynchronousQueue(); |
844 |
|
try { |
850 |
|
|
851 |
|
/** |
852 |
|
* drainTo(c, n) empties up to n elements of queue into c |
853 |
< |
*/ |
853 |
> |
*/ |
854 |
|
public void testDrainToN() { |
855 |
|
final SynchronousQueue q = new SynchronousQueue(); |
856 |
|
Thread t1 = new Thread(new Runnable() { |
857 |
|
public void run() { |
858 |
|
try { |
859 |
|
q.put(one); |
860 |
< |
} catch (InterruptedException ie){ |
860 |
> |
} catch (InterruptedException ie){ |
861 |
|
threadUnexpectedException(); |
862 |
|
} |
863 |
|
} |
866 |
|
public void run() { |
867 |
|
try { |
868 |
|
q.put(two); |
869 |
< |
} catch (InterruptedException ie){ |
869 |
> |
} catch (InterruptedException ie){ |
870 |
|
threadUnexpectedException(); |
871 |
|
} |
872 |
|
} |