ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.32 by jsr166, Thu Apr 14 22:55:08 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines