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.43 by jsr166, Sun Nov 28 08:43:53 2010 UTC vs.
Revision 1.57 by jsr166, Sat Jan 17 22:55:06 2015 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;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10   import java.util.ArrayList;
11   import java.util.Arrays;
12 + import java.util.Collection;
13   import java.util.Iterator;
14   import java.util.List;
15   import java.util.NoSuchElementException;
16 < import java.util.concurrent.*;
17 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 < import static java.util.concurrent.TimeUnit.NANOSECONDS;
16 > import java.util.Queue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.LinkedTransferQueue;
22 >
23   import junit.framework.Test;
23 import junit.framework.TestSuite;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
# Line 65 | Line 65 | public class LinkedTransferQueueTest ext
65       * NullPointerException
66       */
67      public void testConstructor3() {
68 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
69          try {
70 <            Integer[] ints = new Integer[SIZE];
70 <            new LinkedTransferQueue(Arrays.asList(ints));
70 >            new LinkedTransferQueue(elements);
71              shouldThrow();
72          } catch (NullPointerException success) {}
73      }
# Line 77 | Line 77 | public class LinkedTransferQueueTest ext
77       * throws NullPointerException
78       */
79      public void testConstructor4() {
80 +        Integer[] ints = new Integer[SIZE];
81 +        for (int i = 0; i < SIZE-1; ++i)
82 +            ints[i] = i;
83 +        Collection<Integer> elements = Arrays.asList(ints);
84          try {
85 <            Integer[] ints = new Integer[SIZE];
82 <            for (int i = 0; i < SIZE - 1; ++i) {
83 <                ints[i] = i;
84 <            }
85 <            new LinkedTransferQueue(Arrays.asList(ints));
85 >            new LinkedTransferQueue(elements);
86              shouldThrow();
87          } catch (NullPointerException success) {}
88      }
# Line 129 | Line 129 | public class LinkedTransferQueueTest ext
129      }
130  
131      /**
132     * offer(null) throws NullPointerException
133     */
134    public void testOfferNull() {
135        try {
136            LinkedTransferQueue q = new LinkedTransferQueue();
137            q.offer(null);
138            shouldThrow();
139        } catch (NullPointerException success) {}
140    }
141
142    /**
143     * add(null) throws NullPointerException
144     */
145    public void testAddNull() {
146        try {
147            LinkedTransferQueue q = new LinkedTransferQueue();
148            q.add(null);
149            shouldThrow();
150        } catch (NullPointerException success) {}
151    }
152
153    /**
154     * addAll(null) throws NullPointerException
155     */
156    public void testAddAll1() {
157        try {
158            LinkedTransferQueue q = new LinkedTransferQueue();
159            q.addAll(null);
160            shouldThrow();
161        } catch (NullPointerException success) {}
162    }
163
164    /**
132       * addAll(this) throws IllegalArgumentException
133       */
134      public void testAddAllSelf() {
# Line 173 | Line 140 | public class LinkedTransferQueueTest ext
140      }
141  
142      /**
176     * addAll of a collection with null elements throws NullPointerException
177     */
178    public void testAddAll2() {
179        try {
180            LinkedTransferQueue q = new LinkedTransferQueue();
181            Integer[] ints = new Integer[SIZE];
182            q.addAll(Arrays.asList(ints));
183            shouldThrow();
184        } catch (NullPointerException success) {}
185    }
186
187    /**
143       * addAll of a collection with any null elements throws
144       * NullPointerException after possibly adding some elements
145       */
# Line 218 | Line 173 | public class LinkedTransferQueueTest ext
173      }
174  
175      /**
221     * put(null) throws NullPointerException
222     */
223    public void testPutNull() throws InterruptedException {
224        try {
225            LinkedTransferQueue q = new LinkedTransferQueue();
226            q.put(null);
227            shouldThrow();
228        } catch (NullPointerException success) {}
229    }
230
231    /**
176       * all elements successfully put are contained
177       */
178      public void testPut() {
179          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
180          for (int i = 0; i < SIZE; ++i) {
181 <            assertEquals(q.size(), i);
181 >            assertEquals(i, q.size());
182              q.put(i);
183              assertTrue(q.contains(i));
184          }
# Line 254 | Line 198 | public class LinkedTransferQueueTest ext
198       * take removes existing elements until empty, then blocks interruptibly
199       */
200      public void testBlockingTake() throws InterruptedException {
201 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
202 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
201 >        final BlockingQueue q = populatedQueue(SIZE);
202 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
203          Thread t = newStartedThread(new CheckedRunnable() {
204              public void realRun() throws InterruptedException {
205                  for (int i = 0; i < SIZE; ++i) {
206 <                    assertEquals(i, (int) q.take());
206 >                    assertEquals(i, q.take());
207                  }
208 <                aboutToWait.countDown();
208 >
209 >                Thread.currentThread().interrupt();
210                  try {
211                      q.take();
212                      shouldThrow();
213                  } catch (InterruptedException success) {}
214 +                assertFalse(Thread.interrupted());
215 +
216 +                pleaseInterrupt.countDown();
217 +                try {
218 +                    q.take();
219 +                    shouldThrow();
220 +                } catch (InterruptedException success) {}
221 +                assertFalse(Thread.interrupted());
222              }});
223  
224 <        aboutToWait.await();
225 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
224 >        await(pleaseInterrupt);
225 >        assertThreadStaysAlive(t);
226          t.interrupt();
227 <        awaitTermination(t, MEDIUM_DELAY_MS);
275 <        checkEmpty(q);
227 >        awaitTermination(t);
228      }
229  
230      /**
# Line 305 | Line 257 | public class LinkedTransferQueueTest ext
257      public void testTimedPoll() throws InterruptedException {
258          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
259          for (int i = 0; i < SIZE; ++i) {
260 <            long t0 = System.nanoTime();
261 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
262 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
263 <        }
264 <        long t0 = System.nanoTime();
265 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
266 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
260 >            long startTime = System.nanoTime();
261 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
262 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
263 >        }
264 >        long startTime = System.nanoTime();
265 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
266 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
267          checkEmpty(q);
268      }
269  
# Line 417 | Line 369 | public class LinkedTransferQueueTest ext
369      }
370  
371      /**
420     * remove(x) removes x and returns true if present
421     */
422    public void testRemoveElement() throws InterruptedException {
423        LinkedTransferQueue q = populatedQueue(SIZE);
424        for (int i = 1; i < SIZE; i+=2) {
425            assertTrue(q.contains(i));
426            assertTrue(q.remove(i));
427            assertFalse(q.contains(i));
428            assertTrue(q.contains(i-1));
429        }
430        for (int i = 0; i < SIZE; i+=2) {
431            assertTrue(q.contains(i));
432            assertTrue(q.remove(i));
433            assertFalse(q.contains(i));
434            assertFalse(q.remove(i+1));
435            assertFalse(q.contains(i+1));
436        }
437        checkEmpty(q);
438    }
439
440    /**
372       * An add following remove(x) succeeds
373       */
374      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 553 | Line 484 | public class LinkedTransferQueueTest ext
484      }
485  
486      /**
556     * toArray(null) throws NullPointerException
557     */
558    public void testToArray_NullArg() {
559        LinkedTransferQueue q = populatedQueue(SIZE);
560        try {
561            q.toArray(null);
562            shouldThrow();
563        } catch (NullPointerException success) {}
564    }
565
566    /**
487       * toArray(incompatible array type) throws ArrayStoreException
488       */
489      public void testToArray1_BadArg() {
# Line 580 | Line 500 | public class LinkedTransferQueueTest ext
500      public void testIterator() throws InterruptedException {
501          LinkedTransferQueue q = populatedQueue(SIZE);
502          Iterator it = q.iterator();
503 <        int i = 0;
504 <        while (it.hasNext()) {
505 <            assertEquals(it.next(), i++);
506 <        }
503 >        int i;
504 >        for (i = 0; it.hasNext(); i++)
505 >            assertTrue(q.contains(it.next()));
506 >        assertEquals(i, SIZE);
507 >        assertIteratorExhausted(it);
508 >
509 >        it = q.iterator();
510 >        for (i = 0; it.hasNext(); i++)
511 >            assertEquals(it.next(), q.take());
512          assertEquals(i, SIZE);
513 +        assertIteratorExhausted(it);
514 +    }
515 +
516 +    /**
517 +     * iterator of empty collection has no elements
518 +     */
519 +    public void testEmptyIterator() {
520 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
521      }
522  
523      /**
# Line 646 | Line 579 | public class LinkedTransferQueueTest ext
579          LinkedTransferQueue q = populatedQueue(SIZE);
580          String s = q.toString();
581          for (int i = 0; i < SIZE; ++i) {
582 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
582 >            assertTrue(s.contains(String.valueOf(i)));
583          }
584      }
585  
# Line 655 | Line 588 | public class LinkedTransferQueueTest ext
588       */
589      public void testOfferInExecutor() {
590          final LinkedTransferQueue q = new LinkedTransferQueue();
591 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
591 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
592          ExecutorService executor = Executors.newFixedThreadPool(2);
593  
594          executor.execute(new CheckedRunnable() {
595              public void realRun() throws InterruptedException {
663                threadsStarted.countDown();
596                  threadsStarted.await();
597 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
597 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
598              }});
599  
600          executor.execute(new CheckedRunnable() {
601              public void realRun() throws InterruptedException {
670                threadsStarted.countDown();
602                  threadsStarted.await();
603                  assertSame(one, q.take());
604                  checkEmpty(q);
# Line 681 | Line 612 | public class LinkedTransferQueueTest ext
612       */
613      public void testPollInExecutor() {
614          final LinkedTransferQueue q = new LinkedTransferQueue();
615 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
615 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
616          ExecutorService executor = Executors.newFixedThreadPool(2);
617  
618          executor.execute(new CheckedRunnable() {
619              public void realRun() throws InterruptedException {
620                  assertNull(q.poll());
690                threadsStarted.countDown();
621                  threadsStarted.await();
622 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
622 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
623                  checkEmpty(q);
624              }});
625  
626          executor.execute(new CheckedRunnable() {
627              public void realRun() throws InterruptedException {
698                threadsStarted.countDown();
628                  threadsStarted.await();
629                  q.put(one);
630              }});
# Line 707 | Line 636 | public class LinkedTransferQueueTest ext
636       * A deserialized serialized queue has same elements in same order
637       */
638      public void testSerialization() throws Exception {
639 <        LinkedTransferQueue q = populatedQueue(SIZE);
640 <
712 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
713 <        ObjectOutputStream out
714 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
715 <        out.writeObject(q);
716 <        out.close();
717 <
718 <        ByteArrayInputStream bin
719 <            = new ByteArrayInputStream(bout.toByteArray());
720 <        ObjectInputStream in
721 <            = new ObjectInputStream(new BufferedInputStream(bin));
722 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
639 >        Queue x = populatedQueue(SIZE);
640 >        Queue y = serialClone(x);
641  
642 <        assertEquals(q.size(), r.size());
643 <        assertEquals(q.toString(), r.toString());
644 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
645 <        while (!q.isEmpty()) {
646 <            assertEquals(q.remove(), r.remove());
642 >        assertNotSame(y, x);
643 >        assertEquals(x.size(), y.size());
644 >        assertEquals(x.toString(), y.toString());
645 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
646 >        while (!x.isEmpty()) {
647 >            assertFalse(y.isEmpty());
648 >            assertEquals(x.remove(), y.remove());
649          }
650 <    }
731 <
732 <    /**
733 <     * drainTo(null) throws NullPointerException
734 <     */
735 <    public void testDrainToNull() {
736 <        LinkedTransferQueue q = populatedQueue(SIZE);
737 <        try {
738 <            q.drainTo(null);
739 <            shouldThrow();
740 <        } catch (NullPointerException success) {}
741 <    }
742 <
743 <    /**
744 <     * drainTo(this) throws IllegalArgumentException
745 <     */
746 <    public void testDrainToSelf() {
747 <        LinkedTransferQueue q = populatedQueue(SIZE);
748 <        try {
749 <            q.drainTo(q);
750 <            shouldThrow();
751 <        } catch (IllegalArgumentException success) {}
650 >        assertTrue(y.isEmpty());
651      }
652  
653      /**
# Line 758 | Line 657 | public class LinkedTransferQueueTest ext
657          LinkedTransferQueue q = populatedQueue(SIZE);
658          ArrayList l = new ArrayList();
659          q.drainTo(l);
660 <        assertEquals(q.size(), 0);
661 <        assertEquals(l.size(), SIZE);
660 >        assertEquals(0, q.size());
661 >        assertEquals(SIZE, l.size());
662          for (int i = 0; i < SIZE; ++i) {
663 <            assertEquals(l.get(i), i);
663 >            assertEquals(i, l.get(i));
664          }
665          q.add(zero);
666          q.add(one);
# Line 770 | Line 669 | public class LinkedTransferQueueTest ext
669          assertTrue(q.contains(one));
670          l.clear();
671          q.drainTo(l);
672 <        assertEquals(q.size(), 0);
673 <        assertEquals(l.size(), 2);
672 >        assertEquals(0, q.size());
673 >        assertEquals(2, l.size());
674          for (int i = 0; i < 2; ++i) {
675 <            assertEquals(l.get(i), i);
675 >            assertEquals(i, l.get(i));
676          }
677      }
678  
# Line 789 | Line 688 | public class LinkedTransferQueueTest ext
688          ArrayList l = new ArrayList();
689          q.drainTo(l);
690          assertTrue(l.size() >= SIZE);
691 <        for (int i = 0; i < SIZE; ++i) {
692 <            assertEquals(l.get(i), i);
794 <        }
691 >        for (int i = 0; i < SIZE; ++i)
692 >            assertEquals(i, l.get(i));
693          awaitTermination(t, MEDIUM_DELAY_MS);
694          assertTrue(q.size() + l.size() >= SIZE);
695      }
696  
697      /**
800     * drainTo(null, n) throws NullPointerException
801     */
802    public void testDrainToNullN() {
803        LinkedTransferQueue q = populatedQueue(SIZE);
804        try {
805            q.drainTo(null, SIZE);
806            shouldThrow();
807        } catch (NullPointerException success) {}
808    }
809
810    /**
811     * drainTo(this, n) throws IllegalArgumentException
812     */
813    public void testDrainToSelfN() {
814        LinkedTransferQueue q = populatedQueue(SIZE);
815        try {
816            q.drainTo(q, SIZE);
817            shouldThrow();
818        } catch (IllegalArgumentException success) {}
819    }
820
821    /**
698       * drainTo(c, n) empties first min(n, size) elements of queue into c
699       */
700      public void testDrainToN() {
# Line 830 | Line 706 | public class LinkedTransferQueueTest ext
706              ArrayList l = new ArrayList();
707              q.drainTo(l, i);
708              int k = (i < SIZE) ? i : SIZE;
709 <            assertEquals(l.size(), k);
710 <            assertEquals(q.size(), SIZE - k);
711 <            for (int j = 0; j < k; ++j) {
712 <                assertEquals(l.get(j), j);
713 <            }
838 <            while (q.poll() != null)
839 <                ;
709 >            assertEquals(k, l.size());
710 >            assertEquals(SIZE - k, q.size());
711 >            for (int j = 0; j < k; ++j)
712 >                assertEquals(j, l.get(j));
713 >            do {} while (q.poll() != null);
714          }
715      }
716  
# Line 846 | Line 720 | public class LinkedTransferQueueTest ext
720       */
721      public void testWaitingConsumer() throws InterruptedException {
722          final LinkedTransferQueue q = new LinkedTransferQueue();
723 <        assertEquals(q.getWaitingConsumerCount(), 0);
723 >        assertEquals(0, q.getWaitingConsumerCount());
724          assertFalse(q.hasWaitingConsumer());
725          final CountDownLatch threadStarted = new CountDownLatch(1);
726  
# Line 854 | Line 728 | public class LinkedTransferQueueTest ext
728              public void realRun() throws InterruptedException {
729                  threadStarted.countDown();
730                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
731 <                assertEquals(q.getWaitingConsumerCount(), 0);
731 >                assertEquals(0, q.getWaitingConsumerCount());
732                  assertFalse(q.hasWaitingConsumer());
733              }});
734  
735          threadStarted.await();
736          waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
737 <        assertEquals(q.getWaitingConsumerCount(), 1);
737 >        assertEquals(1, q.getWaitingConsumerCount());
738          assertTrue(q.hasWaitingConsumer());
739  
740          assertTrue(q.offer(one));
741 <        assertEquals(q.getWaitingConsumerCount(), 0);
741 >        assertEquals(0, q.getWaitingConsumerCount());
742          assertFalse(q.hasWaitingConsumer());
743  
744          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 1051 | Line 925 | public class LinkedTransferQueueTest ext
925      }
926  
927      /**
928 <     * tryTransfer waits the amount given, and throws
1055 <     * InterruptedException when interrupted.
928 >     * tryTransfer blocks interruptibly if no takers
929       */
930      public void testTryTransfer5() throws InterruptedException {
931          final LinkedTransferQueue q = new LinkedTransferQueue();
932 <        final CountDownLatch threadStarted = new CountDownLatch(1);
932 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
933          assertTrue(q.isEmpty());
934  
935          Thread t = newStartedThread(new CheckedRunnable() {
936              public void realRun() throws InterruptedException {
937 <                long t0 = System.nanoTime();
938 <                threadStarted.countDown();
937 >                Thread.currentThread().interrupt();
938 >                try {
939 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
940 >                    shouldThrow();
941 >                } catch (InterruptedException success) {}
942 >                assertFalse(Thread.interrupted());
943 >
944 >                pleaseInterrupt.countDown();
945                  try {
946                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
947                      shouldThrow();
948                  } catch (InterruptedException success) {}
949 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1071 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
949 >                assertFalse(Thread.interrupted());
950              }});
951  
952 <        threadStarted.await();
953 <        while (q.isEmpty())
1076 <            Thread.yield();
1077 <        Thread.sleep(SHORT_DELAY_MS);
952 >        await(pleaseInterrupt);
953 >        assertThreadStaysAlive(t);
954          t.interrupt();
955 <        awaitTermination(t, MEDIUM_DELAY_MS);
955 >        awaitTermination(t);
956          checkEmpty(q);
957      }
958  
# Line 1090 | Line 966 | public class LinkedTransferQueueTest ext
966              public void realRun() throws InterruptedException {
967                  long t0 = System.nanoTime();
968                  assertFalse(q.tryTransfer(new Object(),
969 <                                          SHORT_DELAY_MS, MILLISECONDS));
970 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
969 >                                          timeoutMillis(), MILLISECONDS));
970 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
971                  checkEmpty(q);
972              }});
973  
974 <        awaitTermination(t, MEDIUM_DELAY_MS);
974 >        awaitTermination(t);
975          checkEmpty(q);
976      }
977  
# Line 1131 | Line 1007 | public class LinkedTransferQueueTest ext
1007          assertTrue(q.offer(four));
1008          assertEquals(1, q.size());
1009          long t0 = System.nanoTime();
1010 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1011 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1010 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1011 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1012          assertEquals(1, q.size());
1013          assertSame(four, q.poll());
1014          assertNull(q.poll());
# Line 1150 | Line 1026 | public class LinkedTransferQueueTest ext
1026          assertFalse(q.isEmpty());
1027          return q;
1028      }
1029 +
1030 +    /**
1031 +     * remove(null), contains(null) always return false
1032 +     */
1033 +    public void testNeverContainsNull() {
1034 +        Collection<?>[] qs = {
1035 +            new LinkedTransferQueue<Object>(),
1036 +            populatedQueue(2),
1037 +        };
1038 +
1039 +        for (Collection<?> q : qs) {
1040 +            assertFalse(q.contains(null));
1041 +            assertFalse(q.remove(null));
1042 +        }
1043 +    }
1044   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines