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.11 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.25 by jsr166, Wed Oct 6 07:49:23 2010 UTC

# Line 9 | Line 9
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 JSR166TestCase {
16  
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      /**
# Line 27 | Line 42 | public class SynchronousQueueTest extend
42      public void testEmptyFull() {
43          SynchronousQueue q = new SynchronousQueue();
44          assertTrue(q.isEmpty());
45 <        assertEquals(0, q.size());
45 >        assertEquals(0, q.size());
46          assertEquals(0, q.remainingCapacity());
47          assertFalse(q.offer(zero));
48      }
# Line 38 | Line 53 | public class SynchronousQueueTest extend
53      public void testFairEmptyFull() {
54          SynchronousQueue q = new SynchronousQueue(true);
55          assertTrue(q.isEmpty());
56 <        assertEquals(0, q.size());
56 >        assertEquals(0, q.size());
57          assertEquals(0, q.remainingCapacity());
58          assertFalse(q.offer(zero));
59      }
# Line 47 | Line 62 | public class SynchronousQueueTest extend
62       * offer(null) throws NPE
63       */
64      public void testOfferNull() {
65 <        try {
65 >        try {
66              SynchronousQueue q = new SynchronousQueue();
67              q.offer(null);
68              shouldThrow();
69 <        } catch (NullPointerException success) { }
69 >        } catch (NullPointerException success) {}
70      }
71  
72      /**
73       * add(null) throws NPE
74       */
75      public void testAddNull() {
76 <        try {
76 >        try {
77              SynchronousQueue q = new SynchronousQueue();
78              q.add(null);
79              shouldThrow();
80 <        } catch (NullPointerException success) { }
80 >        } catch (NullPointerException success) {}
81      }
82  
83      /**
# Line 77 | Line 92 | public class SynchronousQueueTest extend
92       * add throws ISE if no active taker
93       */
94      public void testAdd() {
95 <        try {
95 >        try {
96              SynchronousQueue q = new SynchronousQueue();
97              assertEquals(0, q.remainingCapacity());
98              q.add(one);
99              shouldThrow();
100 <        } catch (IllegalStateException success){
86 <        }
100 >        } catch (IllegalStateException success) {}
101      }
102  
103      /**
# Line 94 | Line 108 | public class SynchronousQueueTest extend
108              SynchronousQueue q = new SynchronousQueue();
109              q.addAll(null);
110              shouldThrow();
111 <        }
98 <        catch (NullPointerException success) {}
111 >        } catch (NullPointerException success) {}
112      }
113  
114      /**
# Line 106 | Line 119 | public class SynchronousQueueTest extend
119              SynchronousQueue q = new SynchronousQueue();
120              q.addAll(q);
121              shouldThrow();
122 <        }
110 <        catch (IllegalArgumentException success) {}
122 >        } catch (IllegalArgumentException success) {}
123      }
124  
125      /**
# Line 119 | Line 131 | public class SynchronousQueueTest extend
131              Integer[] ints = new Integer[1];
132              q.addAll(Arrays.asList(ints));
133              shouldThrow();
134 <        }
123 <        catch (NullPointerException success) {}
134 >        } catch (NullPointerException success) {}
135      }
136 +
137      /**
138       * addAll throws ISE if no active taker
139       */
# Line 133 | Line 145 | public class SynchronousQueueTest extend
145                  ints[i] = new Integer(i);
146              q.addAll(Arrays.asList(ints));
147              shouldThrow();
148 <        }
137 <        catch (IllegalStateException success) {}
148 >        } catch (IllegalStateException success) {}
149      }
150  
151      /**
152       * put(null) throws NPE
153       */
154 <    public void testPutNull() {
155 <        try {
154 >    public void testPutNull() throws InterruptedException {
155 >        try {
156              SynchronousQueue q = new SynchronousQueue();
157              q.put(null);
158              shouldThrow();
159 <        }
149 <        catch (NullPointerException success){
150 <        }
151 <        catch (InterruptedException ie) {
152 <            unexpectedException();
153 <        }
159 >        } catch (NullPointerException success) {}
160       }
161  
162      /**
163       * put blocks interruptibly if no active taker
164       */
165 <    public void testBlockingPut() {
166 <        Thread t = new Thread(new Runnable() {
167 <                public void run() {
168 <                    try {
169 <                        SynchronousQueue q = new SynchronousQueue();
170 <                        q.put(zero);
171 <                        threadShouldThrow();
166 <                    } catch (InterruptedException ie){
167 <                    }
168 <                }});
165 >    public void testBlockingPut() throws InterruptedException {
166 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
167 >            public void realRun() throws InterruptedException {
168 >                SynchronousQueue q = new SynchronousQueue();
169 >                q.put(zero);
170 >            }});
171 >
172          t.start();
173 <        try {
174 <           Thread.sleep(SHORT_DELAY_MS);
175 <           t.interrupt();
173 <           t.join();
174 <        }
175 <        catch (InterruptedException ie) {
176 <            unexpectedException();
177 <        }
173 >        Thread.sleep(SHORT_DELAY_MS);
174 >        t.interrupt();
175 >        t.join();
176      }
177  
178      /**
179       * put blocks waiting for take
180       */
181 <    public void testPutWithTake() {
181 >    public void testPutWithTake() throws InterruptedException {
182          final SynchronousQueue q = new SynchronousQueue();
183 <        Thread t = new Thread(new Runnable() {
184 <                public void run() {
185 <                    int added = 0;
186 <                    try {
187 <                        q.put(new Object());
188 <                        ++added;
191 <                        q.put(new Object());
192 <                        ++added;
193 <                        q.put(new Object());
194 <                        ++added;
195 <                        q.put(new Object());
183 >        Thread t = new Thread(new CheckedRunnable() {
184 >            public void realRun() throws InterruptedException {
185 >                int added = 0;
186 >                try {
187 >                    while (true) {
188 >                        q.put(added);
189                          ++added;
197                        threadShouldThrow();
198                    } catch (InterruptedException e){
199                        assertTrue(added >= 1);
190                      }
191 +                } catch (InterruptedException success) {
192 +                    assertEquals(1, added);
193                  }
194 <            });
195 <        try {
196 <            t.start();
197 <            Thread.sleep(SHORT_DELAY_MS);
198 <            q.take();
199 <            Thread.sleep(SHORT_DELAY_MS);
200 <            t.interrupt();
201 <            t.join();
210 <        } catch (Exception e){
211 <            unexpectedException();
212 <        }
194 >            }});
195 >
196 >        t.start();
197 >        Thread.sleep(SHORT_DELAY_MS);
198 >        assertEquals(0, q.take());
199 >        Thread.sleep(SHORT_DELAY_MS);
200 >        t.interrupt();
201 >        t.join();
202      }
203  
204      /**
205       * timed offer times out if elements not taken
206       */
207 <    public void testTimedOffer() {
207 >    public void testTimedOffer() throws InterruptedException {
208          final SynchronousQueue q = new SynchronousQueue();
209 <        Thread t = new Thread(new Runnable() {
210 <                public void run() {
211 <                    try {
212 <
213 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
225 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
226 <                        threadShouldThrow();
227 <                    } catch (InterruptedException success){}
228 <                }
229 <            });
209 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
210 >            public void realRun() throws InterruptedException {
211 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
212 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
213 >            }});
214  
215 <        try {
216 <            t.start();
217 <            Thread.sleep(SMALL_DELAY_MS);
218 <            t.interrupt();
235 <            t.join();
236 <        } catch (Exception e){
237 <            unexpectedException();
238 <        }
215 >        t.start();
216 >        Thread.sleep(SMALL_DELAY_MS);
217 >        t.interrupt();
218 >        t.join();
219      }
220  
221  
222      /**
223       * take blocks interruptibly when empty
224       */
225 <    public void testTakeFromEmpty() {
225 >    public void testTakeFromEmpty() throws InterruptedException {
226          final SynchronousQueue q = new SynchronousQueue();
227 <        Thread t = new Thread(new Runnable() {
228 <                public void run() {
229 <                    try {
230 <                        q.take();
231 <                        threadShouldThrow();
232 <                    } catch (InterruptedException success){ }
233 <                }
234 <            });
235 <        try {
256 <            t.start();
257 <            Thread.sleep(SHORT_DELAY_MS);
258 <            t.interrupt();
259 <            t.join();
260 <        } catch (Exception e){
261 <            unexpectedException();
262 <        }
227 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
228 >            public void realRun() throws InterruptedException {
229 >                q.take();
230 >            }});
231 >
232 >        t.start();
233 >        Thread.sleep(SHORT_DELAY_MS);
234 >        t.interrupt();
235 >        t.join();
236      }
237  
238  
239      /**
240       * put blocks interruptibly if no active taker
241       */
242 <    public void testFairBlockingPut() {
243 <        Thread t = new Thread(new Runnable() {
244 <                public void run() {
245 <                    try {
246 <                        SynchronousQueue q = new SynchronousQueue(true);
247 <                        q.put(zero);
248 <                        threadShouldThrow();
276 <                    } catch (InterruptedException ie){
277 <                    }
278 <                }});
242 >    public void testFairBlockingPut() throws InterruptedException {
243 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
244 >            public void realRun() throws InterruptedException {
245 >                SynchronousQueue q = new SynchronousQueue(true);
246 >                q.put(zero);
247 >            }});
248 >
249          t.start();
250 <        try {
251 <           Thread.sleep(SHORT_DELAY_MS);
252 <           t.interrupt();
283 <           t.join();
284 <        }
285 <        catch (InterruptedException ie) {
286 <            unexpectedException();
287 <        }
250 >        Thread.sleep(SHORT_DELAY_MS);
251 >        t.interrupt();
252 >        t.join();
253      }
254  
255      /**
256       * put blocks waiting for take
257       */
258 <    public void testFairPutWithTake() {
258 >    public void testFairPutWithTake() throws InterruptedException {
259          final SynchronousQueue q = new SynchronousQueue(true);
260 <        Thread t = new Thread(new Runnable() {
261 <                public void run() {
262 <                    int added = 0;
263 <                    try {
264 <                        q.put(new Object());
265 <                        ++added;
301 <                        q.put(new Object());
302 <                        ++added;
303 <                        q.put(new Object());
304 <                        ++added;
305 <                        q.put(new Object());
260 >        Thread t = new Thread(new CheckedRunnable() {
261 >            public void realRun() throws InterruptedException {
262 >                int added = 0;
263 >                try {
264 >                    while (true) {
265 >                        q.put(added);
266                          ++added;
307                        threadShouldThrow();
308                    } catch (InterruptedException e){
309                        assertTrue(added >= 1);
267                      }
268 +                } catch (InterruptedException success) {
269 +                    assertEquals(1, added);
270                  }
271 <            });
272 <        try {
273 <            t.start();
274 <            Thread.sleep(SHORT_DELAY_MS);
275 <            q.take();
276 <            Thread.sleep(SHORT_DELAY_MS);
277 <            t.interrupt();
278 <            t.join();
320 <        } catch (Exception e){
321 <            unexpectedException();
322 <        }
271 >            }});
272 >
273 >        t.start();
274 >        Thread.sleep(SHORT_DELAY_MS);
275 >        assertEquals(0, q.take());
276 >        Thread.sleep(SHORT_DELAY_MS);
277 >        t.interrupt();
278 >        t.join();
279      }
280  
281      /**
282       * timed offer times out if elements not taken
283       */
284 <    public void testFairTimedOffer() {
284 >    public void testFairTimedOffer() throws InterruptedException {
285          final SynchronousQueue q = new SynchronousQueue(true);
286 <        Thread t = new Thread(new Runnable() {
287 <                public void run() {
288 <                    try {
289 <
290 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
335 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
336 <                        threadShouldThrow();
337 <                    } catch (InterruptedException success){}
338 <                }
339 <            });
286 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
287 >            public void realRun() throws InterruptedException {
288 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
289 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
290 >            }});
291  
292 <        try {
293 <            t.start();
294 <            Thread.sleep(SMALL_DELAY_MS);
295 <            t.interrupt();
345 <            t.join();
346 <        } catch (Exception e){
347 <            unexpectedException();
348 <        }
292 >        t.start();
293 >        Thread.sleep(SMALL_DELAY_MS);
294 >        t.interrupt();
295 >        t.join();
296      }
297  
298  
299      /**
300       * take blocks interruptibly when empty
301       */
302 <    public void testFairTakeFromEmpty() {
302 >    public void testFairTakeFromEmpty() throws InterruptedException {
303          final SynchronousQueue q = new SynchronousQueue(true);
304 <        Thread t = new Thread(new Runnable() {
305 <                public void run() {
306 <                    try {
307 <                        q.take();
308 <                        threadShouldThrow();
309 <                    } catch (InterruptedException success){ }
310 <                }
311 <            });
312 <        try {
366 <            t.start();
367 <            Thread.sleep(SHORT_DELAY_MS);
368 <            t.interrupt();
369 <            t.join();
370 <        } catch (Exception e){
371 <            unexpectedException();
372 <        }
304 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
305 >            public void realRun() throws InterruptedException {
306 >                q.take();
307 >            }});
308 >
309 >        t.start();
310 >        Thread.sleep(SHORT_DELAY_MS);
311 >        t.interrupt();
312 >        t.join();
313      }
314  
315      /**
# Line 377 | Line 317 | public class SynchronousQueueTest extend
317       */
318      public void testPoll() {
319          SynchronousQueue q = new SynchronousQueue();
320 <        assertNull(q.poll());
320 >        assertNull(q.poll());
321      }
322  
323      /**
324       * timed pool with zero timeout times out if no active taker
325       */
326 <    public void testTimedPoll0() {
327 <        try {
328 <            SynchronousQueue q = new SynchronousQueue();
389 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
390 <        } catch (InterruptedException e){
391 <            unexpectedException();
392 <        }
326 >    public void testTimedPoll0() throws InterruptedException {
327 >        SynchronousQueue q = new SynchronousQueue();
328 >        assertNull(q.poll(0, MILLISECONDS));
329      }
330  
331      /**
332       * timed pool with nonzero timeout times out if no active taker
333       */
334 <    public void testTimedPoll() {
335 <        try {
336 <            SynchronousQueue q = new SynchronousQueue();
401 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
402 <        } catch (InterruptedException e){
403 <            unexpectedException();
404 <        }
334 >    public void testTimedPoll() throws InterruptedException {
335 >        SynchronousQueue q = new SynchronousQueue();
336 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
337      }
338  
339      /**
340       * Interrupted timed poll throws InterruptedException instead of
341       * returning timeout status
342       */
343 <    public void testInterruptedTimedPoll() {
412 <        Thread t = new Thread(new Runnable() {
413 <                public void run() {
414 <                    try {
415 <                        SynchronousQueue q = new SynchronousQueue();
416 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
417 <                    } catch (InterruptedException success){
418 <                    }
419 <                }});
420 <        t.start();
421 <        try {
422 <           Thread.sleep(SHORT_DELAY_MS);
423 <           t.interrupt();
424 <           t.join();
425 <        }
426 <        catch (InterruptedException ie) {
427 <            unexpectedException();
428 <        }
429 <    }
430 <
431 <    /**
432 <     *  timed poll before a delayed offer fails; after offer succeeds;
433 <     *  on interruption throws
434 <     */
435 <    public void testTimedPollWithOffer() {
343 >    public void testInterruptedTimedPoll() throws InterruptedException {
344          final SynchronousQueue q = new SynchronousQueue();
345 <        Thread t = new Thread(new Runnable() {
346 <                public void run() {
347 <                    try {
348 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
349 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
350 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
351 <                        threadShouldThrow();
352 <                    } catch (InterruptedException success) { }
353 <                }
446 <            });
447 <        try {
448 <            t.start();
449 <            Thread.sleep(SMALL_DELAY_MS);
450 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
451 <            t.interrupt();
452 <            t.join();
453 <        } catch (Exception e){
454 <            unexpectedException();
455 <        }
345 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
346 >            public void realRun() throws InterruptedException {
347 >                q.poll(SMALL_DELAY_MS, MILLISECONDS);
348 >            }});
349 >
350 >        t.start();
351 >        Thread.sleep(SHORT_DELAY_MS);
352 >        t.interrupt();
353 >        t.join();
354      }
355  
356      /**
357       * Interrupted timed poll throws InterruptedException instead of
358       * returning timeout status
359       */
360 <    public void testFairInterruptedTimedPoll() {
361 <        Thread t = new Thread(new Runnable() {
362 <                public void run() {
363 <                    try {
364 <                        SynchronousQueue q = new SynchronousQueue(true);
365 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
366 <                    } catch (InterruptedException success){
469 <                    }
470 <                }});
360 >    public void testFairInterruptedTimedPoll() throws InterruptedException {
361 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
362 >            public void realRun() throws InterruptedException {
363 >                SynchronousQueue q = new SynchronousQueue(true);
364 >                q.poll(SMALL_DELAY_MS, MILLISECONDS);
365 >            }});
366 >
367          t.start();
368 <        try {
369 <           Thread.sleep(SHORT_DELAY_MS);
370 <           t.interrupt();
475 <           t.join();
476 <        }
477 <        catch (InterruptedException ie) {
478 <            unexpectedException();
479 <        }
368 >        Thread.sleep(SHORT_DELAY_MS);
369 >        t.interrupt();
370 >        t.join();
371      }
372  
373      /**
374       *  timed poll before a delayed offer fails; after offer succeeds;
375       *  on interruption throws
376       */
377 <    public void testFairTimedPollWithOffer() {
377 >    public void testFairTimedPollWithOffer() throws InterruptedException {
378          final SynchronousQueue q = new SynchronousQueue(true);
379 <        Thread t = new Thread(new Runnable() {
380 <                public void run() {
381 <                    try {
382 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
384 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
385 <                        threadShouldThrow();
386 <                    } catch (InterruptedException success) { }
387 <                }
388 <            });
389 <        try {
390 <            t.start();
391 <            Thread.sleep(SMALL_DELAY_MS);
392 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
393 <            t.interrupt();
503 <            t.join();
504 <        } catch (Exception e){
505 <            unexpectedException();
506 <        }
379 >        Thread t = new Thread(new CheckedRunnable() {
380 >            public void realRun() throws InterruptedException {
381 >                try {
382 >                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
383 >                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
384 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
385 >                    threadShouldThrow();
386 >                } catch (InterruptedException success) {}
387 >            }});
388 >
389 >        t.start();
390 >        Thread.sleep(SMALL_DELAY_MS);
391 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
392 >        t.interrupt();
393 >        t.join();
394      }
395  
396  
# Line 512 | Line 399 | public class SynchronousQueueTest extend
399       */
400      public void testPeek() {
401          SynchronousQueue q = new SynchronousQueue();
402 <        assertNull(q.peek());
402 >        assertNull(q.peek());
403      }
404  
405      /**
# Line 523 | Line 410 | public class SynchronousQueueTest extend
410          try {
411              q.element();
412              shouldThrow();
413 <        }
527 <        catch (NoSuchElementException success) {}
413 >        } catch (NoSuchElementException success) {}
414      }
415  
416      /**
# Line 535 | Line 421 | public class SynchronousQueueTest extend
421          try {
422              q.remove();
423              shouldThrow();
424 <        } catch (NoSuchElementException success){
539 <        }
424 >        } catch (NoSuchElementException success) {}
425      }
426  
427      /**
# Line 604 | Line 489 | public class SynchronousQueueTest extend
489       */
490      public void testToArray() {
491          SynchronousQueue q = new SynchronousQueue();
492 <        Object[] o = q.toArray();
492 >        Object[] o = q.toArray();
493          assertEquals(o.length, 0);
494      }
495  
# Line 613 | Line 498 | public class SynchronousQueueTest extend
498       */
499      public void testToArray2() {
500          SynchronousQueue q = new SynchronousQueue();
501 <        Integer[] ints = new Integer[1];
501 >        Integer[] ints = new Integer[1];
502          assertNull(ints[0]);
503      }
504  
# Line 621 | Line 506 | public class SynchronousQueueTest extend
506       * toArray(null) throws NPE
507       */
508      public void testToArray_BadArg() {
509 <        try {
510 <            SynchronousQueue q = new SynchronousQueue();
511 <            Object o[] = q.toArray(null);
512 <            shouldThrow();
513 <        } catch (NullPointerException success){}
509 >        SynchronousQueue q = new SynchronousQueue();
510 >        try {
511 >            Object o[] = q.toArray(null);
512 >            shouldThrow();
513 >        } catch (NullPointerException success) {}
514      }
515  
516  
# Line 634 | Line 519 | public class SynchronousQueueTest extend
519       */
520      public void testIterator() {
521          SynchronousQueue q = new SynchronousQueue();
522 <        Iterator it = q.iterator();
522 >        Iterator it = q.iterator();
523          assertFalse(it.hasNext());
524          try {
525              Object x = it.next();
526              shouldThrow();
527 <        }
643 <        catch (NoSuchElementException success) {}
527 >        } catch (NoSuchElementException success) {}
528      }
529  
530      /**
# Line 648 | Line 532 | public class SynchronousQueueTest extend
532       */
533      public void testIteratorRemove() {
534          SynchronousQueue q = new SynchronousQueue();
535 <        Iterator it = q.iterator();
535 >        Iterator it = q.iterator();
536          try {
537              it.remove();
538              shouldThrow();
539 <        }
656 <        catch (IllegalStateException success) {}
539 >        } catch (IllegalStateException success) {}
540      }
541  
542      /**
# Line 672 | Line 555 | public class SynchronousQueueTest extend
555      public void testOfferInExecutor() {
556          final SynchronousQueue q = new SynchronousQueue();
557          ExecutorService executor = Executors.newFixedThreadPool(2);
675        final Integer one = new Integer(1);
558  
559 <        executor.execute(new Runnable() {
560 <            public void run() {
561 <                threadAssertFalse(q.offer(one));
562 <                try {
563 <                    threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
564 <                    threadAssertEquals(0, q.remainingCapacity());
565 <                }
566 <                catch (InterruptedException e) {
567 <                    threadUnexpectedException();
568 <                }
569 <            }
570 <        });
689 <
690 <        executor.execute(new Runnable() {
691 <            public void run() {
692 <                try {
693 <                    Thread.sleep(SMALL_DELAY_MS);
694 <                    threadAssertEquals(one, q.take());
695 <                }
696 <                catch (InterruptedException e) {
697 <                    threadUnexpectedException();
698 <                }
699 <            }
700 <        });
559 >        executor.execute(new CheckedRunnable() {
560 >            public void realRun() throws InterruptedException {
561 >                assertFalse(q.offer(one));
562 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
563 >                assertEquals(0, q.remainingCapacity());
564 >            }});
565 >
566 >        executor.execute(new CheckedRunnable() {
567 >            public void realRun() throws InterruptedException {
568 >                Thread.sleep(SMALL_DELAY_MS);
569 >                assertSame(one, q.take());
570 >            }});
571  
572          joinPool(executor);
703
573      }
574  
575      /**
# Line 709 | Line 578 | public class SynchronousQueueTest extend
578      public void testPollInExecutor() {
579          final SynchronousQueue q = new SynchronousQueue();
580          ExecutorService executor = Executors.newFixedThreadPool(2);
581 <        executor.execute(new Runnable() {
582 <            public void run() {
583 <                threadAssertNull(q.poll());
584 <                try {
585 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
586 <                    threadAssertTrue(q.isEmpty());
587 <                }
588 <                catch (InterruptedException e) {
589 <                    threadUnexpectedException();
590 <                }
591 <            }
592 <        });
724 <
725 <        executor.execute(new Runnable() {
726 <            public void run() {
727 <                try {
728 <                    Thread.sleep(SMALL_DELAY_MS);
729 <                    q.put(new Integer(1));
730 <                }
731 <                catch (InterruptedException e) {
732 <                    threadUnexpectedException();
733 <                }
734 <            }
735 <        });
581 >        executor.execute(new CheckedRunnable() {
582 >            public void realRun() throws InterruptedException {
583 >                assertNull(q.poll());
584 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
585 >                assertTrue(q.isEmpty());
586 >            }});
587 >
588 >        executor.execute(new CheckedRunnable() {
589 >            public void realRun() throws InterruptedException {
590 >                Thread.sleep(SHORT_DELAY_MS);
591 >                q.put(one);
592 >            }});
593  
594          joinPool(executor);
595      }
# Line 740 | Line 597 | public class SynchronousQueueTest extend
597      /**
598       * a deserialized serialized queue is usable
599       */
600 <    public void testSerialization() {
600 >    public void testSerialization() throws Exception {
601          SynchronousQueue q = new SynchronousQueue();
602 <        try {
603 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
604 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
605 <            out.writeObject(q);
606 <            out.close();
607 <
608 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
609 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
610 <            SynchronousQueue r = (SynchronousQueue)in.readObject();
611 <            assertEquals(q.size(), r.size());
612 <            while (!q.isEmpty())
756 <                assertEquals(q.remove(), r.remove());
757 <        } catch (Exception e){
758 <            e.printStackTrace();
759 <            unexpectedException();
760 <        }
602 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
603 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
604 >        out.writeObject(q);
605 >        out.close();
606 >
607 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
608 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
609 >        SynchronousQueue r = (SynchronousQueue)in.readObject();
610 >        assertEquals(q.size(), r.size());
611 >        while (!q.isEmpty())
612 >            assertEquals(q.remove(), r.remove());
613      }
614  
615      /**
# Line 768 | Line 620 | public class SynchronousQueueTest extend
620          try {
621              q.drainTo(null);
622              shouldThrow();
623 <        } catch (NullPointerException success) {
772 <        }
623 >        } catch (NullPointerException success) {}
624      }
625  
626      /**
# Line 780 | Line 631 | public class SynchronousQueueTest extend
631          try {
632              q.drainTo(q);
633              shouldThrow();
634 <        } catch (IllegalArgumentException success) {
784 <        }
634 >        } catch (IllegalArgumentException success) {}
635      }
636  
637      /**
# Line 798 | Line 648 | public class SynchronousQueueTest extend
648      /**
649       * drainTo empties queue, unblocking a waiting put.
650       */
651 <    public void testDrainToWithActivePut() {
651 >    public void testDrainToWithActivePut() throws InterruptedException {
652          final SynchronousQueue q = new SynchronousQueue();
653 <        Thread t = new Thread(new Runnable() {
654 <                public void run() {
655 <                    try {
656 <                        q.put(new Integer(1));
657 <                    } catch (InterruptedException ie){
658 <                        threadUnexpectedException();
659 <                    }
660 <                }
661 <            });
662 <        try {
663 <            t.start();
664 <            ArrayList l = new ArrayList();
665 <            Thread.sleep(SHORT_DELAY_MS);
666 <            q.drainTo(l);
817 <            assertTrue(l.size() <= 1);
818 <            if (l.size() > 0)
819 <                assertEquals(l.get(0), new Integer(1));
820 <            t.join();
821 <            assertTrue(l.size() <= 1);
822 <        } catch (Exception e){
823 <            unexpectedException();
824 <        }
653 >        Thread t = new Thread(new CheckedRunnable() {
654 >            public void realRun() throws InterruptedException {
655 >                q.put(new Integer(1));
656 >            }});
657 >
658 >        t.start();
659 >        ArrayList l = new ArrayList();
660 >        Thread.sleep(SHORT_DELAY_MS);
661 >        q.drainTo(l);
662 >        assertTrue(l.size() <= 1);
663 >        if (l.size() > 0)
664 >            assertEquals(l.get(0), new Integer(1));
665 >        t.join();
666 >        assertTrue(l.size() <= 1);
667      }
668  
669      /**
# Line 832 | Line 674 | public class SynchronousQueueTest extend
674          try {
675              q.drainTo(null, 0);
676              shouldThrow();
677 <        } catch (NullPointerException success) {
836 <        }
677 >        } catch (NullPointerException success) {}
678      }
679  
680      /**
# Line 844 | Line 685 | public class SynchronousQueueTest extend
685          try {
686              q.drainTo(q, 0);
687              shouldThrow();
688 <        } catch (IllegalArgumentException success) {
848 <        }
688 >        } catch (IllegalArgumentException success) {}
689      }
690  
691      /**
692       * drainTo(c, n) empties up to n elements of queue into c
693       */
694 <    public void testDrainToN() {
694 >    public void testDrainToN() throws InterruptedException {
695          final SynchronousQueue q = new SynchronousQueue();
696 <        Thread t1 = new Thread(new Runnable() {
697 <                public void run() {
698 <                    try {
699 <                        q.put(one);
700 <                    } catch (InterruptedException ie){
701 <                        threadUnexpectedException();
702 <                    }
703 <                }
704 <            });
865 <        Thread t2 = new Thread(new Runnable() {
866 <                public void run() {
867 <                    try {
868 <                        q.put(two);
869 <                    } catch (InterruptedException ie){
870 <                        threadUnexpectedException();
871 <                    }
872 <                }
873 <            });
696 >        Thread t1 = new Thread(new CheckedRunnable() {
697 >            public void realRun() throws InterruptedException {
698 >                q.put(one);
699 >            }});
700 >
701 >        Thread t2 = new Thread(new CheckedRunnable() {
702 >            public void realRun() throws InterruptedException {
703 >                q.put(two);
704 >            }});
705  
706 <        try {
707 <            t1.start();
708 <            t2.start();
709 <            ArrayList l = new ArrayList();
710 <            Thread.sleep(SHORT_DELAY_MS);
711 <            q.drainTo(l, 1);
712 <            assertTrue(l.size() == 1);
713 <            q.drainTo(l, 1);
714 <            assertTrue(l.size() == 2);
715 <            assertTrue(l.contains(one));
716 <            assertTrue(l.contains(two));
717 <            t1.join();
887 <            t2.join();
888 <        } catch (Exception e){
889 <            unexpectedException();
890 <        }
706 >        t1.start();
707 >        t2.start();
708 >        ArrayList l = new ArrayList();
709 >        Thread.sleep(SHORT_DELAY_MS);
710 >        q.drainTo(l, 1);
711 >        assertEquals(1, l.size());
712 >        q.drainTo(l, 1);
713 >        assertEquals(2, l.size());
714 >        assertTrue(l.contains(one));
715 >        assertTrue(l.contains(two));
716 >        t1.join();
717 >        t2.join();
718      }
719  
893
720   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines