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.41 by jsr166, Thu Nov 18 18:49:44 2010 UTC vs.
Revision 1.54 by jsr166, Wed Dec 31 16:44:02 2014 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include John Vint
6   */
7  
8 < import java.io.BufferedInputStream;
9 < import java.io.BufferedOutputStream;
10 < import java.io.ByteArrayInputStream;
11 < import java.io.ByteArrayOutputStream;
12 < import java.io.ObjectInputStream;
13 < import java.io.ObjectOutputStream;
14 < import java.util.ArrayList;
8 > import junit.framework.*;
9   import java.util.Arrays;
10 + import java.util.ArrayList;
11 + import java.util.Collection;
12   import java.util.Iterator;
13   import java.util.List;
14   import java.util.NoSuchElementException;
15 < import java.util.concurrent.*;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.LinkedTransferQueue;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 import static java.util.concurrent.TimeUnit.NANOSECONDS;
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
22  
23   @SuppressWarnings({"unchecked", "rawtypes"})
24   public class LinkedTransferQueueTest extends JSR166TestCase {
# Line 40 | Line 38 | public class LinkedTransferQueueTest ext
38                              new Generic().testSuite());
39      }
40  
43    void checkEmpty(BlockingQueue q) {
44        try {
45            assertTrue(q.isEmpty());
46            assertEquals(0, q.size());
47            assertNull(q.peek());
48            assertNull(q.poll());
49            assertNull(q.poll(0, MILLISECONDS));
50            assertEquals(q.toString(), "[]");
51            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52            assertFalse(q.iterator().hasNext());
53            try {
54                q.element();
55                shouldThrow();
56            } catch (NoSuchElementException success) {}
57            try {
58                q.iterator().next();
59                shouldThrow();
60            } catch (NoSuchElementException success) {}
61            try {
62                q.remove();
63                shouldThrow();
64            } catch (NoSuchElementException success) {}
65        } catch (InterruptedException ie) {
66            threadUnexpectedException(ie);
67        }
68    }
69
41      /**
42       * Constructor builds new queue with size being zero and empty
43       * being true
# Line 92 | Line 63 | public class LinkedTransferQueueTest ext
63       * NullPointerException
64       */
65      public void testConstructor3() {
66 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
67          try {
68 <            Integer[] ints = new Integer[SIZE];
97 <            new LinkedTransferQueue(Arrays.asList(ints));
68 >            new LinkedTransferQueue(elements);
69              shouldThrow();
70          } catch (NullPointerException success) {}
71      }
# Line 104 | Line 75 | public class LinkedTransferQueueTest ext
75       * throws NullPointerException
76       */
77      public void testConstructor4() {
78 +        Integer[] ints = new Integer[SIZE];
79 +        for (int i = 0; i < SIZE-1; ++i)
80 +            ints[i] = i;
81 +        Collection<Integer> elements = Arrays.asList(ints);
82          try {
83 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE - 1; ++i) {
110 <                ints[i] = i;
111 <            }
112 <            new LinkedTransferQueue(Arrays.asList(ints));
83 >            new LinkedTransferQueue(elements);
84              shouldThrow();
85          } catch (NullPointerException success) {}
86      }
# Line 156 | Line 127 | public class LinkedTransferQueueTest ext
127      }
128  
129      /**
159     * offer(null) throws NullPointerException
160     */
161    public void testOfferNull() {
162        try {
163            LinkedTransferQueue q = new LinkedTransferQueue();
164            q.offer(null);
165            shouldThrow();
166        } catch (NullPointerException success) {}
167    }
168
169    /**
170     * add(null) throws NullPointerException
171     */
172    public void testAddNull() {
173        try {
174            LinkedTransferQueue q = new LinkedTransferQueue();
175            q.add(null);
176            shouldThrow();
177        } catch (NullPointerException success) {}
178    }
179
180    /**
181     * addAll(null) throws NullPointerException
182     */
183    public void testAddAll1() {
184        try {
185            LinkedTransferQueue q = new LinkedTransferQueue();
186            q.addAll(null);
187            shouldThrow();
188        } catch (NullPointerException success) {}
189    }
190
191    /**
130       * addAll(this) throws IllegalArgumentException
131       */
132      public void testAddAllSelf() {
# Line 200 | Line 138 | public class LinkedTransferQueueTest ext
138      }
139  
140      /**
203     * addAll of a collection with null elements throws NullPointerException
204     */
205    public void testAddAll2() {
206        try {
207            LinkedTransferQueue q = new LinkedTransferQueue();
208            Integer[] ints = new Integer[SIZE];
209            q.addAll(Arrays.asList(ints));
210            shouldThrow();
211        } catch (NullPointerException success) {}
212    }
213
214    /**
141       * addAll of a collection with any null elements throws
142       * NullPointerException after possibly adding some elements
143       */
# Line 245 | Line 171 | public class LinkedTransferQueueTest ext
171      }
172  
173      /**
248     * put(null) throws NullPointerException
249     */
250    public void testPutNull() throws InterruptedException {
251        try {
252            LinkedTransferQueue q = new LinkedTransferQueue();
253            q.put(null);
254            shouldThrow();
255        } catch (NullPointerException success) {}
256    }
257
258    /**
174       * all elements successfully put are contained
175       */
176      public void testPut() {
177          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
178          for (int i = 0; i < SIZE; ++i) {
179 <            assertEquals(q.size(), i);
179 >            assertEquals(i, q.size());
180              q.put(i);
181              assertTrue(q.contains(i));
182          }
# Line 281 | Line 196 | public class LinkedTransferQueueTest ext
196       * take removes existing elements until empty, then blocks interruptibly
197       */
198      public void testBlockingTake() throws InterruptedException {
199 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
200 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
199 >        final BlockingQueue q = populatedQueue(SIZE);
200 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
201          Thread t = newStartedThread(new CheckedRunnable() {
202              public void realRun() throws InterruptedException {
203                  for (int i = 0; i < SIZE; ++i) {
204 <                    assertEquals(i, (int) q.take());
204 >                    assertEquals(i, q.take());
205                  }
206 <                aboutToWait.countDown();
206 >
207 >                Thread.currentThread().interrupt();
208 >                try {
209 >                    q.take();
210 >                    shouldThrow();
211 >                } catch (InterruptedException success) {}
212 >                assertFalse(Thread.interrupted());
213 >
214 >                pleaseInterrupt.countDown();
215                  try {
216                      q.take();
217                      shouldThrow();
218                  } catch (InterruptedException success) {}
219 +                assertFalse(Thread.interrupted());
220              }});
221  
222 <        aboutToWait.await();
223 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
222 >        await(pleaseInterrupt);
223 >        assertThreadStaysAlive(t);
224          t.interrupt();
225 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 <        checkEmpty(q);
225 >        awaitTermination(t);
226      }
227  
228      /**
# Line 332 | Line 255 | public class LinkedTransferQueueTest ext
255      public void testTimedPoll() throws InterruptedException {
256          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
257          for (int i = 0; i < SIZE; ++i) {
258 <            long t0 = System.nanoTime();
259 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
260 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
261 <        }
262 <        long t0 = System.nanoTime();
263 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
264 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
258 >            long startTime = System.nanoTime();
259 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
260 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
261 >        }
262 >        long startTime = System.nanoTime();
263 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
264 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
265          checkEmpty(q);
266      }
267  
# Line 444 | Line 367 | public class LinkedTransferQueueTest ext
367      }
368  
369      /**
447     * remove(x) removes x and returns true if present
448     */
449    public void testRemoveElement() throws InterruptedException {
450        LinkedTransferQueue q = populatedQueue(SIZE);
451        for (int i = 1; i < SIZE; i += 2) {
452            assertTrue(q.remove(i));
453        }
454        for (int i = 0; i < SIZE; i += 2) {
455            assertTrue(q.remove(i));
456            assertFalse(q.remove(i + 1));
457        }
458        checkEmpty(q);
459    }
460
461    /**
370       * An add following remove(x) succeeds
371       */
372      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 574 | Line 482 | public class LinkedTransferQueueTest ext
482      }
483  
484      /**
577     * toArray(null) throws NullPointerException
578     */
579    public void testToArray_NullArg() {
580        LinkedTransferQueue q = populatedQueue(SIZE);
581        try {
582            q.toArray(null);
583            shouldThrow();
584        } catch (NullPointerException success) {}
585    }
586
587    /**
485       * toArray(incompatible array type) throws ArrayStoreException
486       */
487      public void testToArray1_BadArg() {
# Line 667 | Line 564 | public class LinkedTransferQueueTest ext
564          LinkedTransferQueue q = populatedQueue(SIZE);
565          String s = q.toString();
566          for (int i = 0; i < SIZE; ++i) {
567 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
567 >            assertTrue(s.contains(String.valueOf(i)));
568          }
569      }
570  
# Line 676 | Line 573 | public class LinkedTransferQueueTest ext
573       */
574      public void testOfferInExecutor() {
575          final LinkedTransferQueue q = new LinkedTransferQueue();
576 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
576 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
577          ExecutorService executor = Executors.newFixedThreadPool(2);
578  
579          executor.execute(new CheckedRunnable() {
580              public void realRun() throws InterruptedException {
684                threadsStarted.countDown();
581                  threadsStarted.await();
582 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
582 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
583              }});
584  
585          executor.execute(new CheckedRunnable() {
586              public void realRun() throws InterruptedException {
691                threadsStarted.countDown();
587                  threadsStarted.await();
588                  assertSame(one, q.take());
589                  checkEmpty(q);
# Line 702 | Line 597 | public class LinkedTransferQueueTest ext
597       */
598      public void testPollInExecutor() {
599          final LinkedTransferQueue q = new LinkedTransferQueue();
600 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
600 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
601          ExecutorService executor = Executors.newFixedThreadPool(2);
602  
603          executor.execute(new CheckedRunnable() {
604              public void realRun() throws InterruptedException {
605                  assertNull(q.poll());
711                threadsStarted.countDown();
606                  threadsStarted.await();
607 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
607 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
608                  checkEmpty(q);
609              }});
610  
611          executor.execute(new CheckedRunnable() {
612              public void realRun() throws InterruptedException {
719                threadsStarted.countDown();
613                  threadsStarted.await();
614                  q.put(one);
615              }});
# Line 728 | Line 621 | public class LinkedTransferQueueTest ext
621       * A deserialized serialized queue has same elements in same order
622       */
623      public void testSerialization() throws Exception {
624 <        LinkedTransferQueue q = populatedQueue(SIZE);
625 <
733 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
734 <        ObjectOutputStream out
735 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
736 <        out.writeObject(q);
737 <        out.close();
624 >        Queue x = populatedQueue(SIZE);
625 >        Queue y = serialClone(x);
626  
627 <        ByteArrayInputStream bin
628 <            = new ByteArrayInputStream(bout.toByteArray());
629 <        ObjectInputStream in
630 <            = new ObjectInputStream(new BufferedInputStream(bin));
631 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
632 <
633 <        assertEquals(q.size(), r.size());
746 <        assertEquals(q.toString(), r.toString());
747 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
748 <        while (!q.isEmpty()) {
749 <            assertEquals(q.remove(), r.remove());
627 >        assertNotSame(y, x);
628 >        assertEquals(x.size(), y.size());
629 >        assertEquals(x.toString(), y.toString());
630 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
631 >        while (!x.isEmpty()) {
632 >            assertFalse(y.isEmpty());
633 >            assertEquals(x.remove(), y.remove());
634          }
635 <    }
752 <
753 <    /**
754 <     * drainTo(null) throws NullPointerException
755 <     */
756 <    public void testDrainToNull() {
757 <        LinkedTransferQueue q = populatedQueue(SIZE);
758 <        try {
759 <            q.drainTo(null);
760 <            shouldThrow();
761 <        } catch (NullPointerException success) {}
762 <    }
763 <
764 <    /**
765 <     * drainTo(this) throws IllegalArgumentException
766 <     */
767 <    public void testDrainToSelf() {
768 <        LinkedTransferQueue q = populatedQueue(SIZE);
769 <        try {
770 <            q.drainTo(q);
771 <            shouldThrow();
772 <        } catch (IllegalArgumentException success) {}
635 >        assertTrue(y.isEmpty());
636      }
637  
638      /**
# Line 779 | Line 642 | public class LinkedTransferQueueTest ext
642          LinkedTransferQueue q = populatedQueue(SIZE);
643          ArrayList l = new ArrayList();
644          q.drainTo(l);
645 <        assertEquals(q.size(), 0);
646 <        assertEquals(l.size(), SIZE);
645 >        assertEquals(0, q.size());
646 >        assertEquals(SIZE, l.size());
647          for (int i = 0; i < SIZE; ++i) {
648 <            assertEquals(l.get(i), i);
648 >            assertEquals(i, l.get(i));
649          }
650          q.add(zero);
651          q.add(one);
# Line 791 | Line 654 | public class LinkedTransferQueueTest ext
654          assertTrue(q.contains(one));
655          l.clear();
656          q.drainTo(l);
657 <        assertEquals(q.size(), 0);
658 <        assertEquals(l.size(), 2);
657 >        assertEquals(0, q.size());
658 >        assertEquals(2, l.size());
659          for (int i = 0; i < 2; ++i) {
660 <            assertEquals(l.get(i), i);
660 >            assertEquals(i, l.get(i));
661          }
662      }
663  
# Line 810 | Line 673 | public class LinkedTransferQueueTest ext
673          ArrayList l = new ArrayList();
674          q.drainTo(l);
675          assertTrue(l.size() >= SIZE);
676 <        for (int i = 0; i < SIZE; ++i) {
677 <            assertEquals(l.get(i), i);
815 <        }
676 >        for (int i = 0; i < SIZE; ++i)
677 >            assertEquals(i, l.get(i));
678          awaitTermination(t, MEDIUM_DELAY_MS);
679          assertTrue(q.size() + l.size() >= SIZE);
680      }
681  
682      /**
821     * drainTo(null, n) throws NullPointerException
822     */
823    public void testDrainToNullN() {
824        LinkedTransferQueue q = populatedQueue(SIZE);
825        try {
826            q.drainTo(null, SIZE);
827            shouldThrow();
828        } catch (NullPointerException success) {}
829    }
830
831    /**
832     * drainTo(this, n) throws IllegalArgumentException
833     */
834    public void testDrainToSelfN() {
835        LinkedTransferQueue q = populatedQueue(SIZE);
836        try {
837            q.drainTo(q, SIZE);
838            shouldThrow();
839        } catch (IllegalArgumentException success) {}
840    }
841
842    /**
683       * drainTo(c, n) empties first min(n, size) elements of queue into c
684       */
685      public void testDrainToN() {
# Line 851 | Line 691 | public class LinkedTransferQueueTest ext
691              ArrayList l = new ArrayList();
692              q.drainTo(l, i);
693              int k = (i < SIZE) ? i : SIZE;
694 <            assertEquals(l.size(), k);
695 <            assertEquals(q.size(), SIZE - k);
696 <            for (int j = 0; j < k; ++j) {
697 <                assertEquals(l.get(j), j);
858 <            }
694 >            assertEquals(k, l.size());
695 >            assertEquals(SIZE - k, q.size());
696 >            for (int j = 0; j < k; ++j)
697 >                assertEquals(j, l.get(j));
698              while (q.poll() != null)
699                  ;
700          }
# Line 867 | Line 706 | public class LinkedTransferQueueTest ext
706       */
707      public void testWaitingConsumer() throws InterruptedException {
708          final LinkedTransferQueue q = new LinkedTransferQueue();
709 <        assertEquals(q.getWaitingConsumerCount(), 0);
709 >        assertEquals(0, q.getWaitingConsumerCount());
710          assertFalse(q.hasWaitingConsumer());
711          final CountDownLatch threadStarted = new CountDownLatch(1);
712  
# Line 875 | Line 714 | public class LinkedTransferQueueTest ext
714              public void realRun() throws InterruptedException {
715                  threadStarted.countDown();
716                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
717 <                assertEquals(q.getWaitingConsumerCount(), 0);
717 >                assertEquals(0, q.getWaitingConsumerCount());
718                  assertFalse(q.hasWaitingConsumer());
719              }});
720  
721          threadStarted.await();
722          waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
723 <        assertEquals(q.getWaitingConsumerCount(), 1);
723 >        assertEquals(1, q.getWaitingConsumerCount());
724          assertTrue(q.hasWaitingConsumer());
725  
726          assertTrue(q.offer(one));
727 <        assertEquals(q.getWaitingConsumerCount(), 0);
727 >        assertEquals(0, q.getWaitingConsumerCount());
728          assertFalse(q.hasWaitingConsumer());
729  
730          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 1072 | Line 911 | public class LinkedTransferQueueTest ext
911      }
912  
913      /**
914 <     * tryTransfer waits the amount given, and throws
1076 <     * InterruptedException when interrupted.
914 >     * tryTransfer blocks interruptibly if no takers
915       */
916      public void testTryTransfer5() throws InterruptedException {
917          final LinkedTransferQueue q = new LinkedTransferQueue();
918 <        final CountDownLatch threadStarted = new CountDownLatch(1);
918 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
919          assertTrue(q.isEmpty());
920  
921          Thread t = newStartedThread(new CheckedRunnable() {
922              public void realRun() throws InterruptedException {
923 <                long t0 = System.nanoTime();
1086 <                threadStarted.countDown();
923 >                Thread.currentThread().interrupt();
924                  try {
925                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
926                      shouldThrow();
927                  } catch (InterruptedException success) {}
928 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
929 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
928 >                assertFalse(Thread.interrupted());
929 >
930 >                pleaseInterrupt.countDown();
931 >                try {
932 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
933 >                    shouldThrow();
934 >                } catch (InterruptedException success) {}
935 >                assertFalse(Thread.interrupted());
936              }});
937  
938 <        threadStarted.await();
939 <        while (q.isEmpty())
1097 <            Thread.yield();
1098 <        Thread.sleep(SHORT_DELAY_MS);
938 >        await(pleaseInterrupt);
939 >        assertThreadStaysAlive(t);
940          t.interrupt();
941 <        awaitTermination(t, MEDIUM_DELAY_MS);
941 >        awaitTermination(t);
942          checkEmpty(q);
943      }
944  
# Line 1111 | Line 952 | public class LinkedTransferQueueTest ext
952              public void realRun() throws InterruptedException {
953                  long t0 = System.nanoTime();
954                  assertFalse(q.tryTransfer(new Object(),
955 <                                          SHORT_DELAY_MS, MILLISECONDS));
956 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
955 >                                          timeoutMillis(), MILLISECONDS));
956 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
957                  checkEmpty(q);
958              }});
959  
960 <        awaitTermination(t, MEDIUM_DELAY_MS);
960 >        awaitTermination(t);
961          checkEmpty(q);
962      }
963  
# Line 1152 | Line 993 | public class LinkedTransferQueueTest ext
993          assertTrue(q.offer(four));
994          assertEquals(1, q.size());
995          long t0 = System.nanoTime();
996 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
997 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
996 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
997 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
998          assertEquals(1, q.size());
999          assertSame(four, q.poll());
1000          assertNull(q.poll());
# Line 1171 | Line 1012 | public class LinkedTransferQueueTest ext
1012          assertFalse(q.isEmpty());
1013          return q;
1014      }
1015 +
1016 +    /**
1017 +     * remove(null), contains(null) always return false
1018 +     */
1019 +    public void testNeverContainsNull() {
1020 +        Collection<?>[] qs = {
1021 +            new LinkedTransferQueue<Object>(),
1022 +            populatedQueue(2),
1023 +        };
1024 +
1025 +        for (Collection<?> q : qs) {
1026 +            assertFalse(q.contains(null));
1027 +            assertFalse(q.remove(null));
1028 +        }
1029 +    }
1030   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines