ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.34
Committed: Sat May 21 06:24:33 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +6 -8 lines
Log Message:
various test improvements

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