ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.21
Committed: Wed Aug 25 00:07:03 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
whitespace

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