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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.13 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.23 by jsr166, Sun Nov 22 00:17:37 2009 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 PriorityBlockingQueueTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(PriorityBlockingQueueTest.class);
20 >        return new TestSuite(PriorityBlockingQueueTest.class);
21      }
22  
23      private static final int NOCAP = Integer.MAX_VALUE;
# Line 39 | Line 40 | public class PriorityBlockingQueueTest e
40      private PriorityBlockingQueue populatedQueue(int n) {
41          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
42          assertTrue(q.isEmpty());
43 <        for (int i = n-1; i >= 0; i-=2)
44 <            assertTrue(q.offer(new Integer(i)));
45 <        for (int i = (n & 1); i < n; i+=2)
46 <            assertTrue(q.offer(new Integer(i)));
43 >        for (int i = n-1; i >= 0; i-=2)
44 >            assertTrue(q.offer(new Integer(i)));
45 >        for (int i = (n & 1); i < n; i+=2)
46 >            assertTrue(q.offer(new Integer(i)));
47          assertFalse(q.isEmpty());
48          assertEquals(NOCAP, q.remainingCapacity());
49 <        assertEquals(n, q.size());
49 >        assertEquals(n, q.size());
50          return q;
51      }
52  
# Line 57 | Line 58 | public class PriorityBlockingQueueTest e
58      }
59  
60      /**
61 <     * Constructor throws IAE if  capacity argument nonpositive
61 >     * Constructor throws IAE if capacity argument nonpositive
62       */
63      public void testConstructor2() {
64          try {
65              PriorityBlockingQueue q = new PriorityBlockingQueue(0);
66              shouldThrow();
67 <        }
67 <        catch (IllegalArgumentException success) {}
67 >        } catch (IllegalArgumentException success) {}
68      }
69  
70      /**
# Line 74 | Line 74 | public class PriorityBlockingQueueTest e
74          try {
75              PriorityBlockingQueue q = new PriorityBlockingQueue(null);
76              shouldThrow();
77 <        }
78 <        catch (NullPointerException success) {}
77 >        } catch (NullPointerException success) {}
78      }
79  
80      /**
# Line 86 | Line 85 | public class PriorityBlockingQueueTest e
85              Integer[] ints = new Integer[SIZE];
86              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
87              shouldThrow();
88 <        }
90 <        catch (NullPointerException success) {}
88 >        } catch (NullPointerException success) {}
89      }
90  
91      /**
# Line 100 | Line 98 | public class PriorityBlockingQueueTest e
98                  ints[i] = new Integer(i);
99              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
100              shouldThrow();
101 <        }
104 <        catch (NullPointerException success) {}
101 >        } catch (NullPointerException success) {}
102      }
103  
104      /**
105       * Queue contains all elements of collection used to initialize
106       */
107      public void testConstructor6() {
108 <        try {
109 <            Integer[] ints = new Integer[SIZE];
110 <            for (int i = 0; i < SIZE; ++i)
111 <                ints[i] = new Integer(i);
112 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
113 <            for (int i = 0; i < SIZE; ++i)
117 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
108 >        Integer[] ints = new Integer[SIZE];
109 >        for (int i = 0; i < SIZE; ++i)
110 >            ints[i] = new Integer(i);
111 >        PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
112 >        for (int i = 0; i < SIZE; ++i)
113 >            assertEquals(ints[i], q.poll());
114      }
115  
116      /**
117       * The comparator used in constructor is used
118       */
119      public void testConstructor7() {
120 <        try {
121 <            MyReverseComparator cmp = new MyReverseComparator();
122 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
123 <            assertEquals(cmp, q.comparator());
124 <            Integer[] ints = new Integer[SIZE];
125 <            for (int i = 0; i < SIZE; ++i)
126 <                ints[i] = new Integer(i);
127 <            q.addAll(Arrays.asList(ints));
128 <            for (int i = SIZE-1; i >= 0; --i)
135 <                assertEquals(ints[i], q.poll());
136 <        }
137 <        finally {}
120 >        MyReverseComparator cmp = new MyReverseComparator();
121 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
122 >        assertEquals(cmp, q.comparator());
123 >        Integer[] ints = new Integer[SIZE];
124 >        for (int i = 0; i < SIZE; ++i)
125 >            ints[i] = new Integer(i);
126 >        q.addAll(Arrays.asList(ints));
127 >        for (int i = SIZE-1; i >= 0; --i)
128 >            assertEquals(ints[i], q.poll());
129      }
130  
131      /**
# Line 174 | Line 165 | public class PriorityBlockingQueueTest e
165       * offer(null) throws NPE
166       */
167      public void testOfferNull() {
168 <        try {
168 >        try {
169              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
170              q.offer(null);
171              shouldThrow();
172 <        } catch (NullPointerException success) { }
172 >        } catch (NullPointerException success) {}
173      }
174  
175      /**
176       * add(null) throws NPE
177       */
178      public void testAddNull() {
179 <        try {
179 >        try {
180              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
181              q.add(null);
182              shouldThrow();
183 <        } catch (NullPointerException success) { }
183 >        } catch (NullPointerException success) {}
184      }
185  
186      /**
# Line 211 | Line 202 | public class PriorityBlockingQueueTest e
202              q.offer(new Object());
203              q.offer(new Object());
204              shouldThrow();
205 <        }
215 <        catch (ClassCastException success) {}
205 >        } catch (ClassCastException success) {}
206      }
207  
208      /**
# Line 234 | Line 224 | public class PriorityBlockingQueueTest e
224              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
225              q.addAll(null);
226              shouldThrow();
227 <        }
238 <        catch (NullPointerException success) {}
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
# Line 246 | Line 235 | public class PriorityBlockingQueueTest e
235              PriorityBlockingQueue q = populatedQueue(SIZE);
236              q.addAll(q);
237              shouldThrow();
238 <        }
250 <        catch (IllegalArgumentException success) {}
238 >        } catch (IllegalArgumentException success) {}
239      }
240  
241      /**
# Line 259 | Line 247 | public class PriorityBlockingQueueTest e
247              Integer[] ints = new Integer[SIZE];
248              q.addAll(Arrays.asList(ints));
249              shouldThrow();
250 <        }
263 <        catch (NullPointerException success) {}
250 >        } catch (NullPointerException success) {}
251      }
252      /**
253       * addAll of a collection with any null elements throws NPE after
# Line 274 | Line 261 | public class PriorityBlockingQueueTest e
261                  ints[i] = new Integer(i);
262              q.addAll(Arrays.asList(ints));
263              shouldThrow();
264 <        }
278 <        catch (NullPointerException success) {}
264 >        } catch (NullPointerException success) {}
265      }
266  
267      /**
268       * Queue contains all elements of successful addAll
269       */
270      public void testAddAll5() {
271 <        try {
272 <            Integer[] empty = new Integer[0];
273 <            Integer[] ints = new Integer[SIZE];
274 <            for (int i = SIZE-1; i >= 0; --i)
275 <                ints[i] = new Integer(i);
276 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
277 <            assertFalse(q.addAll(Arrays.asList(empty)));
278 <            assertTrue(q.addAll(Arrays.asList(ints)));
279 <            for (int i = 0; i < SIZE; ++i)
294 <                assertEquals(ints[i], q.poll());
295 <        }
296 <        finally {}
271 >        Integer[] empty = new Integer[0];
272 >        Integer[] ints = new Integer[SIZE];
273 >        for (int i = SIZE-1; i >= 0; --i)
274 >            ints[i] = new Integer(i);
275 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
276 >        assertFalse(q.addAll(Arrays.asList(empty)));
277 >        assertTrue(q.addAll(Arrays.asList(ints)));
278 >        for (int i = 0; i < SIZE; ++i)
279 >            assertEquals(ints[i], q.poll());
280      }
281  
282      /**
283       * put(null) throws NPE
284       */
285       public void testPutNull() {
286 <        try {
286 >        try {
287              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
288              q.put(null);
289              shouldThrow();
290 <        }
308 <        catch (NullPointerException success){
309 <        }
290 >        } catch (NullPointerException success) {}
291       }
292  
293      /**
294       * all elements successfully put are contained
295       */
296       public void testPut() {
297 <         try {
298 <             PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
299 <             for (int i = 0; i < SIZE; ++i) {
300 <                 Integer I = new Integer(i);
301 <                 q.put(I);
321 <                 assertTrue(q.contains(I));
322 <             }
323 <             assertEquals(SIZE, q.size());
297 >         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
298 >         for (int i = 0; i < SIZE; ++i) {
299 >             Integer I = new Integer(i);
300 >             q.put(I);
301 >             assertTrue(q.contains(I));
302           }
303 <         finally {
326 <        }
303 >         assertEquals(SIZE, q.size());
304      }
305  
306      /**
307       * put doesn't block waiting for take
308       */
309 <    public void testPutWithTake() {
309 >    public void testPutWithTake() throws InterruptedException {
310          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
311 <        Thread t = new Thread(new Runnable() {
312 <                public void run() {
313 <                    int added = 0;
314 <                    try {
315 <                        q.put(new Integer(0));
316 <                        ++added;
317 <                        q.put(new Integer(0));
318 <                        ++added;
319 <                        q.put(new Integer(0));
320 <                        ++added;
321 <                        q.put(new Integer(0));
322 <                        ++added;
323 <                        threadAssertTrue(added == 4);
347 <                    } finally {
348 <                    }
349 <                }
350 <            });
351 <        try {
352 <            t.start();
353 <            Thread.sleep(SHORT_DELAY_MS);
354 <            q.take();
355 <            t.interrupt();
356 <            t.join();
357 <        } catch (Exception e){
358 <            unexpectedException();
359 <        }
311 >        final int size = 4;
312 >        Thread t = new Thread(new CheckedRunnable() {
313 >            public void realRun() {
314 >                for (int i = 0; i < size; i++)
315 >                    q.put(new Integer(0));
316 >            }});
317 >
318 >        t.start();
319 >        Thread.sleep(SHORT_DELAY_MS);
320 >        assertEquals(q.size(), size);
321 >        q.take();
322 >        t.interrupt();
323 >        t.join();
324      }
325  
326      /**
327       * timed offer does not time out
328       */
329 <    public void testTimedOffer() {
329 >    public void testTimedOffer() throws InterruptedException {
330          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
331 <        Thread t = new Thread(new Runnable() {
332 <                public void run() {
333 <                    try {
334 <                        q.put(new Integer(0));
335 <                        q.put(new Integer(0));
336 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
337 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
374 <                    } finally { }
375 <                }
376 <            });
331 >        Thread t = new Thread(new CheckedRunnable() {
332 >            public void realRun() {
333 >                q.put(new Integer(0));
334 >                q.put(new Integer(0));
335 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
336 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
337 >            }});
338  
339 <        try {
340 <            t.start();
341 <            Thread.sleep(SMALL_DELAY_MS);
342 <            t.interrupt();
382 <            t.join();
383 <        } catch (Exception e){
384 <            unexpectedException();
385 <        }
339 >        t.start();
340 >        Thread.sleep(SMALL_DELAY_MS);
341 >        t.interrupt();
342 >        t.join();
343      }
344  
345      /**
346       * take retrieves elements in priority order
347       */
348 <    public void testTake() {
349 <        try {
350 <            PriorityBlockingQueue q = populatedQueue(SIZE);
351 <            for (int i = 0; i < SIZE; ++i) {
352 <                assertEquals(i, ((Integer)q.take()).intValue());
396 <            }
397 <        } catch (InterruptedException e){
398 <            unexpectedException();
399 <        }
348 >    public void testTake() throws InterruptedException {
349 >        PriorityBlockingQueue q = populatedQueue(SIZE);
350 >        for (int i = 0; i < SIZE; ++i) {
351 >            assertEquals(i, ((Integer)q.take()).intValue());
352 >        }
353      }
354  
355      /**
356       * take blocks interruptibly when empty
357       */
358 <    public void testTakeFromEmpty() {
358 >    public void testTakeFromEmpty() throws InterruptedException {
359          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run() {
362 <                    try {
363 <                        q.take();
364 <                        threadShouldThrow();
365 <                    } catch (InterruptedException success){ }
366 <                }
367 <            });
368 <        try {
416 <            t.start();
417 <            Thread.sleep(SHORT_DELAY_MS);
418 <            t.interrupt();
419 <            t.join();
420 <        } catch (Exception e){
421 <            unexpectedException();
422 <        }
360 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                q.take();
363 >            }});
364 >
365 >        t.start();
366 >        Thread.sleep(SHORT_DELAY_MS);
367 >        t.interrupt();
368 >        t.join();
369      }
370  
371      /**
372       * Take removes existing elements until empty, then blocks interruptibly
373       */
374 <    public void testBlockingTake() {
375 <        Thread t = new Thread(new Runnable() {
376 <                public void run() {
377 <                    try {
378 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
379 <                        for (int i = 0; i < SIZE; ++i) {
380 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
381 <                        }
382 <                        q.take();
383 <                        threadShouldThrow();
384 <                    } catch (InterruptedException success){
385 <                    }
386 <                }});
374 >    public void testBlockingTake() throws InterruptedException {
375 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
376 >        Thread t = new Thread(new CheckedRunnable() {
377 >            public void realRun() throws InterruptedException {
378 >                for (int i = 0; i < SIZE; ++i) {
379 >                    assertEquals(i, q.take());
380 >                }
381 >                try {
382 >                    q.take();
383 >                    shouldThrow();
384 >                } catch (InterruptedException success) {}
385 >            }});
386 >
387          t.start();
388 <        try {
389 <           Thread.sleep(SHORT_DELAY_MS);
390 <           t.interrupt();
445 <           t.join();
446 <        }
447 <        catch (InterruptedException ie) {
448 <            unexpectedException();
449 <        }
388 >        Thread.sleep(SHORT_DELAY_MS);
389 >        t.interrupt();
390 >        t.join();
391      }
392  
393  
# Line 458 | Line 399 | public class PriorityBlockingQueueTest e
399          for (int i = 0; i < SIZE; ++i) {
400              assertEquals(i, ((Integer)q.poll()).intValue());
401          }
402 <        assertNull(q.poll());
402 >        assertNull(q.poll());
403      }
404  
405      /**
406       * timed pool with zero timeout succeeds when non-empty, else times out
407       */
408 <    public void testTimedPoll0() {
409 <        try {
410 <            PriorityBlockingQueue q = populatedQueue(SIZE);
411 <            for (int i = 0; i < SIZE; ++i) {
412 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
413 <            }
473 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
474 <        } catch (InterruptedException e){
475 <            unexpectedException();
476 <        }
408 >    public void testTimedPoll0() throws InterruptedException {
409 >        PriorityBlockingQueue q = populatedQueue(SIZE);
410 >        for (int i = 0; i < SIZE; ++i) {
411 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
412 >        }
413 >        assertNull(q.poll(0, MILLISECONDS));
414      }
415  
416      /**
417       * timed pool with nonzero timeout succeeds when non-empty, else times out
418       */
419 <    public void testTimedPoll() {
420 <        try {
421 <            PriorityBlockingQueue q = populatedQueue(SIZE);
422 <            for (int i = 0; i < SIZE; ++i) {
423 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
424 <            }
488 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489 <        } catch (InterruptedException e){
490 <            unexpectedException();
491 <        }
419 >    public void testTimedPoll() throws InterruptedException {
420 >        PriorityBlockingQueue q = populatedQueue(SIZE);
421 >        for (int i = 0; i < SIZE; ++i) {
422 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
423 >        }
424 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
425      }
426  
427      /**
428       * Interrupted timed poll throws InterruptedException instead of
429       * returning timeout status
430       */
431 <    public void testInterruptedTimedPoll() {
432 <        Thread t = new Thread(new Runnable() {
433 <                public void run() {
434 <                    try {
435 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
436 <                        for (int i = 0; i < SIZE; ++i) {
437 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
438 <                        }
439 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
440 <                    } catch (InterruptedException success){
441 <                    }
442 <                }});
431 >    public void testInterruptedTimedPoll() throws InterruptedException {
432 >        Thread t = new Thread(new CheckedRunnable() {
433 >            public void realRun() throws InterruptedException {
434 >                PriorityBlockingQueue q = populatedQueue(SIZE);
435 >                for (int i = 0; i < SIZE; ++i) {
436 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
437 >                }
438 >                try {
439 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
440 >                    shouldThrow();
441 >                } catch (InterruptedException success) {}
442 >            }});
443 >
444          t.start();
445 <        try {
446 <           Thread.sleep(SHORT_DELAY_MS);
447 <           t.interrupt();
514 <           t.join();
515 <        }
516 <        catch (InterruptedException ie) {
517 <            unexpectedException();
518 <        }
445 >        Thread.sleep(SHORT_DELAY_MS);
446 >        t.interrupt();
447 >        t.join();
448      }
449  
450      /**
451       *  timed poll before a delayed offer fails; after offer succeeds;
452       *  on interruption throws
453       */
454 <    public void testTimedPollWithOffer() {
454 >    public void testTimedPollWithOffer() throws InterruptedException {
455          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
456 <        Thread t = new Thread(new Runnable() {
457 <                public void run() {
458 <                    try {
459 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
460 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
461 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
462 <                        threadShouldThrow();
463 <                    } catch (InterruptedException success) { }
464 <                }
465 <            });
466 <        try {
467 <            t.start();
468 <            Thread.sleep(SMALL_DELAY_MS);
469 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
470 <            t.interrupt();
542 <            t.join();
543 <        } catch (Exception e){
544 <            unexpectedException();
545 <        }
456 >        Thread t = new Thread(new CheckedRunnable() {
457 >            public void realRun() throws InterruptedException {
458 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
459 >                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
460 >                try {
461 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
462 >                    shouldThrow();
463 >                } catch (InterruptedException success) {}
464 >            }});
465 >
466 >        t.start();
467 >        Thread.sleep(SMALL_DELAY_MS);
468 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
469 >        t.interrupt();
470 >        t.join();
471      }
472  
473  
# Line 557 | Line 482 | public class PriorityBlockingQueueTest e
482              assertTrue(q.peek() == null ||
483                         i != ((Integer)q.peek()).intValue());
484          }
485 <        assertNull(q.peek());
485 >        assertNull(q.peek());
486      }
487  
488      /**
# Line 572 | Line 497 | public class PriorityBlockingQueueTest e
497          try {
498              q.element();
499              shouldThrow();
500 <        }
576 <        catch (NoSuchElementException success) {}
500 >        } catch (NoSuchElementException success) {}
501      }
502  
503      /**
# Line 587 | Line 511 | public class PriorityBlockingQueueTest e
511          try {
512              q.remove();
513              shouldThrow();
514 <        } catch (NoSuchElementException success){
591 <        }
514 >        } catch (NoSuchElementException success) {}
515      }
516  
517      /**
# Line 685 | Line 608 | public class PriorityBlockingQueueTest e
608      /**
609       *  toArray contains all elements
610       */
611 <    public void testToArray() {
611 >    public void testToArray() throws InterruptedException {
612          PriorityBlockingQueue q = populatedQueue(SIZE);
613 <        Object[] o = q.toArray();
613 >        Object[] o = q.toArray();
614          Arrays.sort(o);
615 <        try {
616 <        for (int i = 0; i < o.length; i++)
694 <            assertEquals(o[i], q.take());
695 <        } catch (InterruptedException e){
696 <            unexpectedException();
697 <        }
615 >        for (int i = 0; i < o.length; i++)
616 >            assertEquals(o[i], q.take());
617      }
618  
619      /**
620       * toArray(a) contains all elements
621       */
622 <    public void testToArray2() {
622 >    public void testToArray2() throws InterruptedException {
623          PriorityBlockingQueue q = populatedQueue(SIZE);
624 <        Integer[] ints = new Integer[SIZE];
625 <        ints = (Integer[])q.toArray(ints);
624 >        Integer[] ints = new Integer[SIZE];
625 >        ints = (Integer[])q.toArray(ints);
626          Arrays.sort(ints);
627 <        try {
628 <            for (int i = 0; i < ints.length; i++)
710 <                assertEquals(ints[i], q.take());
711 <        } catch (InterruptedException e){
712 <            unexpectedException();
713 <        }
627 >        for (int i = 0; i < ints.length; i++)
628 >            assertEquals(ints[i], q.take());
629      }
630  
631      /**
632       * toArray(null) throws NPE
633       */
634      public void testToArray_BadArg() {
635 <        try {
635 >        try {
636              PriorityBlockingQueue q = populatedQueue(SIZE);
637 <            Object o[] = q.toArray(null);
638 <            shouldThrow();
639 <        } catch (NullPointerException success){}
637 >            Object o[] = q.toArray(null);
638 >            shouldThrow();
639 >        } catch (NullPointerException success) {}
640      }
641  
642      /**
643       * toArray with incompatible array type throws CCE
644       */
645      public void testToArray1_BadArg() {
646 <        try {
646 >        try {
647              PriorityBlockingQueue q = populatedQueue(SIZE);
648 <            Object o[] = q.toArray(new String[10] );
649 <            shouldThrow();
650 <        } catch (ArrayStoreException  success){}
648 >            Object o[] = q.toArray(new String[10] );
649 >            shouldThrow();
650 >        } catch (ArrayStoreException success) {}
651      }
652  
653      /**
# Line 741 | Line 656 | public class PriorityBlockingQueueTest e
656      public void testIterator() {
657          PriorityBlockingQueue q = populatedQueue(SIZE);
658          int i = 0;
659 <        Iterator it = q.iterator();
659 >        Iterator it = q.iterator();
660          while (it.hasNext()) {
661              assertTrue(q.contains(it.next()));
662              ++i;
# Line 786 | Line 701 | public class PriorityBlockingQueueTest e
701      public void testPollInExecutor() {
702          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
703          ExecutorService executor = Executors.newFixedThreadPool(2);
704 <        executor.execute(new Runnable() {
705 <            public void run() {
704 >        executor.execute(new CheckedRunnable() {
705 >            public void realRun() throws InterruptedException {
706                  threadAssertNull(q.poll());
707 <                try {
708 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
709 <                    threadAssertTrue(q.isEmpty());
710 <                }
711 <                catch (InterruptedException e) {
712 <                    threadUnexpectedException();
713 <                }
714 <            }
715 <        });
801 <
802 <        executor.execute(new Runnable() {
803 <            public void run() {
804 <                try {
805 <                    Thread.sleep(SMALL_DELAY_MS);
806 <                    q.put(new Integer(1));
807 <                }
808 <                catch (InterruptedException e) {
809 <                    threadUnexpectedException();
810 <                }
811 <            }
812 <        });
707 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
708 >                threadAssertTrue(q.isEmpty());
709 >            }});
710 >
711 >        executor.execute(new CheckedRunnable() {
712 >            public void realRun() throws InterruptedException {
713 >                Thread.sleep(SMALL_DELAY_MS);
714 >                q.put(new Integer(1));
715 >            }});
716  
717          joinPool(executor);
718      }
# Line 817 | Line 720 | public class PriorityBlockingQueueTest e
720      /**
721       * A deserialized serialized queue has same elements
722       */
723 <    public void testSerialization() {
723 >    public void testSerialization() throws Exception {
724          PriorityBlockingQueue q = populatedQueue(SIZE);
725 <        try {
726 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
727 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
728 <            out.writeObject(q);
729 <            out.close();
730 <
731 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
732 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
733 <            PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
734 <            assertEquals(q.size(), r.size());
735 <            while (!q.isEmpty())
833 <                assertEquals(q.remove(), r.remove());
834 <        } catch (Exception e){
835 <            unexpectedException();
836 <        }
725 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
726 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
727 >        out.writeObject(q);
728 >        out.close();
729 >
730 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
731 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
732 >        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
733 >        assertEquals(q.size(), r.size());
734 >        while (!q.isEmpty())
735 >            assertEquals(q.remove(), r.remove());
736      }
737  
738      /**
# Line 844 | Line 743 | public class PriorityBlockingQueueTest e
743          try {
744              q.drainTo(null);
745              shouldThrow();
746 <        } catch (NullPointerException success) {
848 <        }
746 >        } catch (NullPointerException success) {}
747      }
748  
749      /**
# Line 856 | Line 754 | public class PriorityBlockingQueueTest e
754          try {
755              q.drainTo(q);
756              shouldThrow();
757 <        } catch (IllegalArgumentException success) {
860 <        }
757 >        } catch (IllegalArgumentException success) {}
758      }
759  
760      /**
# Line 887 | Line 784 | public class PriorityBlockingQueueTest e
784      /**
785       * drainTo empties queue
786       */
787 <    public void testDrainToWithActivePut() {
787 >    public void testDrainToWithActivePut() throws InterruptedException {
788          final PriorityBlockingQueue q = populatedQueue(SIZE);
789 <        Thread t = new Thread(new Runnable() {
790 <                public void run() {
791 <                    q.put(new Integer(SIZE+1));
792 <                }
793 <            });
794 <        try {
795 <            t.start();
796 <            ArrayList l = new ArrayList();
797 <            q.drainTo(l);
798 <            assertTrue(l.size() >= SIZE);
799 <            for (int i = 0; i < SIZE; ++i)
800 <                assertEquals(l.get(i), new Integer(i));
801 <            t.join();
905 <            assertTrue(q.size() + l.size() >= SIZE);
906 <        } catch (Exception e){
907 <            unexpectedException();
908 <        }
789 >        Thread t = new Thread(new CheckedRunnable() {
790 >            public void realRun() {
791 >                q.put(new Integer(SIZE+1));
792 >            }});
793 >
794 >        t.start();
795 >        ArrayList l = new ArrayList();
796 >        q.drainTo(l);
797 >        assertTrue(l.size() >= SIZE);
798 >        for (int i = 0; i < SIZE; ++i)
799 >            assertEquals(l.get(i), new Integer(i));
800 >        t.join();
801 >        assertTrue(q.size() + l.size() >= SIZE);
802      }
803  
804      /**
# Line 916 | Line 809 | public class PriorityBlockingQueueTest e
809          try {
810              q.drainTo(null, 0);
811              shouldThrow();
812 <        } catch (NullPointerException success) {
920 <        }
812 >        } catch (NullPointerException success) {}
813      }
814  
815      /**
# Line 928 | Line 820 | public class PriorityBlockingQueueTest e
820          try {
821              q.drainTo(q, 0);
822              shouldThrow();
823 <        } catch (IllegalArgumentException success) {
932 <        }
823 >        } catch (IllegalArgumentException success) {}
824      }
825  
826      /**
# Line 951 | Line 842 | public class PriorityBlockingQueueTest e
842          }
843      }
844  
954
845   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines