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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.5 by jsr166, Sun Aug 2 08:17:31 2009 UTC vs.
Revision 1.9 by jsr166, Wed Aug 5 00:43:23 2009 UTC

# Line 17 | Line 17 | import java.util.ConcurrentModificationE
17   import java.util.Iterator;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
23  
# Line 31 | Line 32 | public class LinkedTransferQueueTest ext
32      }
33  
34      /**
35 <     * Constructor builds new queue with size being zero and empty being true
35 >     * Constructor builds new queue with size being zero and empty
36 >     * being true
37       */
38      public void testConstructor1() {
39          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 41 | public class LinkedTransferQueueTest ext
41      }
42  
43      /**
44 <     * Initializing constructor with null collection throws NullPointerException
44 >     * Initializing constructor with null collection throws
45 >     * NullPointerException
46       */
47      public void testConstructor2() {
48          try {
# Line 50 | Line 53 | public class LinkedTransferQueueTest ext
53      }
54  
55      /**
56 <     * Initializing from Collection of null elements throws NullPointerException
56 >     * Initializing from Collection of null elements throws
57 >     * NullPointerException
58       */
59      public void testConstructor3() {
60          try {
61              Integer[] ints = new Integer[SIZE];
62 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
62 >            new LinkedTransferQueue(Arrays.asList(ints));
63              shouldThrow();
64          } catch (NullPointerException success) {
65          }
# Line 71 | Line 75 | public class LinkedTransferQueueTest ext
75              for (int i = 0; i < SIZE - 1; ++i) {
76                  ints[i] = i;
77              }
78 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(Arrays.asList(ints));
79              shouldThrow();
80          } catch (NullPointerException success) {
81          }
# Line 86 | Line 90 | public class LinkedTransferQueueTest ext
90              for (int i = 0; i < SIZE; ++i) {
91                  ints[i] = i;
92              }
93 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
93 >            LinkedTransferQueue q
94 >                = new LinkedTransferQueue(Arrays.asList(ints));
95              for (int i = 0; i < SIZE; ++i) {
96                  assertEquals(ints[i], q.poll());
97              }
# Line 173 | Line 178 | public class LinkedTransferQueueTest ext
178      }
179  
180      /**
181 <     * addAll of a collection with any null elements throws NullPointerException after
182 <     * possibly adding some elements
181 >     * addAll of a collection with any null elements throws
182 >     * NullPointerException after possibly adding some elements
183       */
184      public void testAddAll3() {
185          try {
# Line 244 | Line 249 | public class LinkedTransferQueueTest ext
249       */
250      public void testTakeFromEmpty() throws InterruptedException {
251          final LinkedTransferQueue q = new LinkedTransferQueue();
252 <        Thread t = new Thread(new Runnable() {
253 <            public void run() {
254 <                try {
250 <                    q.take();
251 <                    threadShouldThrow();
252 <                } catch (InterruptedException success) {
253 <                } catch (Throwable ex) {
254 <                    threadUnexpectedException(ex);
255 <                }
252 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
253 >            void realRun() throws InterruptedException {
254 >                q.take();
255              }});
257        t.start();
256          Thread.sleep(SHORT_DELAY_MS);
257          t.interrupt();
258          t.join();
# Line 264 | Line 262 | public class LinkedTransferQueueTest ext
262       * Take removes existing elements until empty, then blocks interruptibly
263       */
264      public void testBlockingTake() throws InterruptedException {
265 <        Thread t = new Thread(new Runnable() {
266 <            public void run() {
267 <                try {
268 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
269 <                    for (int i = 0; i < SIZE; ++i) {
272 <                        threadAssertEquals(i, (int) q.take());
273 <                    }
274 <                    q.take();
275 <                    threadShouldThrow();
276 <                } catch (InterruptedException success) {
277 <                } catch (Throwable ex) {
278 <                    threadUnexpectedException(ex);
265 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
266 >            void realRun() throws InterruptedException {
267 >                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
268 >                for (int i = 0; i < SIZE; ++i) {
269 >                    threadAssertEquals(i, (int) q.take());
270                  }
271 +                q.take();
272              }});
281        t.start();
273          Thread.sleep(SHORT_DELAY_MS);
274          t.interrupt();
275          t.join();
# Line 301 | Line 292 | public class LinkedTransferQueueTest ext
292      public void testTimedPoll0() throws InterruptedException {
293          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
294          for (int i = 0; i < SIZE; ++i) {
295 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
295 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
296          }
297 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
297 >        assertNull(q.poll(0, MILLISECONDS));
298      }
299  
300      /**
# Line 312 | Line 303 | public class LinkedTransferQueueTest ext
303      public void testTimedPoll() throws InterruptedException {
304          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
306 >            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS));
307          }
308 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
308 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
309      }
310  
311      /**
# Line 322 | Line 313 | public class LinkedTransferQueueTest ext
313       * returning timeout status
314       */
315      public void testInterruptedTimedPoll() throws InterruptedException {
316 <        Thread t = new Thread(new Runnable() {
317 <            public void run() {
318 <                try {
319 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
320 <                    for (int i = 0; i < SIZE; ++i) {
321 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
331 <                    }
332 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
333 <                } catch (InterruptedException success) {
334 <                } catch (Throwable ex) {
335 <                    threadUnexpectedException(ex);
316 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
317 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
318 >            void realRun() throws InterruptedException {
319 >                for (int i = 0; i < SIZE; ++i) {
320 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
321 >                                                       MILLISECONDS));
322                  }
323 +                q.poll(LONG_DELAY_MS, MILLISECONDS);
324              }});
325 <        t.start();
339 <        Thread.sleep(SHORT_DELAY_MS);
325 >        Thread.sleep(SMALL_DELAY_MS);
326          t.interrupt();
327          t.join();
328 +        assertEquals(q.size(), 0);
329 +        assertNull(q.poll());
330      }
331  
332      /**
# Line 347 | Line 335 | public class LinkedTransferQueueTest ext
335       */
336      public void testTimedPollWithOffer() throws InterruptedException {
337          final LinkedTransferQueue q = new LinkedTransferQueue();
338 <        Thread t = new Thread(new Runnable() {
339 <            public void run() {
340 <                try {
341 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
342 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
355 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
338 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
339 >            void realRun() throws InterruptedException {
340 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
342 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
343              }});
362        t.start();
344          Thread.sleep(SMALL_DELAY_MS);
345 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
345 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
346          t.interrupt();
347          t.join();
348      }
# Line 482 | Line 463 | public class LinkedTransferQueueTest ext
463      }
464  
465      /**
466 <     * retainAll(c) retains only those elements of c and reports true if changed
466 >     * retainAll(c) retains only those elements of c and reports true
467 >     * if changed
468       */
469      public void testRetainAll() {
470          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 483 | public class LinkedTransferQueueTest ext
483      }
484  
485      /**
486 <     * removeAll(c) removes only those elements of c and reports true if changed
486 >     * removeAll(c) removes only those elements of c and reports true
487 >     * if changed
488       */
489      public void testRemoveAll() {
490          for (int i = 1; i < SIZE; ++i) {
# Line 596 | Line 579 | public class LinkedTransferQueueTest ext
579       * iterator ordering is FIFO
580       */
581      public void testIteratorOrdering() {
582 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
582 >        final LinkedTransferQueue<Integer> q
583 >            = new LinkedTransferQueue<Integer>();
584          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
585          q.add(one);
586          q.add(two);
# Line 643 | Line 627 | public class LinkedTransferQueueTest ext
627          q.add(one);
628          q.add(two);
629          ExecutorService executor = Executors.newFixedThreadPool(2);
630 <        executor.execute(new Runnable() {
631 <            public void run() {
632 <                try {
633 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
634 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
630 >
631 >        executor.execute(new CheckedRunnable() {
632 >            void realRun() {
633 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
634 >                                         MILLISECONDS));
635              }});
636  
637 <        executor.execute(new Runnable() {
638 <            public void run() {
639 <                try {
640 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
637 >        executor.execute(new CheckedRunnable() {
638 >            void realRun() throws InterruptedException {
639 >                Thread.sleep(SMALL_DELAY_MS);
640 >                threadAssertEquals(one, q.take());
641              }});
642  
643          joinPool(executor);
# Line 671 | Line 649 | public class LinkedTransferQueueTest ext
649      public void testPollInExecutor() {
650          final LinkedTransferQueue q = new LinkedTransferQueue();
651          ExecutorService executor = Executors.newFixedThreadPool(2);
652 <        executor.execute(new Runnable() {
653 <            public void run() {
654 <                try {
655 <                    threadAssertNull(q.poll());
656 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
657 <                    threadAssertTrue(q.isEmpty());
658 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
652 >
653 >        executor.execute(new CheckedRunnable() {
654 >            void realRun() throws InterruptedException {
655 >                threadAssertNull(q.poll());
656 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
657 >                                                MILLISECONDS));
658 >                threadAssertTrue(q.isEmpty());
659              }});
660  
661 <        executor.execute(new Runnable() {
662 <            public void run() {
663 <                try {
664 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
661 >        executor.execute(new CheckedRunnable() {
662 >            void realRun() throws InterruptedException {
663 >                Thread.sleep(SMALL_DELAY_MS);
664 >                q.put(one);
665              }});
666  
667          joinPool(executor);
# Line 702 | Line 674 | public class LinkedTransferQueueTest ext
674          LinkedTransferQueue q = populatedQueue(SIZE);
675  
676          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
677 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
677 >        ObjectOutputStream out
678 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
679          out.writeObject(q);
680          out.close();
681  
682 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
683 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
682 >        ByteArrayInputStream bin
683 >            = new ByteArrayInputStream(bout.toByteArray());
684 >        ObjectInputStream in
685 >            = new ObjectInputStream(new BufferedInputStream(bin));
686          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
687  
688          assertEquals(q.size(), r.size());
# Line 771 | Line 746 | public class LinkedTransferQueueTest ext
746       */
747      public void testDrainToWithActivePut() throws InterruptedException {
748          final LinkedTransferQueue q = populatedQueue(SIZE);
749 <        Thread t = new Thread(new Runnable() {
750 <            public void run() {
751 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
749 >        Thread t = newStartedThread(new CheckedRunnable() {
750 >            void realRun() {
751 >                q.put(SIZE + 1);
752              }});
782        t.start();
753          ArrayList l = new ArrayList();
754          q.drainTo(l);
755          assertTrue(l.size() >= SIZE);
# Line 837 | Line 807 | public class LinkedTransferQueueTest ext
807      }
808  
809      /**
810 <     * poll and take should decrement the waiting consumer count
810 >     * poll and take decrement the waiting consumer count
811       */
812      public void testWaitingConsumer() throws InterruptedException {
813          final LinkedTransferQueue q = new LinkedTransferQueue();
814          final ConsumerObserver waiting = new ConsumerObserver();
815 <        new Thread(new Runnable() {
816 <            public void run() {
817 <                try {
818 <                    threadAssertTrue(q.hasWaitingConsumer());
819 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
820 <                    threadAssertTrue(q.offer(new Object()));
821 <                } catch (Throwable ex) {
822 <                    threadUnexpectedException(ex);
823 <                }
824 <            }}).start();
825 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
826 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
815 >
816 >        Thread t = newStartedThread(new CheckedRunnable() {
817 >            void realRun() throws InterruptedException {
818 >                Thread.sleep(SMALL_DELAY_MS);
819 >                threadAssertTrue(q.hasWaitingConsumer());
820 >                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
821 >                threadAssertTrue(q.offer(new Object()));
822 >            }});
823 >
824 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
825 >        assertTrue(q.getWaitingConsumerCount()
826 >                   < waiting.getWaitingConsumers());
827 >        t.join();
828      }
829  
830      /**
# Line 869 | Line 840 | public class LinkedTransferQueueTest ext
840      }
841  
842      /**
843 <     * transfer attempts to insert into the queue then wait until that
844 <     * object is removed via take or poll.
843 >     * transfer waits until a poll occurs. The transfered element
844 >     * is returned by this associated poll.
845       */
846      public void testTransfer2() throws InterruptedException {
847 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
847 >        final LinkedTransferQueue<Integer> q
848 >            = new LinkedTransferQueue<Integer>();
849  
850 <        new Thread(new Runnable() {
851 <            public void run() {
852 <                try {
853 <                    q.transfer(SIZE);
854 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
850 >        Thread t = newStartedThread(new CheckedRunnable() {
851 >            void realRun() throws InterruptedException {
852 >                q.transfer(SIZE);
853 >                threadAssertTrue(q.isEmpty());
854 >            }});
855  
856          Thread.sleep(SHORT_DELAY_MS);
857          assertEquals(1, q.size());
858          assertEquals(SIZE, (int) q.poll());
859          assertTrue(q.isEmpty());
860 +        t.join();
861      }
862  
863      /**
864 <     * transfer will attempt to transfer in fifo order and continue
896 <     * waiting if the element being transfered is not polled or taken
864 >     * transfer waits until a poll occurs, and then transfers in fifo order
865       */
866      public void testTransfer3() throws InterruptedException {
867 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
867 >        final LinkedTransferQueue<Integer> q
868 >            = new LinkedTransferQueue<Integer>();
869  
870 <        Thread first =
871 <            new Thread(new Runnable() {
872 <                public void run() {
873 <                    try {
874 <                        Integer i = SIZE + 1;
875 <                        q.transfer(i);
876 <                        threadAssertTrue(!q.contains(i));
908 <                        threadAssertEquals(1, q.size());
909 <                    } catch (Throwable ex) {
910 <                        threadUnexpectedException(ex);
911 <                    }
912 <                }});
913 <        first.start();
870 >        Thread first = newStartedThread(new CheckedRunnable() {
871 >            void realRun() throws InterruptedException {
872 >                Integer i = SIZE + 1;
873 >                q.transfer(i);
874 >                threadAssertTrue(!q.contains(i));
875 >                threadAssertEquals(1, q.size());
876 >            }});
877  
878 <        Thread interruptedThread =
879 <            new Thread(new Runnable() {
880 <                public void run() {
881 <                    try {
882 <                        while (q.size() == 0)
883 <                            Thread.yield();
921 <                        q.transfer(SIZE);
922 <                        threadShouldThrow();
923 <                    } catch (InterruptedException success) {
924 <                    } catch (Throwable ex) {
925 <                        threadUnexpectedException(ex);
926 <                    }
878 >        Thread interruptedThread = newStartedThread(
879 >            new CheckedInterruptedRunnable() {
880 >                void realRun() throws InterruptedException {
881 >                    while (q.size() == 0)
882 >                        Thread.yield();
883 >                    q.transfer(SIZE);
884                  }});
928        interruptedThread.start();
885  
886          while (q.size() < 2)
887              Thread.yield();
# Line 940 | Line 896 | public class LinkedTransferQueueTest ext
896      }
897  
898      /**
899 <     * transfer will wait as long as a poll or take occurs if one does occur
900 <     * the waiting is finished and the thread that tries to poll/take
945 <     * wins in retrieving the element
899 >     * transfer waits until a poll occurs, at which point the polling
900 >     * thread returns the element
901       */
902      public void testTransfer4() throws InterruptedException {
903          final LinkedTransferQueue q = new LinkedTransferQueue();
904 <        new Thread(new Runnable() {
905 <            public void run() {
906 <                try {
907 <                    q.transfer(four);
908 <                    threadAssertFalse(q.contains(four));
909 <                    threadAssertEquals(three, q.poll());
910 <                } catch (Throwable ex) {
911 <                    threadUnexpectedException(ex);
912 <                }
958 <            }}).start();
959 <        Thread.sleep(MEDIUM_DELAY_MS);
904 >
905 >        Thread t = newStartedThread(new CheckedRunnable() {
906 >            void realRun() throws InterruptedException {
907 >                q.transfer(four);
908 >                threadAssertFalse(q.contains(four));
909 >                threadAssertEquals(three, q.poll());
910 >            }});
911 >
912 >        Thread.sleep(SHORT_DELAY_MS);
913          assertTrue(q.offer(three));
914          assertEquals(four, q.poll());
915 +        t.join();
916 +    }
917 +
918 +    /**
919 +     * transfer waits until a take occurs. The transfered element
920 +     * is returned by this associated take.
921 +     */
922 +    public void testTransfer5() throws InterruptedException {
923 +        final LinkedTransferQueue<Integer> q
924 +            = new LinkedTransferQueue<Integer>();
925 +
926 +        Thread t = newStartedThread(new CheckedRunnable() {
927 +            void realRun() throws InterruptedException {
928 +                q.transfer(SIZE);
929 +                threadAssertTrue(q.isEmpty());
930 +            }});
931 +
932 +        Thread.sleep(SHORT_DELAY_MS);
933 +        assertEquals(SIZE, (int) q.take());
934 +        assertTrue(q.isEmpty());
935 +        t.join();
936      }
937  
938      /**
# Line 968 | Line 942 | public class LinkedTransferQueueTest ext
942          try {
943              final LinkedTransferQueue q = new LinkedTransferQueue();
944              q.tryTransfer(null);
945 <            this.shouldThrow();
945 >            shouldThrow();
946          } catch (NullPointerException ex) {
947          }
948      }
# Line 992 | Line 966 | public class LinkedTransferQueueTest ext
966      public void testTryTransfer3() throws InterruptedException {
967          final Object hotPotato = new Object();
968          final LinkedTransferQueue q = new LinkedTransferQueue();
969 <        new Thread(new Runnable() {
970 <            public void run() {
971 <                try {
972 <                    while (! q.hasWaitingConsumer())
973 <                        Thread.yield();
974 <                    threadAssertTrue(q.hasWaitingConsumer());
975 <                    threadAssertTrue(q.isEmpty());
976 <                    threadAssertTrue(q.size() == 0);
977 <                    threadAssertTrue(q.tryTransfer(hotPotato));
978 <                } catch (Throwable ex) {
979 <                    threadUnexpectedException(ex);
980 <                }
1007 <            }}).start();
1008 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
969 >
970 >        Thread t = newStartedThread(new CheckedRunnable() {
971 >            void realRun() {
972 >                while (! q.hasWaitingConsumer())
973 >                    Thread.yield();
974 >                threadAssertTrue(q.hasWaitingConsumer());
975 >                threadAssertTrue(q.isEmpty());
976 >                threadAssertTrue(q.size() == 0);
977 >                threadAssertTrue(q.tryTransfer(hotPotato));
978 >            }});
979 >
980 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
981          assertTrue(q.isEmpty());
982 +        t.join();
983      }
984  
985      /**
# Line 1016 | Line 989 | public class LinkedTransferQueueTest ext
989      public void testTryTransfer4() throws InterruptedException {
990          final Object hotPotato = new Object();
991          final LinkedTransferQueue q = new LinkedTransferQueue();
992 <        new Thread(new Runnable() {
993 <            public void run() {
994 <                try {
995 <                    while (! q.hasWaitingConsumer())
996 <                        Thread.yield();
997 <                    threadAssertTrue(q.hasWaitingConsumer());
998 <                    threadAssertTrue(q.isEmpty());
999 <                    threadAssertTrue(q.size() == 0);
1000 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1001 <                } catch (Throwable ex) {
1002 <                    threadUnexpectedException(ex);
1030 <                }
1031 <            }}).start();
992 >
993 >        Thread t = newStartedThread(new CheckedRunnable() {
994 >            void realRun() {
995 >                while (! q.hasWaitingConsumer())
996 >                    Thread.yield();
997 >                threadAssertTrue(q.hasWaitingConsumer());
998 >                threadAssertTrue(q.isEmpty());
999 >                threadAssertTrue(q.size() == 0);
1000 >                threadAssertTrue(q.tryTransfer(hotPotato));
1001 >            }});
1002 >
1003          assertTrue(q.take() == hotPotato);
1004          assertTrue(q.isEmpty());
1005 +        t.join();
1006      }
1007  
1008      /**
1009 <     * tryTransfer waits the amount given if interrupted, show an
1010 <     * interrupted exception
1009 >     * tryTransfer waits the amount given if interrupted, and
1010 >     * throws interrupted exception
1011       */
1012      public void testTryTransfer5() throws InterruptedException {
1013          final LinkedTransferQueue q = new LinkedTransferQueue();
1014 <        Thread toInterrupt = new Thread(new Runnable() {
1015 <            public void run() {
1016 <                try {
1017 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1046 <                    threadShouldThrow();
1047 <                } catch (InterruptedException success) {
1048 <                } catch (Throwable ex) {
1049 <                    threadUnexpectedException(ex);
1050 <                }
1014 >
1015 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1016 >            void realRun() throws InterruptedException {
1017 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1018              }});
1019 <        toInterrupt.start();
1019 >
1020          Thread.sleep(SMALL_DELAY_MS);
1021          toInterrupt.interrupt();
1022 +        toInterrupt.join();
1023      }
1024  
1025      /**
# Line 1059 | Line 1027 | public class LinkedTransferQueueTest ext
1027       */
1028      public void testTryTransfer6() throws InterruptedException {
1029          final LinkedTransferQueue q = new LinkedTransferQueue();
1030 <        new Thread(new Runnable() {
1031 <            public void run() {
1032 <                try {
1033 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1034 <                } catch (Throwable ex) {
1035 <                    threadUnexpectedException(ex);
1036 <                }
1037 <            }}).start();
1038 <        Thread.sleep(LONG_DELAY_MS);
1030 >
1031 >        Thread t = newStartedThread(new CheckedRunnable() {
1032 >            void realRun() throws InterruptedException {
1033 >                threadAssertFalse
1034 >                    (q.tryTransfer(new Object(),
1035 >                                   SHORT_DELAY_MS, MILLISECONDS));
1036 >            }});
1037 >
1038 >        Thread.sleep(SMALL_DELAY_MS);
1039          assertTrue(q.isEmpty());
1040 +        t.join();
1041      }
1042  
1043      /**
# Line 1078 | Line 1047 | public class LinkedTransferQueueTest ext
1047      public void testTryTransfer7() throws InterruptedException {
1048          final LinkedTransferQueue q = new LinkedTransferQueue();
1049          assertTrue(q.offer(four));
1050 <        new Thread(new Runnable() {
1051 <            public void run() {
1052 <                try {
1053 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1054 <                    threadAssertTrue(q.isEmpty());
1055 <                } catch (Throwable ex) {
1056 <                    threadUnexpectedException(ex);
1057 <                }
1089 <            }}).start();
1050 >
1051 >        Thread t = newStartedThread(new CheckedRunnable() {
1052 >            void realRun() throws InterruptedException {
1053 >                threadAssertTrue(q.tryTransfer(five,
1054 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1055 >                threadAssertTrue(q.isEmpty());
1056 >            }});
1057 >
1058          Thread.sleep(SHORT_DELAY_MS);
1059          assertEquals(2, q.size());
1060          assertEquals(four, q.poll());
1061          assertEquals(five, q.poll());
1062          assertTrue(q.isEmpty());
1063 +        t.join();
1064      }
1065  
1066      /**
# Line 1102 | Line 1071 | public class LinkedTransferQueueTest ext
1071          final LinkedTransferQueue q = new LinkedTransferQueue();
1072          assertTrue(q.offer(four));
1073          assertEquals(1, q.size());
1074 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1074 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1075          assertEquals(1, q.size());
1076          assertEquals(four, q.poll());
1077          threadAssertTrue(q.isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines