ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.64
Committed: Tue Aug 13 00:54:51 2019 UTC (4 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.63: +3 -3 lines
Log Message:
use randomBoolean()

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