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

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.19 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.20 by jsr166, Sat Nov 21 09:28:16 2009 UTC

# Line 144 | Line 144 | public class DelayQueueTest extends JSR1
144          try {
145              DelayQueue q = new DelayQueue(null);
146              shouldThrow();
147 <        }
148 <        catch (NullPointerException success) {}
147 >        } catch (NullPointerException success) {}
148      }
149  
150      /**
# Line 156 | Line 155 | public class DelayQueueTest extends JSR1
155              PDelay[] ints = new PDelay[SIZE];
156              DelayQueue q = new DelayQueue(Arrays.asList(ints));
157              shouldThrow();
158 <        }
160 <        catch (NullPointerException success) {}
158 >        } catch (NullPointerException success) {}
159      }
160  
161      /**
# Line 170 | Line 168 | public class DelayQueueTest extends JSR1
168                  ints[i] = new PDelay(i);
169              DelayQueue q = new DelayQueue(Arrays.asList(ints));
170              shouldThrow();
171 <        }
174 <        catch (NullPointerException success) {}
171 >        } catch (NullPointerException success) {}
172      }
173  
174      /**
# Line 230 | Line 227 | public class DelayQueueTest extends JSR1
227              DelayQueue q = new DelayQueue();
228              q.offer(null);
229              shouldThrow();
230 <        } catch (NullPointerException success) { }
230 >        } catch (NullPointerException success) {}
231      }
232  
233      /**
# Line 241 | Line 238 | public class DelayQueueTest extends JSR1
238              DelayQueue q = new DelayQueue();
239              q.add(null);
240              shouldThrow();
241 <        } catch (NullPointerException success) { }
241 >        } catch (NullPointerException success) {}
242      }
243  
244      /**
# Line 272 | Line 269 | public class DelayQueueTest extends JSR1
269              DelayQueue q = new DelayQueue();
270              q.addAll(null);
271              shouldThrow();
272 <        }
276 <        catch (NullPointerException success) {}
272 >        } catch (NullPointerException success) {}
273      }
274  
275  
# Line 285 | Line 281 | public class DelayQueueTest extends JSR1
281              DelayQueue q = populatedQueue(SIZE);
282              q.addAll(q);
283              shouldThrow();
284 <        }
289 <        catch (IllegalArgumentException success) {}
284 >        } catch (IllegalArgumentException success) {}
285      }
286  
287      /**
# Line 298 | Line 293 | public class DelayQueueTest extends JSR1
293              PDelay[] ints = new PDelay[SIZE];
294              q.addAll(Arrays.asList(ints));
295              shouldThrow();
296 <        }
302 <        catch (NullPointerException success) {}
296 >        } catch (NullPointerException success) {}
297      }
298      /**
299       * addAll of a collection with any null elements throws NPE after
# Line 313 | Line 307 | public class DelayQueueTest extends JSR1
307                  ints[i] = new PDelay(i);
308              q.addAll(Arrays.asList(ints));
309              shouldThrow();
310 <        }
317 <        catch (NullPointerException success) {}
310 >        } catch (NullPointerException success) {}
311      }
312  
313      /**
# Line 343 | Line 336 | public class DelayQueueTest extends JSR1
336              DelayQueue q = new DelayQueue();
337              q.put(null);
338              shouldThrow();
339 <        }
347 <        catch (NullPointerException success) {
348 <        }
339 >        } catch (NullPointerException success) {}
340       }
341  
342      /**
343       * all elements successfully put are contained
344       */
345       public void testPut() {
346 <         try {
347 <             DelayQueue q = new DelayQueue();
348 <             for (int i = 0; i < SIZE; ++i) {
349 <                 PDelay I = new PDelay(i);
350 <                 q.put(I);
360 <                 assertTrue(q.contains(I));
361 <             }
362 <             assertEquals(SIZE, q.size());
346 >         DelayQueue q = new DelayQueue();
347 >         for (int i = 0; i < SIZE; ++i) {
348 >             PDelay I = new PDelay(i);
349 >             q.put(I);
350 >             assertTrue(q.contains(I));
351           }
352 <         finally {
365 <        }
352 >         assertEquals(SIZE, q.size());
353      }
354  
355      /**
356       * put doesn't block waiting for take
357       */
358 <    public void testPutWithTake() {
358 >    public void testPutWithTake() throws InterruptedException {
359          final DelayQueue q = new DelayQueue();
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run() {
362 <                    int added = 0;
363 <                    try {
364 <                        q.put(new PDelay(0));
365 <                        ++added;
366 <                        q.put(new PDelay(0));
367 <                        ++added;
368 <                        q.put(new PDelay(0));
369 <                        ++added;
370 <                        q.put(new PDelay(0));
371 <                        ++added;
372 <                        threadAssertTrue(added == 4);
386 <                    } finally {
387 <                    }
388 <                }
389 <            });
390 <        try {
391 <            t.start();
392 <            Thread.sleep(SHORT_DELAY_MS);
393 <            q.take();
394 <            t.interrupt();
395 <            t.join();
396 <        } catch (Exception e) {
397 <            unexpectedException();
398 <        }
360 >        Thread t = new Thread(new CheckedRunnable() {
361 >            public void realRun() {
362 >                q.put(new PDelay(0));
363 >                q.put(new PDelay(0));
364 >                q.put(new PDelay(0));
365 >                q.put(new PDelay(0));
366 >            }});
367 >
368 >        t.start();
369 >        Thread.sleep(SHORT_DELAY_MS);
370 >        q.take();
371 >        t.interrupt();
372 >        t.join();
373      }
374  
375      /**
376       * timed offer does not time out
377       */
378 <    public void testTimedOffer() {
378 >    public void testTimedOffer() throws InterruptedException {
379          final DelayQueue q = new DelayQueue();
380 <        Thread t = new Thread(new Runnable() {
381 <                public void run() {
382 <                    try {
383 <                        q.put(new PDelay(0));
384 <                        q.put(new PDelay(0));
385 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
386 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
413 <                    } finally { }
414 <                }
415 <            });
380 >        Thread t = new Thread(new CheckedRunnable() {
381 >            public void realRun() throws InterruptedException {
382 >                q.put(new PDelay(0));
383 >                q.put(new PDelay(0));
384 >                threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
385 >                threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
386 >            }});
387  
388 <        try {
389 <            t.start();
390 <            Thread.sleep(SMALL_DELAY_MS);
391 <            t.interrupt();
421 <            t.join();
422 <        } catch (Exception e) {
423 <            unexpectedException();
424 <        }
388 >        t.start();
389 >        Thread.sleep(SMALL_DELAY_MS);
390 >        t.interrupt();
391 >        t.join();
392      }
393  
394      /**
395       * take retrieves elements in priority order
396       */
397 <    public void testTake() {
398 <        try {
399 <            DelayQueue q = populatedQueue(SIZE);
400 <            for (int i = 0; i < SIZE; ++i) {
434 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
435 <            }
436 <        } catch (InterruptedException e) {
437 <            unexpectedException();
397 >    public void testTake() throws InterruptedException {
398 >        DelayQueue q = populatedQueue(SIZE);
399 >        for (int i = 0; i < SIZE; ++i) {
400 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
401          }
402      }
403  
404      /**
405       * take blocks interruptibly when empty
406       */
407 <    public void testTakeFromEmpty() {
407 >    public void testTakeFromEmpty() throws InterruptedException {
408          final DelayQueue q = new DelayQueue();
409 <        Thread t = new Thread(new Runnable() {
410 <                public void run() {
411 <                    try {
412 <                        q.take();
413 <                        threadShouldThrow();
414 <                    } catch (InterruptedException success) { }
415 <                }
416 <            });
417 <        try {
455 <            t.start();
456 <            Thread.sleep(SHORT_DELAY_MS);
457 <            t.interrupt();
458 <            t.join();
459 <        } catch (Exception e) {
460 <            unexpectedException();
461 <        }
409 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
410 >            public void realRun() throws InterruptedException {
411 >                q.take();
412 >            }};
413 >
414 >        t.start();
415 >        Thread.sleep(SHORT_DELAY_MS);
416 >        t.interrupt();
417 >        t.join();
418      }
419  
420      /**
421       * Take removes existing elements until empty, then blocks interruptibly
422       */
423 <    public void testBlockingTake() {
424 <        Thread t = new Thread(new Runnable() {
425 <                public void run() {
426 <                    try {
427 <                        DelayQueue q = populatedQueue(SIZE);
428 <                        for (int i = 0; i < SIZE; ++i) {
429 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
430 <                        }
431 <                        q.take();
432 <                        threadShouldThrow();
477 <                    } catch (InterruptedException success) {
478 <                    }
479 <                }});
423 >    public void testBlockingTake() throws InterruptedException {
424 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
425 >            public void realRun() throws InterruptedException {
426 >                DelayQueue q = populatedQueue(SIZE);
427 >                for (int i = 0; i < SIZE; ++i) {
428 >                    threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
429 >                }
430 >                q.take();
431 >            }};
432 >
433          t.start();
434 <        try {
435 <           Thread.sleep(SHORT_DELAY_MS);
436 <           t.interrupt();
484 <           t.join();
485 <        }
486 <        catch (InterruptedException ie) {
487 <            unexpectedException();
488 <        }
434 >        Thread.sleep(SHORT_DELAY_MS);
435 >        t.interrupt();
436 >        t.join();
437      }
438  
439  
# Line 503 | Line 451 | public class DelayQueueTest extends JSR1
451      /**
452       * timed pool with zero timeout succeeds when non-empty, else times out
453       */
454 <    public void testTimedPoll0() {
455 <        try {
456 <            DelayQueue q = populatedQueue(SIZE);
457 <            for (int i = 0; i < SIZE; ++i) {
510 <                assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
511 <            }
512 <            assertNull(q.poll(0, MILLISECONDS));
513 <        } catch (InterruptedException e) {
514 <            unexpectedException();
454 >    public void testTimedPoll0() throws InterruptedException {
455 >        DelayQueue q = populatedQueue(SIZE);
456 >        for (int i = 0; i < SIZE; ++i) {
457 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
458          }
459 +        assertNull(q.poll(0, MILLISECONDS));
460      }
461  
462      /**
463       * timed pool with nonzero timeout succeeds when non-empty, else times out
464       */
465 <    public void testTimedPoll() {
466 <        try {
467 <            DelayQueue q = populatedQueue(SIZE);
468 <            for (int i = 0; i < SIZE; ++i) {
525 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
526 <            }
527 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
528 <        } catch (InterruptedException e) {
529 <            unexpectedException();
465 >    public void testTimedPoll() throws InterruptedException {
466 >        DelayQueue q = populatedQueue(SIZE);
467 >        for (int i = 0; i < SIZE; ++i) {
468 >            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
469          }
470 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
471      }
472  
473      /**
474       * Interrupted timed poll throws InterruptedException instead of
475       * returning timeout status
476       */
477 <    public void testInterruptedTimedPoll() {
478 <        Thread t = new Thread(new Runnable() {
479 <                public void run() {
480 <                    try {
481 <                        DelayQueue q = populatedQueue(SIZE);
482 <                        for (int i = 0; i < SIZE; ++i) {
483 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
484 <                        }
485 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
486 <                    } catch (InterruptedException success) {
487 <                    }
488 <                }});
477 >    public void testInterruptedTimedPoll() throws InterruptedException {
478 >        Thread t = new Thread(new CheckedRunnable() {
479 >            public void realRun() throws InterruptedException {
480 >                DelayQueue q = populatedQueue(SIZE);
481 >                for (int i = 0; i < SIZE; ++i) {
482 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
483 >                }
484 >                try {
485 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
486 >                    shouldThrow();
487 >                } catch (InterruptedException success) {}
488 >            }});
489 >
490          t.start();
491 <        try {
492 <           Thread.sleep(SHORT_DELAY_MS);
493 <           t.interrupt();
553 <           t.join();
554 <        }
555 <        catch (InterruptedException ie) {
556 <            unexpectedException();
557 <        }
491 >        Thread.sleep(SHORT_DELAY_MS);
492 >        t.interrupt();
493 >        t.join();
494      }
495  
496      /**
497       *  timed poll before a delayed offer fails; after offer succeeds;
498       *  on interruption throws
499       */
500 <    public void testTimedPollWithOffer() {
500 >    public void testTimedPollWithOffer() throws InterruptedException {
501          final DelayQueue q = new DelayQueue();
502 <        Thread t = new Thread(new Runnable() {
503 <                public void run() {
504 <                    try {
505 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
506 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
507 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
508 <                        threadFail("Should block");
509 <                    } catch (InterruptedException success) { }
510 <                }
511 <            });
512 <        try {
513 <            t.start();
514 <            Thread.sleep(SMALL_DELAY_MS);
515 <            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
516 <            t.interrupt();
581 <            t.join();
582 <        } catch (Exception e) {
583 <            unexpectedException();
584 <        }
502 >        Thread t = new Thread(new CheckedRunnable() {
503 >            public void realRun() throws InterruptedException {
504 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
505 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
506 >                try {
507 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
508 >                    shouldThrow();
509 >                } catch (InterruptedException success) {}
510 >            }});
511 >
512 >        t.start();
513 >        Thread.sleep(SMALL_DELAY_MS);
514 >        assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
515 >        t.interrupt();
516 >        t.join();
517      }
518  
519  
# Line 613 | Line 545 | public class DelayQueueTest extends JSR1
545          try {
546              q.element();
547              shouldThrow();
548 <        }
617 <        catch (NoSuchElementException success) {}
548 >        } catch (NoSuchElementException success) {}
549      }
550  
551      /**
# Line 628 | Line 559 | public class DelayQueueTest extends JSR1
559          try {
560              q.remove();
561              shouldThrow();
562 <        } catch (NoSuchElementException success) {
632 <        }
562 >        } catch (NoSuchElementException success) {}
563      }
564  
565      /**
# Line 728 | Line 658 | public class DelayQueueTest extends JSR1
658      /**
659       * toArray contains all elements
660       */
661 <    public void testToArray() {
661 >    public void testToArray() throws InterruptedException {
662          DelayQueue q = populatedQueue(SIZE);
663          Object[] o = q.toArray();
664          Arrays.sort(o);
735        try {
665          for (int i = 0; i < o.length; i++)
666              assertEquals(o[i], q.take());
738        } catch (InterruptedException e) {
739            unexpectedException();
740        }
667      }
668  
669      /**
670       * toArray(a) contains all elements
671       */
672 <    public void testToArray2() {
672 >    public void testToArray2() throws InterruptedException {
673          DelayQueue q = populatedQueue(SIZE);
674          PDelay[] ints = new PDelay[SIZE];
675          ints = (PDelay[])q.toArray(ints);
676          Arrays.sort(ints);
677 <        try {
678 <            for (int i = 0; i < ints.length; i++)
753 <                assertEquals(ints[i], q.take());
754 <        } catch (InterruptedException e) {
755 <            unexpectedException();
756 <        }
677 >        for (int i = 0; i < ints.length; i++)
678 >            assertEquals(ints[i], q.take());
679      }
680  
681  
# Line 776 | Line 698 | public class DelayQueueTest extends JSR1
698              DelayQueue q = populatedQueue(SIZE);
699              Object o[] = q.toArray(new String[10] );
700              shouldThrow();
701 <        } catch (ArrayStoreException  success) {}
701 >        } catch (ArrayStoreException success) {}
702      }
703  
704      /**
# Line 828 | Line 750 | public class DelayQueueTest extends JSR1
750      public void testPollInExecutor() {
751          final DelayQueue q = new DelayQueue();
752          ExecutorService executor = Executors.newFixedThreadPool(2);
753 <        executor.execute(new Runnable() {
754 <            public void run() {
753 >        executor.execute(new CheckedRunnable() {
754 >            public void realRun() throws InterruptedException {
755                  threadAssertNull(q.poll());
756 <                try {
757 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
758 <                    threadAssertTrue(q.isEmpty());
759 <                }
760 <                catch (InterruptedException e) {
761 <                    threadUnexpectedException();
762 <                }
763 <            }
764 <        });
756 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
757 >                threadAssertTrue(q.isEmpty());
758 >            }});
759 >
760 >        executor.execute(new CheckedRunnable() {
761 >            public void realRun() throws InterruptedException {
762 >                Thread.sleep(SHORT_DELAY_MS);
763 >                q.put(new PDelay(1));
764 >            }});
765  
844        executor.execute(new Runnable() {
845            public void run() {
846                try {
847                    Thread.sleep(SHORT_DELAY_MS);
848                    q.put(new PDelay(1));
849                }
850                catch (InterruptedException e) {
851                    threadUnexpectedException();
852                }
853            }
854        });
766          joinPool(executor);
767  
768      }
# Line 860 | Line 771 | public class DelayQueueTest extends JSR1
771      /**
772       * Delayed actions do not occur until their delay elapses
773       */
774 <    public void testDelay() {
774 >    public void testDelay() throws InterruptedException {
775          DelayQueue q = new DelayQueue();
776          NanoDelay[] elements = new NanoDelay[SIZE];
777          for (int i = 0; i < SIZE; ++i) {
# Line 870 | Line 781 | public class DelayQueueTest extends JSR1
781              q.add(elements[i]);
782          }
783  
784 <        try {
785 <            long last = 0;
786 <            for (int i = 0; i < SIZE; ++i) {
787 <                NanoDelay e = (NanoDelay)(q.take());
788 <                long tt = e.getTriggerTime();
789 <                assertTrue(tt <= System.nanoTime());
790 <                if (i != 0)
791 <                    assertTrue(tt >= last);
881 <                last = tt;
882 <            }
883 <        }
884 <        catch (InterruptedException ie) {
885 <            unexpectedException();
784 >        long last = 0;
785 >        for (int i = 0; i < SIZE; ++i) {
786 >            NanoDelay e = (NanoDelay)(q.take());
787 >            long tt = e.getTriggerTime();
788 >            assertTrue(tt <= System.nanoTime());
789 >            if (i != 0)
790 >                assertTrue(tt >= last);
791 >            last = tt;
792          }
793      }
794  
# Line 908 | Line 814 | public class DelayQueueTest extends JSR1
814      /**
815       * timed poll of a non-empty queue returns null if no expired elements.
816       */
817 <    public void testTimedPollDelayed() {
817 >    public void testTimedPollDelayed() throws InterruptedException {
818          DelayQueue q = new DelayQueue();
819          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
820 <        try {
915 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
916 <        } catch (Exception ex) {
917 <            unexpectedException();
918 <        }
820 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
821      }
822  
823      /**
# Line 926 | Line 828 | public class DelayQueueTest extends JSR1
828          try {
829              q.drainTo(null);
830              shouldThrow();
831 <        } catch (NullPointerException success) {
930 <        }
831 >        } catch (NullPointerException success) {}
832      }
833  
834      /**
# Line 938 | Line 839 | public class DelayQueueTest extends JSR1
839          try {
840              q.drainTo(q);
841              shouldThrow();
842 <        } catch (IllegalArgumentException success) {
942 <        }
842 >        } catch (IllegalArgumentException success) {}
843      }
844  
845      /**
# Line 973 | Line 873 | public class DelayQueueTest extends JSR1
873      /**
874       * drainTo empties queue
875       */
876 <    public void testDrainToWithActivePut() {
876 >    public void testDrainToWithActivePut() throws InterruptedException {
877          final DelayQueue q = populatedQueue(SIZE);
878 <        Thread t = new Thread(new Runnable() {
879 <                public void run() {
880 <                    q.put(new PDelay(SIZE+1));
881 <                }
882 <            });
883 <        try {
884 <            t.start();
885 <            ArrayList l = new ArrayList();
886 <            q.drainTo(l);
887 <            assertTrue(l.size() >= SIZE);
888 <            t.join();
989 <            assertTrue(q.size() + l.size() >= SIZE);
990 <        } catch (Exception e) {
991 <            unexpectedException();
992 <        }
878 >        Thread t = new Thread(new CheckedRunnable() {
879 >            public void realRun() {
880 >                q.put(new PDelay(SIZE+1));
881 >            }});
882 >
883 >        t.start();
884 >        ArrayList l = new ArrayList();
885 >        q.drainTo(l);
886 >        assertTrue(l.size() >= SIZE);
887 >        t.join();
888 >        assertTrue(q.size() + l.size() >= SIZE);
889      }
890  
891      /**
# Line 1000 | Line 896 | public class DelayQueueTest extends JSR1
896          try {
897              q.drainTo(null, 0);
898              shouldThrow();
899 <        } catch (NullPointerException success) {
1004 <        }
899 >        } catch (NullPointerException success) {}
900      }
901  
902      /**
# Line 1012 | Line 907 | public class DelayQueueTest extends JSR1
907          try {
908              q.drainTo(q, 0);
909              shouldThrow();
910 <        } catch (IllegalArgumentException success) {
1016 <        }
910 >        } catch (IllegalArgumentException success) {}
911      }
912  
913      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines