ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.24
Committed: Wed Sep 29 12:33:48 2010 UTC (13 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.23: +4 -4 lines
Log Message:
Allow InterruptedException during initial checks in *TimedPollWithOffer

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.7 * 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 jsr166 1.10 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.2 import java.io.*;
14 dl 1.1
15 dl 1.3 public class SynchronousQueueTest extends JSR166TestCase {
16 dl 1.1
17     public static void main(String[] args) {
18 jsr166 1.21 junit.textui.TestRunner.run(suite());
19 dl 1.1 }
20    
21     public static Test suite() {
22 jsr166 1.14 return new TestSuite(SynchronousQueueTest.class);
23 dl 1.1 }
24    
25 dl 1.4 /**
26 dl 1.5 * A SynchronousQueue is both empty and full
27 dl 1.4 */
28 dl 1.1 public void testEmptyFull() {
29     SynchronousQueue q = new SynchronousQueue();
30     assertTrue(q.isEmpty());
31 jsr166 1.14 assertEquals(0, q.size());
32 dl 1.1 assertEquals(0, q.remainingCapacity());
33 dl 1.5 assertFalse(q.offer(zero));
34 dl 1.1 }
35    
36 dl 1.4 /**
37 dl 1.8 * A fair SynchronousQueue is both empty and full
38     */
39     public void testFairEmptyFull() {
40     SynchronousQueue q = new SynchronousQueue(true);
41     assertTrue(q.isEmpty());
42 jsr166 1.14 assertEquals(0, q.size());
43 dl 1.8 assertEquals(0, q.remainingCapacity());
44     assertFalse(q.offer(zero));
45     }
46    
47     /**
48 dl 1.5 * offer(null) throws NPE
49 dl 1.4 */
50     public void testOfferNull() {
51 jsr166 1.14 try {
52 dl 1.1 SynchronousQueue q = new SynchronousQueue();
53     q.offer(null);
54 dl 1.4 shouldThrow();
55 jsr166 1.13 } catch (NullPointerException success) {}
56 dl 1.1 }
57    
58 dl 1.4 /**
59 dl 1.6 * add(null) throws NPE
60     */
61     public void testAddNull() {
62 jsr166 1.14 try {
63 dl 1.6 SynchronousQueue q = new SynchronousQueue();
64     q.add(null);
65     shouldThrow();
66 jsr166 1.13 } catch (NullPointerException success) {}
67 dl 1.6 }
68    
69     /**
70 dl 1.5 * offer fails if no active taker
71 dl 1.4 */
72     public void testOffer() {
73 dl 1.1 SynchronousQueue q = new SynchronousQueue();
74 dl 1.5 assertFalse(q.offer(one));
75 dl 1.1 }
76    
77 dl 1.4 /**
78 dl 1.5 * add throws ISE if no active taker
79 dl 1.4 */
80     public void testAdd() {
81 jsr166 1.14 try {
82 dl 1.1 SynchronousQueue q = new SynchronousQueue();
83     assertEquals(0, q.remainingCapacity());
84 dl 1.5 q.add(one);
85     shouldThrow();
86 jsr166 1.13 } catch (IllegalStateException success) {}
87 dl 1.1 }
88    
89 dl 1.4 /**
90 dl 1.5 * addAll(null) throws NPE
91 dl 1.4 */
92     public void testAddAll1() {
93 dl 1.1 try {
94     SynchronousQueue q = new SynchronousQueue();
95     q.addAll(null);
96 dl 1.4 shouldThrow();
97 jsr166 1.13 } catch (NullPointerException success) {}
98 dl 1.1 }
99 dl 1.6
100     /**
101     * addAll(this) throws IAE
102     */
103     public void testAddAllSelf() {
104     try {
105     SynchronousQueue q = new SynchronousQueue();
106     q.addAll(q);
107     shouldThrow();
108 jsr166 1.13 } catch (IllegalArgumentException success) {}
109 dl 1.6 }
110    
111 dl 1.4 /**
112 dl 1.5 * addAll of a collection with null elements throws NPE
113 dl 1.4 */
114     public void testAddAll2() {
115 dl 1.1 try {
116     SynchronousQueue q = new SynchronousQueue();
117 dl 1.3 Integer[] ints = new Integer[1];
118 dl 1.1 q.addAll(Arrays.asList(ints));
119 dl 1.4 shouldThrow();
120 jsr166 1.13 } catch (NullPointerException success) {}
121 dl 1.1 }
122 jsr166 1.22
123 dl 1.4 /**
124 dl 1.5 * addAll throws ISE if no active taker
125 dl 1.4 */
126     public void testAddAll4() {
127 dl 1.1 try {
128     SynchronousQueue q = new SynchronousQueue();
129 dl 1.3 Integer[] ints = new Integer[1];
130     for (int i = 0; i < 1; ++i)
131 dl 1.1 ints[i] = new Integer(i);
132     q.addAll(Arrays.asList(ints));
133 dl 1.4 shouldThrow();
134 jsr166 1.13 } catch (IllegalStateException success) {}
135 dl 1.1 }
136    
137 dl 1.4 /**
138 dl 1.5 * put(null) throws NPE
139 dl 1.4 */
140 jsr166 1.13 public void testPutNull() throws InterruptedException {
141 jsr166 1.14 try {
142 dl 1.1 SynchronousQueue q = new SynchronousQueue();
143     q.put(null);
144 dl 1.4 shouldThrow();
145 jsr166 1.13 } catch (NullPointerException success) {}
146 dl 1.1 }
147    
148 dl 1.4 /**
149 dl 1.5 * put blocks interruptibly if no active taker
150 dl 1.4 */
151 jsr166 1.13 public void testBlockingPut() throws InterruptedException {
152 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
153     public void realRun() throws InterruptedException {
154 jsr166 1.13 SynchronousQueue q = new SynchronousQueue();
155     q.put(zero);
156     }});
157    
158 dl 1.1 t.start();
159 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
160     t.interrupt();
161     t.join();
162 dl 1.1 }
163    
164 dl 1.4 /**
165 jsr166 1.10 * put blocks waiting for take
166 dl 1.4 */
167 jsr166 1.13 public void testPutWithTake() throws InterruptedException {
168 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
169 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
170     public void realRun() throws InterruptedException {
171 jsr166 1.13 int added = 0;
172     try {
173 jsr166 1.19 while (true) {
174     q.put(added);
175     ++added;
176     }
177 jsr166 1.13 } catch (InterruptedException success) {
178 jsr166 1.23 assertEquals(1, added);
179 dl 1.1 }
180 jsr166 1.13 }});
181    
182     t.start();
183     Thread.sleep(SHORT_DELAY_MS);
184 jsr166 1.19 assertEquals(0, q.take());
185 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
186     t.interrupt();
187     t.join();
188 dl 1.1 }
189    
190 dl 1.4 /**
191 dl 1.5 * timed offer times out if elements not taken
192 dl 1.4 */
193 jsr166 1.13 public void testTimedOffer() throws InterruptedException {
194 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
195 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
196     public void realRun() throws InterruptedException {
197 jsr166 1.17 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
198 jsr166 1.13 q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
199     }});
200 jsr166 1.10
201 jsr166 1.13 t.start();
202     Thread.sleep(SMALL_DELAY_MS);
203     t.interrupt();
204     t.join();
205 dl 1.1 }
206    
207    
208 dl 1.4 /**
209 dl 1.5 * take blocks interruptibly when empty
210 dl 1.4 */
211 jsr166 1.13 public void testTakeFromEmpty() throws InterruptedException {
212 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
213 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
214     public void realRun() throws InterruptedException {
215 jsr166 1.13 q.take();
216     }});
217    
218     t.start();
219     Thread.sleep(SHORT_DELAY_MS);
220     t.interrupt();
221     t.join();
222 dl 1.1 }
223    
224 dl 1.8
225     /**
226     * put blocks interruptibly if no active taker
227     */
228 jsr166 1.13 public void testFairBlockingPut() throws InterruptedException {
229 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
230     public void realRun() throws InterruptedException {
231 jsr166 1.13 SynchronousQueue q = new SynchronousQueue(true);
232     q.put(zero);
233     }});
234    
235 dl 1.8 t.start();
236 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
237     t.interrupt();
238     t.join();
239 dl 1.8 }
240    
241     /**
242 jsr166 1.10 * put blocks waiting for take
243 dl 1.8 */
244 jsr166 1.13 public void testFairPutWithTake() throws InterruptedException {
245 dl 1.8 final SynchronousQueue q = new SynchronousQueue(true);
246 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
247     public void realRun() throws InterruptedException {
248 jsr166 1.13 int added = 0;
249     try {
250 jsr166 1.19 while (true) {
251     q.put(added);
252     ++added;
253     }
254 jsr166 1.13 } catch (InterruptedException success) {
255 jsr166 1.23 assertEquals(1, added);
256 dl 1.8 }
257 jsr166 1.13 }});
258    
259     t.start();
260     Thread.sleep(SHORT_DELAY_MS);
261 jsr166 1.19 assertEquals(0, q.take());
262 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
263     t.interrupt();
264     t.join();
265 dl 1.8 }
266    
267     /**
268     * timed offer times out if elements not taken
269     */
270 jsr166 1.13 public void testFairTimedOffer() throws InterruptedException {
271 dl 1.8 final SynchronousQueue q = new SynchronousQueue(true);
272 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
273     public void realRun() throws InterruptedException {
274 jsr166 1.19 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
275 jsr166 1.13 q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
276     }});
277 jsr166 1.10
278 jsr166 1.13 t.start();
279     Thread.sleep(SMALL_DELAY_MS);
280     t.interrupt();
281     t.join();
282 dl 1.8 }
283    
284    
285     /**
286     * take blocks interruptibly when empty
287     */
288 jsr166 1.13 public void testFairTakeFromEmpty() throws InterruptedException {
289 dl 1.8 final SynchronousQueue q = new SynchronousQueue(true);
290 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
291     public void realRun() throws InterruptedException {
292 jsr166 1.13 q.take();
293     }});
294    
295     t.start();
296     Thread.sleep(SHORT_DELAY_MS);
297     t.interrupt();
298     t.join();
299 dl 1.8 }
300    
301 dl 1.4 /**
302 dl 1.5 * poll fails unless active taker
303 dl 1.4 */
304     public void testPoll() {
305 dl 1.1 SynchronousQueue q = new SynchronousQueue();
306 jsr166 1.14 assertNull(q.poll());
307 dl 1.1 }
308    
309 dl 1.4 /**
310 dl 1.5 * timed pool with zero timeout times out if no active taker
311 dl 1.4 */
312 jsr166 1.13 public void testTimedPoll0() throws InterruptedException {
313     SynchronousQueue q = new SynchronousQueue();
314     assertNull(q.poll(0, MILLISECONDS));
315 dl 1.1 }
316    
317 dl 1.4 /**
318 dl 1.5 * timed pool with nonzero timeout times out if no active taker
319 dl 1.4 */
320 jsr166 1.13 public void testTimedPoll() throws InterruptedException {
321     SynchronousQueue q = new SynchronousQueue();
322     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
323 dl 1.1 }
324    
325 dl 1.4 /**
326 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
327     * returning timeout status
328 dl 1.4 */
329 jsr166 1.13 public void testInterruptedTimedPoll() throws InterruptedException {
330 jsr166 1.15 final SynchronousQueue q = new SynchronousQueue();
331 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
332     public void realRun() throws InterruptedException {
333 jsr166 1.13 q.poll(SMALL_DELAY_MS, MILLISECONDS);
334     }});
335    
336 dl 1.1 t.start();
337 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
338     t.interrupt();
339     t.join();
340 dl 1.1 }
341    
342 dl 1.4 /**
343 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
344     * on interruption throws
345 dl 1.4 */
346 jsr166 1.13 public void testTimedPollWithOffer() throws InterruptedException {
347 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
348 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
349     public void realRun() throws InterruptedException {
350 jsr166 1.13 try {
351 dl 1.24 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
352     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
353 jsr166 1.13 q.poll(LONG_DELAY_MS, MILLISECONDS);
354 jsr166 1.16 shouldThrow();
355 jsr166 1.13 } catch (InterruptedException success) {}
356     }});
357    
358     t.start();
359     Thread.sleep(SMALL_DELAY_MS);
360     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
361     t.interrupt();
362     t.join();
363 jsr166 1.10 }
364 dl 1.8
365     /**
366     * Interrupted timed poll throws InterruptedException instead of
367     * returning timeout status
368     */
369 jsr166 1.13 public void testFairInterruptedTimedPoll() throws InterruptedException {
370 jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
371     public void realRun() throws InterruptedException {
372 jsr166 1.13 SynchronousQueue q = new SynchronousQueue(true);
373     q.poll(SMALL_DELAY_MS, MILLISECONDS);
374     }});
375    
376 dl 1.8 t.start();
377 jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
378     t.interrupt();
379     t.join();
380 dl 1.8 }
381    
382     /**
383     * timed poll before a delayed offer fails; after offer succeeds;
384     * on interruption throws
385     */
386 jsr166 1.13 public void testFairTimedPollWithOffer() throws InterruptedException {
387 dl 1.8 final SynchronousQueue q = new SynchronousQueue(true);
388 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
389     public void realRun() throws InterruptedException {
390 jsr166 1.13 try {
391 dl 1.24 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
392     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
393 jsr166 1.13 q.poll(LONG_DELAY_MS, MILLISECONDS);
394     threadShouldThrow();
395     } catch (InterruptedException success) {}
396     }});
397    
398     t.start();
399     Thread.sleep(SMALL_DELAY_MS);
400     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
401     t.interrupt();
402     t.join();
403 jsr166 1.10 }
404 dl 1.1
405    
406 dl 1.4 /**
407 dl 1.5 * peek returns null
408 dl 1.4 */
409     public void testPeek() {
410 dl 1.1 SynchronousQueue q = new SynchronousQueue();
411 jsr166 1.14 assertNull(q.peek());
412 dl 1.1 }
413    
414 dl 1.4 /**
415 dl 1.5 * element throws NSEE
416 dl 1.4 */
417     public void testElement() {
418 dl 1.1 SynchronousQueue q = new SynchronousQueue();
419     try {
420     q.element();
421 dl 1.4 shouldThrow();
422 jsr166 1.13 } catch (NoSuchElementException success) {}
423 dl 1.1 }
424    
425 dl 1.4 /**
426 dl 1.5 * remove throws NSEE if no active taker
427 dl 1.4 */
428     public void testRemove() {
429 dl 1.1 SynchronousQueue q = new SynchronousQueue();
430     try {
431     q.remove();
432 dl 1.4 shouldThrow();
433 jsr166 1.16 } catch (NoSuchElementException success) {}
434 dl 1.1 }
435    
436 dl 1.4 /**
437 dl 1.5 * remove(x) returns false
438 dl 1.4 */
439     public void testRemoveElement() {
440 dl 1.1 SynchronousQueue q = new SynchronousQueue();
441 dl 1.5 assertFalse(q.remove(zero));
442 dl 1.2 assertTrue(q.isEmpty());
443 dl 1.1 }
444 jsr166 1.10
445 dl 1.4 /**
446 dl 1.5 * contains returns false
447 dl 1.4 */
448     public void testContains() {
449 dl 1.1 SynchronousQueue q = new SynchronousQueue();
450 dl 1.5 assertFalse(q.contains(zero));
451 dl 1.1 }
452    
453 dl 1.4 /**
454 dl 1.5 * clear ensures isEmpty
455 dl 1.4 */
456     public void testClear() {
457 dl 1.1 SynchronousQueue q = new SynchronousQueue();
458     q.clear();
459     assertTrue(q.isEmpty());
460     }
461    
462 dl 1.4 /**
463 dl 1.5 * containsAll returns false unless empty
464 dl 1.4 */
465     public void testContainsAll() {
466 dl 1.1 SynchronousQueue q = new SynchronousQueue();
467     Integer[] empty = new Integer[0];
468 dl 1.5 assertTrue(q.containsAll(Arrays.asList(empty)));
469     Integer[] ints = new Integer[1]; ints[0] = zero;
470 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
471     }
472    
473 dl 1.4 /**
474 dl 1.5 * retainAll returns false
475 dl 1.4 */
476     public void testRetainAll() {
477 dl 1.1 SynchronousQueue q = new SynchronousQueue();
478     Integer[] empty = new Integer[0];
479 dl 1.5 assertFalse(q.retainAll(Arrays.asList(empty)));
480     Integer[] ints = new Integer[1]; ints[0] = zero;
481     assertFalse(q.retainAll(Arrays.asList(ints)));
482 dl 1.1 }
483    
484 dl 1.4 /**
485 dl 1.5 * removeAll returns false
486 dl 1.4 */
487     public void testRemoveAll() {
488 dl 1.1 SynchronousQueue q = new SynchronousQueue();
489     Integer[] empty = new Integer[0];
490 dl 1.5 assertFalse(q.removeAll(Arrays.asList(empty)));
491     Integer[] ints = new Integer[1]; ints[0] = zero;
492 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
493     }
494    
495    
496 dl 1.4 /**
497 dl 1.5 * toArray is empty
498 dl 1.4 */
499     public void testToArray() {
500 dl 1.1 SynchronousQueue q = new SynchronousQueue();
501 jsr166 1.14 Object[] o = q.toArray();
502 dl 1.1 assertEquals(o.length, 0);
503     }
504    
505 dl 1.4 /**
506 dl 1.5 * toArray(a) is nulled at position 0
507 dl 1.4 */
508     public void testToArray2() {
509 dl 1.1 SynchronousQueue q = new SynchronousQueue();
510 jsr166 1.14 Integer[] ints = new Integer[1];
511 dl 1.1 assertNull(ints[0]);
512     }
513 jsr166 1.10
514 dl 1.4 /**
515 dl 1.6 * toArray(null) throws NPE
516     */
517     public void testToArray_BadArg() {
518 jsr166 1.18 SynchronousQueue q = new SynchronousQueue();
519 jsr166 1.14 try {
520     Object o[] = q.toArray(null);
521     shouldThrow();
522     } catch (NullPointerException success) {}
523 dl 1.6 }
524    
525    
526     /**
527 dl 1.5 * iterator does not traverse any elements
528 dl 1.4 */
529     public void testIterator() {
530 dl 1.1 SynchronousQueue q = new SynchronousQueue();
531 jsr166 1.14 Iterator it = q.iterator();
532 dl 1.1 assertFalse(it.hasNext());
533     try {
534     Object x = it.next();
535 dl 1.4 shouldThrow();
536 jsr166 1.13 } catch (NoSuchElementException success) {}
537 dl 1.1 }
538    
539 dl 1.4 /**
540 dl 1.5 * iterator remove throws ISE
541 dl 1.4 */
542     public void testIteratorRemove() {
543 dl 1.1 SynchronousQueue q = new SynchronousQueue();
544 jsr166 1.14 Iterator it = q.iterator();
545 dl 1.1 try {
546     it.remove();
547 dl 1.4 shouldThrow();
548 jsr166 1.13 } catch (IllegalStateException success) {}
549 dl 1.1 }
550    
551 dl 1.4 /**
552 dl 1.5 * toString returns a non-null string
553 dl 1.4 */
554     public void testToString() {
555 dl 1.1 SynchronousQueue q = new SynchronousQueue();
556     String s = q.toString();
557 dl 1.5 assertNotNull(s);
558 jsr166 1.10 }
559 dl 1.1
560    
561 dl 1.4 /**
562 dl 1.5 * offer transfers elements across Executor tasks
563 dl 1.4 */
564 dl 1.1 public void testOfferInExecutor() {
565     final SynchronousQueue q = new SynchronousQueue();
566     ExecutorService executor = Executors.newFixedThreadPool(2);
567    
568 jsr166 1.13 executor.execute(new CheckedRunnable() {
569 jsr166 1.14 public void realRun() throws InterruptedException {
570 jsr166 1.19 assertFalse(q.offer(one));
571     assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
572     assertEquals(0, q.remainingCapacity());
573 jsr166 1.13 }});
574    
575     executor.execute(new CheckedRunnable() {
576 jsr166 1.14 public void realRun() throws InterruptedException {
577 jsr166 1.13 Thread.sleep(SMALL_DELAY_MS);
578 jsr166 1.19 assertSame(one, q.take());
579 jsr166 1.13 }});
580 jsr166 1.10
581 dl 1.3 joinPool(executor);
582 dl 1.1 }
583    
584 dl 1.4 /**
585 dl 1.5 * poll retrieves elements across Executor threads
586 dl 1.4 */
587 dl 1.1 public void testPollInExecutor() {
588     final SynchronousQueue q = new SynchronousQueue();
589     ExecutorService executor = Executors.newFixedThreadPool(2);
590 jsr166 1.13 executor.execute(new CheckedRunnable() {
591 jsr166 1.14 public void realRun() throws InterruptedException {
592 jsr166 1.19 assertNull(q.poll());
593     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
594     assertTrue(q.isEmpty());
595 jsr166 1.13 }});
596    
597     executor.execute(new CheckedRunnable() {
598 jsr166 1.14 public void realRun() throws InterruptedException {
599 jsr166 1.19 Thread.sleep(SHORT_DELAY_MS);
600     q.put(one);
601 jsr166 1.13 }});
602 jsr166 1.10
603 dl 1.3 joinPool(executor);
604 dl 1.2 }
605    
606 dl 1.4 /**
607 dl 1.5 * a deserialized serialized queue is usable
608 dl 1.4 */
609 jsr166 1.13 public void testSerialization() throws Exception {
610 dl 1.2 SynchronousQueue q = new SynchronousQueue();
611 jsr166 1.13 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
612     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
613     out.writeObject(q);
614     out.close();
615    
616     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
617     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
618     SynchronousQueue r = (SynchronousQueue)in.readObject();
619     assertEquals(q.size(), r.size());
620     while (!q.isEmpty())
621     assertEquals(q.remove(), r.remove());
622 dl 1.1 }
623 dl 1.6
624     /**
625     * drainTo(null) throws NPE
626 jsr166 1.10 */
627 dl 1.6 public void testDrainToNull() {
628     SynchronousQueue q = new SynchronousQueue();
629     try {
630     q.drainTo(null);
631     shouldThrow();
632 jsr166 1.13 } catch (NullPointerException success) {}
633 dl 1.6 }
634    
635     /**
636     * drainTo(this) throws IAE
637 jsr166 1.10 */
638 dl 1.6 public void testDrainToSelf() {
639     SynchronousQueue q = new SynchronousQueue();
640     try {
641     q.drainTo(q);
642     shouldThrow();
643 jsr166 1.13 } catch (IllegalArgumentException success) {}
644 dl 1.6 }
645    
646     /**
647     * drainTo(c) of empty queue doesn't transfer elements
648 jsr166 1.10 */
649 dl 1.6 public void testDrainTo() {
650     SynchronousQueue q = new SynchronousQueue();
651     ArrayList l = new ArrayList();
652     q.drainTo(l);
653     assertEquals(q.size(), 0);
654     assertEquals(l.size(), 0);
655     }
656    
657     /**
658     * drainTo empties queue, unblocking a waiting put.
659 jsr166 1.10 */
660 jsr166 1.13 public void testDrainToWithActivePut() throws InterruptedException {
661 dl 1.6 final SynchronousQueue q = new SynchronousQueue();
662 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
663     public void realRun() throws InterruptedException {
664 jsr166 1.13 q.put(new Integer(1));
665     }});
666    
667     t.start();
668     ArrayList l = new ArrayList();
669     Thread.sleep(SHORT_DELAY_MS);
670     q.drainTo(l);
671     assertTrue(l.size() <= 1);
672     if (l.size() > 0)
673     assertEquals(l.get(0), new Integer(1));
674     t.join();
675     assertTrue(l.size() <= 1);
676 dl 1.6 }
677    
678     /**
679     * drainTo(null, n) throws NPE
680 jsr166 1.10 */
681 dl 1.6 public void testDrainToNullN() {
682     SynchronousQueue q = new SynchronousQueue();
683     try {
684     q.drainTo(null, 0);
685     shouldThrow();
686 jsr166 1.13 } catch (NullPointerException success) {}
687 dl 1.6 }
688    
689     /**
690     * drainTo(this, n) throws IAE
691 jsr166 1.10 */
692 dl 1.6 public void testDrainToSelfN() {
693     SynchronousQueue q = new SynchronousQueue();
694     try {
695     q.drainTo(q, 0);
696     shouldThrow();
697 jsr166 1.16 } catch (IllegalArgumentException success) {}
698 dl 1.6 }
699    
700     /**
701     * drainTo(c, n) empties up to n elements of queue into c
702 jsr166 1.10 */
703 jsr166 1.13 public void testDrainToN() throws InterruptedException {
704 dl 1.6 final SynchronousQueue q = new SynchronousQueue();
705 jsr166 1.14 Thread t1 = new Thread(new CheckedRunnable() {
706     public void realRun() throws InterruptedException {
707 jsr166 1.13 q.put(one);
708     }});
709    
710 jsr166 1.14 Thread t2 = new Thread(new CheckedRunnable() {
711     public void realRun() throws InterruptedException {
712 jsr166 1.13 q.put(two);
713     }});
714 dl 1.6
715 jsr166 1.13 t1.start();
716     t2.start();
717     ArrayList l = new ArrayList();
718     Thread.sleep(SHORT_DELAY_MS);
719     q.drainTo(l, 1);
720 jsr166 1.23 assertEquals(1, l.size());
721 jsr166 1.13 q.drainTo(l, 1);
722 jsr166 1.23 assertEquals(2, l.size());
723 jsr166 1.13 assertTrue(l.contains(one));
724     assertTrue(l.contains(two));
725     t1.join();
726     t2.join();
727 dl 1.6 }
728    
729 dl 1.1 }