ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.43
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +0 -1 lines
Log Message:
remove unused imports

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 jsr166 1.37 import java.util.Arrays;
11     import java.util.ArrayList;
12     import java.util.Collection;
13     import java.util.Iterator;
14     import java.util.NoSuchElementException;
15     import java.util.concurrent.BlockingQueue;
16     import java.util.concurrent.CountDownLatch;
17     import java.util.concurrent.Executors;
18     import java.util.concurrent.ExecutorService;
19     import java.util.concurrent.SynchronousQueue;
20 jsr166 1.13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 dl 1.1
22 dl 1.3 public class SynchronousQueueTest extends JSR166TestCase {
23 dl 1.1
24 jsr166 1.25 public static class Fair extends BlockingQueueTest {
25     protected BlockingQueue emptyCollection() {
26     return new SynchronousQueue(true);
27     }
28     }
29    
30     public static class NonFair extends BlockingQueueTest {
31     protected BlockingQueue emptyCollection() {
32     return new SynchronousQueue(false);
33     }
34     }
35    
36 dl 1.1 public static void main(String[] args) {
37 jsr166 1.21 junit.textui.TestRunner.run(suite());
38 dl 1.1 }
39    
40     public static Test suite() {
41 jsr166 1.25 return newTestSuite(SynchronousQueueTest.class,
42     new Fair().testSuite(),
43     new NonFair().testSuite());
44 dl 1.1 }
45    
46 dl 1.4 /**
47 jsr166 1.29 * Any SynchronousQueue is both empty and full
48 dl 1.4 */
49 jsr166 1.35 public void testEmptyFull() { testEmptyFull(false); }
50     public void testEmptyFull_fair() { testEmptyFull(true); }
51     public void testEmptyFull(boolean fair) {
52     final SynchronousQueue q = new SynchronousQueue(fair);
53 dl 1.1 assertTrue(q.isEmpty());
54 jsr166 1.14 assertEquals(0, q.size());
55 dl 1.1 assertEquals(0, q.remainingCapacity());
56 dl 1.5 assertFalse(q.offer(zero));
57 dl 1.1 }
58    
59 dl 1.4 /**
60 dl 1.5 * offer fails if no active taker
61 dl 1.4 */
62 jsr166 1.36 public void testOffer() { testOffer(false); }
63     public void testOffer_fair() { testOffer(true); }
64     public void testOffer(boolean fair) {
65     SynchronousQueue q = new SynchronousQueue(fair);
66 dl 1.5 assertFalse(q.offer(one));
67 dl 1.1 }
68    
69 dl 1.4 /**
70 jsr166 1.36 * add throws IllegalStateException if no active taker
71 dl 1.4 */
72 jsr166 1.36 public void testAdd() { testAdd(false); }
73     public void testAdd_fair() { testAdd(true); }
74     public void testAdd(boolean fair) {
75     SynchronousQueue q = new SynchronousQueue(fair);
76     assertEquals(0, q.remainingCapacity());
77 jsr166 1.14 try {
78 dl 1.5 q.add(one);
79     shouldThrow();
80 jsr166 1.13 } catch (IllegalStateException success) {}
81 dl 1.1 }
82    
83 dl 1.4 /**
84 jsr166 1.36 * addAll(this) throws IllegalArgumentException
85 dl 1.6 */
86 jsr166 1.36 public void testAddAll_self() { testAddAll_self(false); }
87     public void testAddAll_self_fair() { testAddAll_self(true); }
88     public void testAddAll_self(boolean fair) {
89     SynchronousQueue q = new SynchronousQueue(fair);
90 dl 1.6 try {
91     q.addAll(q);
92     shouldThrow();
93 jsr166 1.13 } catch (IllegalArgumentException success) {}
94 dl 1.6 }
95    
96 dl 1.4 /**
97 dl 1.5 * addAll throws ISE if no active taker
98 dl 1.4 */
99 jsr166 1.36 public void testAddAll_ISE() { testAddAll_ISE(false); }
100     public void testAddAll_ISE_fair() { testAddAll_ISE(true); }
101     public void testAddAll_ISE(boolean fair) {
102     SynchronousQueue q = new SynchronousQueue(fair);
103     Integer[] ints = new Integer[1];
104     for (int i = 0; i < ints.length; i++)
105     ints[i] = i;
106     Collection<Integer> coll = Arrays.asList(ints);
107 dl 1.1 try {
108 jsr166 1.36 q.addAll(coll);
109 dl 1.4 shouldThrow();
110 jsr166 1.13 } catch (IllegalStateException success) {}
111 dl 1.1 }
112    
113 dl 1.4 /**
114 dl 1.5 * put blocks interruptibly if no active taker
115 dl 1.4 */
116 jsr166 1.35 public void testBlockingPut() { testBlockingPut(false); }
117     public void testBlockingPut_fair() { testBlockingPut(true); }
118     public void testBlockingPut(boolean fair) {
119     final SynchronousQueue q = new SynchronousQueue(fair);
120     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
121     Thread t = newStartedThread(new CheckedRunnable() {
122 jsr166 1.14 public void realRun() throws InterruptedException {
123 jsr166 1.35 Thread.currentThread().interrupt();
124     try {
125     q.put(99);
126     shouldThrow();
127     } catch (InterruptedException success) {}
128     assertFalse(Thread.interrupted());
129    
130     pleaseInterrupt.countDown();
131     try {
132     q.put(99);
133     shouldThrow();
134     } catch (InterruptedException success) {}
135     assertFalse(Thread.interrupted());
136 jsr166 1.13 }});
137    
138 jsr166 1.35 await(pleaseInterrupt);
139     assertThreadStaysAlive(t);
140 jsr166 1.13 t.interrupt();
141 jsr166 1.35 awaitTermination(t);
142     assertEquals(0, q.remainingCapacity());
143 dl 1.1 }
144    
145 dl 1.4 /**
146 jsr166 1.35 * put blocks interruptibly waiting for take
147 dl 1.4 */
148 jsr166 1.35 public void testPutWithTake() { testPutWithTake(false); }
149     public void testPutWithTake_fair() { testPutWithTake(true); }
150     public void testPutWithTake(boolean fair) {
151     final SynchronousQueue q = new SynchronousQueue(fair);
152     final CountDownLatch pleaseTake = new CountDownLatch(1);
153     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
154     Thread t = newStartedThread(new CheckedRunnable() {
155 jsr166 1.14 public void realRun() throws InterruptedException {
156 jsr166 1.35 pleaseTake.countDown();
157     q.put(one);
158    
159     pleaseInterrupt.countDown();
160 jsr166 1.13 try {
161 jsr166 1.35 q.put(99);
162     shouldThrow();
163     } catch (InterruptedException success) {}
164     assertFalse(Thread.interrupted());
165 jsr166 1.13 }});
166    
167 jsr166 1.35 await(pleaseTake);
168 jsr166 1.40 assertEquals(0, q.remainingCapacity());
169 jsr166 1.35 try { assertSame(one, q.take()); }
170     catch (InterruptedException e) { threadUnexpectedException(e); }
171    
172     await(pleaseInterrupt);
173     assertThreadStaysAlive(t);
174 jsr166 1.13 t.interrupt();
175 jsr166 1.35 awaitTermination(t);
176 jsr166 1.40 assertEquals(0, q.remainingCapacity());
177 dl 1.1 }
178    
179 dl 1.4 /**
180 dl 1.5 * timed offer times out if elements not taken
181 dl 1.4 */
182 jsr166 1.35 public void testTimedOffer() { testTimedOffer(false); }
183     public void testTimedOffer_fair() { testTimedOffer(true); }
184     public void testTimedOffer(boolean fair) {
185     final SynchronousQueue q = new SynchronousQueue(fair);
186 jsr166 1.29 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
187     Thread t = newStartedThread(new CheckedRunnable() {
188 jsr166 1.14 public void realRun() throws InterruptedException {
189 jsr166 1.34 long startTime = System.nanoTime();
190     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
191     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
192 jsr166 1.29 pleaseInterrupt.countDown();
193     try {
194 jsr166 1.34 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
195 jsr166 1.29 shouldThrow();
196     } catch (InterruptedException success) {}
197 jsr166 1.13 }});
198 jsr166 1.10
199 jsr166 1.34 await(pleaseInterrupt);
200 jsr166 1.35 assertThreadStaysAlive(t);
201 jsr166 1.13 t.interrupt();
202 jsr166 1.34 awaitTermination(t);
203 jsr166 1.29 }
204    
205     /**
206     * poll return null if no active putter
207 dl 1.4 */
208 jsr166 1.36 public void testPoll() { testPoll(false); }
209     public void testPoll_fair() { testPoll(true); }
210     public void testPoll(boolean fair) {
211     final SynchronousQueue q = new SynchronousQueue(fair);
212 jsr166 1.14 assertNull(q.poll());
213 dl 1.1 }
214    
215 dl 1.4 /**
216 jsr166 1.29 * timed poll with zero timeout times out if no active putter
217 dl 1.4 */
218 jsr166 1.36 public void testTimedPoll0() { testTimedPoll0(false); }
219     public void testTimedPoll0_fair() { testTimedPoll0(true); }
220     public void testTimedPoll0(boolean fair) {
221     final SynchronousQueue q = new SynchronousQueue(fair);
222     try { assertNull(q.poll(0, MILLISECONDS)); }
223     catch (InterruptedException e) { threadUnexpectedException(e); }
224 dl 1.1 }
225    
226 dl 1.4 /**
227 jsr166 1.29 * timed poll with nonzero timeout times out if no active putter
228 dl 1.4 */
229 jsr166 1.36 public void testTimedPoll() { testTimedPoll(false); }
230     public void testTimedPoll_fair() { testTimedPoll(true); }
231     public void testTimedPoll(boolean fair) {
232     final SynchronousQueue q = new SynchronousQueue(fair);
233 jsr166 1.35 long startTime = System.nanoTime();
234 jsr166 1.36 try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
235     catch (InterruptedException e) { threadUnexpectedException(e); }
236 jsr166 1.35 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
237 dl 1.8 }
238    
239     /**
240 jsr166 1.29 * timed poll before a delayed offer times out, returning null;
241     * after offer succeeds; on interruption throws
242 dl 1.8 */
243 jsr166 1.36 public void testTimedPollWithOffer() { testTimedPollWithOffer(false); }
244     public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
245     public void testTimedPollWithOffer(boolean fair) {
246     final SynchronousQueue q = new SynchronousQueue(fair);
247 jsr166 1.29 final CountDownLatch pleaseOffer = new CountDownLatch(1);
248 jsr166 1.36 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
249 jsr166 1.29 Thread t = newStartedThread(new CheckedRunnable() {
250 jsr166 1.14 public void realRun() throws InterruptedException {
251 jsr166 1.36 long startTime = System.nanoTime();
252     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
253     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
254 jsr166 1.29
255     pleaseOffer.countDown();
256 jsr166 1.36 startTime = System.nanoTime();
257 jsr166 1.29 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
258 jsr166 1.36 assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
259 jsr166 1.29
260 jsr166 1.36 Thread.currentThread().interrupt();
261 jsr166 1.13 try {
262     q.poll(LONG_DELAY_MS, MILLISECONDS);
263 jsr166 1.29 shouldThrow();
264 jsr166 1.13 } catch (InterruptedException success) {}
265 jsr166 1.36 assertFalse(Thread.interrupted());
266    
267     pleaseInterrupt.countDown();
268     try {
269     q.poll(LONG_DELAY_MS, MILLISECONDS);
270     shouldThrow();
271     } catch (InterruptedException success) {}
272     assertFalse(Thread.interrupted());
273 jsr166 1.13 }});
274    
275 jsr166 1.36 await(pleaseOffer);
276     long startTime = System.nanoTime();
277     try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
278     catch (InterruptedException e) { threadUnexpectedException(e); }
279     assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
280 jsr166 1.30
281 jsr166 1.36 await(pleaseInterrupt);
282     assertThreadStaysAlive(t);
283 jsr166 1.13 t.interrupt();
284 jsr166 1.36 awaitTermination(t);
285 jsr166 1.10 }
286 dl 1.1
287 dl 1.4 /**
288 jsr166 1.29 * peek() returns null if no active putter
289 dl 1.4 */
290 jsr166 1.36 public void testPeek() { testPeek(false); }
291     public void testPeek_fair() { testPeek(true); }
292     public void testPeek(boolean fair) {
293     final SynchronousQueue q = new SynchronousQueue(fair);
294 jsr166 1.14 assertNull(q.peek());
295 dl 1.1 }
296    
297 dl 1.4 /**
298 jsr166 1.36 * element() throws NoSuchElementException if no active putter
299 dl 1.4 */
300 jsr166 1.36 public void testElement() { testElement(false); }
301     public void testElement_fair() { testElement(true); }
302     public void testElement(boolean fair) {
303     final SynchronousQueue q = new SynchronousQueue(fair);
304 dl 1.1 try {
305     q.element();
306 dl 1.4 shouldThrow();
307 jsr166 1.13 } catch (NoSuchElementException success) {}
308 dl 1.1 }
309    
310 dl 1.4 /**
311 jsr166 1.36 * remove() throws NoSuchElementException if no active putter
312 dl 1.4 */
313 jsr166 1.36 public void testRemove() { testRemove(false); }
314     public void testRemove_fair() { testRemove(true); }
315     public void testRemove(boolean fair) {
316     final SynchronousQueue q = new SynchronousQueue(fair);
317 dl 1.1 try {
318     q.remove();
319 dl 1.4 shouldThrow();
320 jsr166 1.16 } catch (NoSuchElementException success) {}
321 dl 1.1 }
322    
323 dl 1.4 /**
324 dl 1.5 * contains returns false
325 dl 1.4 */
326 jsr166 1.36 public void testContains() { testContains(false); }
327     public void testContains_fair() { testContains(true); }
328     public void testContains(boolean fair) {
329     final SynchronousQueue q = new SynchronousQueue(fair);
330 dl 1.5 assertFalse(q.contains(zero));
331 dl 1.1 }
332    
333 dl 1.4 /**
334 dl 1.5 * clear ensures isEmpty
335 dl 1.4 */
336 jsr166 1.36 public void testClear() { testClear(false); }
337     public void testClear_fair() { testClear(true); }
338     public void testClear(boolean fair) {
339     final SynchronousQueue q = new SynchronousQueue(fair);
340 dl 1.1 q.clear();
341     assertTrue(q.isEmpty());
342     }
343    
344 dl 1.4 /**
345 dl 1.5 * containsAll returns false unless empty
346 dl 1.4 */
347 jsr166 1.36 public void testContainsAll() { testContainsAll(false); }
348     public void testContainsAll_fair() { testContainsAll(true); }
349     public void testContainsAll(boolean fair) {
350     final SynchronousQueue q = new SynchronousQueue(fair);
351 dl 1.1 Integer[] empty = new Integer[0];
352 dl 1.5 assertTrue(q.containsAll(Arrays.asList(empty)));
353     Integer[] ints = new Integer[1]; ints[0] = zero;
354 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
355     }
356    
357 dl 1.4 /**
358 dl 1.5 * retainAll returns false
359 dl 1.4 */
360 jsr166 1.36 public void testRetainAll() { testRetainAll(false); }
361     public void testRetainAll_fair() { testRetainAll(true); }
362     public void testRetainAll(boolean fair) {
363     final SynchronousQueue q = new SynchronousQueue(fair);
364 dl 1.1 Integer[] empty = new Integer[0];
365 dl 1.5 assertFalse(q.retainAll(Arrays.asList(empty)));
366     Integer[] ints = new Integer[1]; ints[0] = zero;
367     assertFalse(q.retainAll(Arrays.asList(ints)));
368 dl 1.1 }
369    
370 dl 1.4 /**
371 dl 1.5 * removeAll returns false
372 dl 1.4 */
373 jsr166 1.36 public void testRemoveAll() { testRemoveAll(false); }
374     public void testRemoveAll_fair() { testRemoveAll(true); }
375     public void testRemoveAll(boolean fair) {
376     final SynchronousQueue q = new SynchronousQueue(fair);
377 dl 1.1 Integer[] empty = new Integer[0];
378 dl 1.5 assertFalse(q.removeAll(Arrays.asList(empty)));
379     Integer[] ints = new Integer[1]; ints[0] = zero;
380 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
381     }
382    
383 dl 1.4 /**
384 dl 1.5 * toArray is empty
385 dl 1.4 */
386 jsr166 1.36 public void testToArray() { testToArray(false); }
387     public void testToArray_fair() { testToArray(true); }
388     public void testToArray(boolean fair) {
389     final SynchronousQueue q = new SynchronousQueue(fair);
390 jsr166 1.14 Object[] o = q.toArray();
391 jsr166 1.40 assertEquals(0, o.length);
392 dl 1.1 }
393    
394 dl 1.4 /**
395 jsr166 1.40 * toArray(Integer array) returns its argument with the first
396     * element (if present) nulled out
397 dl 1.4 */
398 jsr166 1.36 public void testToArray2() { testToArray2(false); }
399     public void testToArray2_fair() { testToArray2(true); }
400     public void testToArray2(boolean fair) {
401 jsr166 1.40 final SynchronousQueue<Integer> q
402     = new SynchronousQueue<Integer>(fair);
403     Integer[] a;
404    
405     a = new Integer[0];
406     assertSame(a, q.toArray(a));
407 jsr166 1.41
408 jsr166 1.40 a = new Integer[3];
409     Arrays.fill(a, 42);
410     assertSame(a, q.toArray(a));
411     assertNull(a[0]);
412     for (int i = 1; i < a.length; i++)
413     assertEquals(42, (int) a[i]);
414 dl 1.1 }
415 jsr166 1.10
416 dl 1.4 /**
417 dl 1.6 * toArray(null) throws NPE
418     */
419 jsr166 1.36 public void testToArray_null() { testToArray_null(false); }
420     public void testToArray_null_fair() { testToArray_null(true); }
421     public void testToArray_null(boolean fair) {
422     final SynchronousQueue q = new SynchronousQueue(fair);
423 jsr166 1.14 try {
424     Object o[] = q.toArray(null);
425     shouldThrow();
426     } catch (NullPointerException success) {}
427 dl 1.6 }
428    
429     /**
430 dl 1.5 * iterator does not traverse any elements
431 dl 1.4 */
432 jsr166 1.36 public void testIterator() { testIterator(false); }
433     public void testIterator_fair() { testIterator(true); }
434     public void testIterator(boolean fair) {
435     final SynchronousQueue q = new SynchronousQueue(fair);
436 jsr166 1.14 Iterator it = q.iterator();
437 dl 1.1 assertFalse(it.hasNext());
438     try {
439     Object x = it.next();
440 dl 1.4 shouldThrow();
441 jsr166 1.13 } catch (NoSuchElementException success) {}
442 dl 1.1 }
443    
444 dl 1.4 /**
445 dl 1.5 * iterator remove throws ISE
446 dl 1.4 */
447 jsr166 1.36 public void testIteratorRemove() { testIteratorRemove(false); }
448     public void testIteratorRemove_fair() { testIteratorRemove(true); }
449     public void testIteratorRemove(boolean fair) {
450     final SynchronousQueue q = new SynchronousQueue(fair);
451 jsr166 1.14 Iterator it = q.iterator();
452 dl 1.1 try {
453     it.remove();
454 dl 1.4 shouldThrow();
455 jsr166 1.13 } catch (IllegalStateException success) {}
456 dl 1.1 }
457    
458 dl 1.4 /**
459 dl 1.5 * toString returns a non-null string
460 dl 1.4 */
461 jsr166 1.36 public void testToString() { testToString(false); }
462     public void testToString_fair() { testToString(true); }
463     public void testToString(boolean fair) {
464     final SynchronousQueue q = new SynchronousQueue(fair);
465 dl 1.1 String s = q.toString();
466 dl 1.5 assertNotNull(s);
467 jsr166 1.10 }
468 dl 1.1
469 dl 1.4 /**
470 dl 1.5 * offer transfers elements across Executor tasks
471 dl 1.4 */
472 jsr166 1.36 public void testOfferInExecutor() { testOfferInExecutor(false); }
473     public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
474     public void testOfferInExecutor(boolean fair) {
475     final SynchronousQueue q = new SynchronousQueue(fair);
476 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
477 jsr166 1.35 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
478 dl 1.1
479 jsr166 1.13 executor.execute(new CheckedRunnable() {
480 jsr166 1.14 public void realRun() throws InterruptedException {
481 jsr166 1.19 assertFalse(q.offer(one));
482 jsr166 1.35 threadsStarted.await();
483     assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
484 jsr166 1.19 assertEquals(0, q.remainingCapacity());
485 jsr166 1.13 }});
486    
487     executor.execute(new CheckedRunnable() {
488 jsr166 1.14 public void realRun() throws InterruptedException {
489 jsr166 1.35 threadsStarted.await();
490 jsr166 1.19 assertSame(one, q.take());
491 jsr166 1.13 }});
492 jsr166 1.10
493 dl 1.3 joinPool(executor);
494 dl 1.1 }
495    
496 dl 1.4 /**
497 jsr166 1.35 * timed poll retrieves elements across Executor threads
498 dl 1.4 */
499 jsr166 1.36 public void testPollInExecutor() { testPollInExecutor(false); }
500     public void testPollInExecutor_fair() { testPollInExecutor(true); }
501     public void testPollInExecutor(boolean fair) {
502     final SynchronousQueue q = new SynchronousQueue(fair);
503 jsr166 1.35 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
504 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
505 jsr166 1.13 executor.execute(new CheckedRunnable() {
506 jsr166 1.14 public void realRun() throws InterruptedException {
507 jsr166 1.19 assertNull(q.poll());
508 jsr166 1.35 threadsStarted.await();
509     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
510 jsr166 1.19 assertTrue(q.isEmpty());
511 jsr166 1.13 }});
512    
513     executor.execute(new CheckedRunnable() {
514 jsr166 1.14 public void realRun() throws InterruptedException {
515 jsr166 1.35 threadsStarted.await();
516 jsr166 1.19 q.put(one);
517 jsr166 1.13 }});
518 jsr166 1.10
519 dl 1.3 joinPool(executor);
520 dl 1.2 }
521    
522 dl 1.4 /**
523 dl 1.5 * a deserialized serialized queue is usable
524 dl 1.4 */
525 jsr166 1.40 public void testSerialization() {
526     final SynchronousQueue x = new SynchronousQueue();
527     final SynchronousQueue y = new SynchronousQueue(false);
528     final SynchronousQueue z = new SynchronousQueue(true);
529     assertSerialEquals(x, y);
530     assertNotSerialEquals(x, z);
531     SynchronousQueue[] qs = { x, y, z };
532     for (SynchronousQueue q : qs) {
533     SynchronousQueue clone = serialClone(q);
534 jsr166 1.41 assertNotSame(q, clone);
535 jsr166 1.40 assertSerialEquals(q, clone);
536     assertTrue(clone.isEmpty());
537     assertEquals(0, clone.size());
538     assertEquals(0, clone.remainingCapacity());
539     assertFalse(clone.offer(zero));
540     }
541 dl 1.1 }
542 dl 1.6
543     /**
544     * drainTo(c) of empty queue doesn't transfer elements
545 jsr166 1.10 */
546 jsr166 1.36 public void testDrainTo() { testDrainTo(false); }
547     public void testDrainTo_fair() { testDrainTo(true); }
548     public void testDrainTo(boolean fair) {
549     final SynchronousQueue q = new SynchronousQueue(fair);
550 dl 1.6 ArrayList l = new ArrayList();
551     q.drainTo(l);
552 jsr166 1.40 assertEquals(0, q.size());
553     assertEquals(0, l.size());
554 dl 1.6 }
555    
556     /**
557     * drainTo empties queue, unblocking a waiting put.
558 jsr166 1.10 */
559 jsr166 1.36 public void testDrainToWithActivePut() { testDrainToWithActivePut(false); }
560     public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); }
561     public void testDrainToWithActivePut(boolean fair) {
562     final SynchronousQueue q = new SynchronousQueue(fair);
563 jsr166 1.35 Thread t = newStartedThread(new CheckedRunnable() {
564 jsr166 1.14 public void realRun() throws InterruptedException {
565 jsr166 1.35 q.put(one);
566 jsr166 1.13 }});
567    
568     ArrayList l = new ArrayList();
569 jsr166 1.35 long startTime = System.nanoTime();
570     while (l.isEmpty()) {
571     q.drainTo(l);
572     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
573     fail("timed out");
574     Thread.yield();
575     }
576     assertTrue(l.size() == 1);
577     assertSame(one, l.get(0));
578     awaitTermination(t);
579 dl 1.6 }
580    
581     /**
582     * drainTo(c, n) empties up to n elements of queue into c
583 jsr166 1.10 */
584 jsr166 1.13 public void testDrainToN() throws InterruptedException {
585 dl 1.6 final SynchronousQueue q = new SynchronousQueue();
586 jsr166 1.35 Thread t1 = newStartedThread(new CheckedRunnable() {
587 jsr166 1.14 public void realRun() throws InterruptedException {
588 jsr166 1.13 q.put(one);
589     }});
590    
591 jsr166 1.35 Thread t2 = newStartedThread(new CheckedRunnable() {
592 jsr166 1.14 public void realRun() throws InterruptedException {
593 jsr166 1.13 q.put(two);
594     }});
595 dl 1.6
596 jsr166 1.13 ArrayList l = new ArrayList();
597 dl 1.33 delay(SHORT_DELAY_MS);
598 jsr166 1.13 q.drainTo(l, 1);
599 jsr166 1.23 assertEquals(1, l.size());
600 jsr166 1.13 q.drainTo(l, 1);
601 jsr166 1.23 assertEquals(2, l.size());
602 jsr166 1.13 assertTrue(l.contains(one));
603     assertTrue(l.contains(two));
604 jsr166 1.35 awaitTermination(t1);
605     awaitTermination(t2);
606 dl 1.6 }
607    
608 jsr166 1.42 /**
609     * remove(null), contains(null) always return false
610     */
611     public void testNeverContainsNull() {
612     Collection<?> q = new SynchronousQueue();
613     assertFalse(q.contains(null));
614     assertFalse(q.remove(null));
615     }
616    
617 dl 1.1 }