ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.27
Committed: Thu Oct 28 17:57:26 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +0 -18 lines
Log Message:
refactor testTakeFromEmpty into BlockingQueueTest

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