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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.44 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.60 by jsr166, Sat May 23 00:53:08 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
10 >
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
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.LinkedBlockingQueue;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingQueueTest extends JSR166TestCase {
26  
# Line 22 | Line 32 | public class LinkedBlockingQueueTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingQueue(20);
35 >            return new LinkedBlockingQueue(SIZE);
36          }
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 37 | Line 47 | public class LinkedBlockingQueueTest ext
47      }
48  
49      /**
50 <     * Create a queue of given size containing consecutive
50 >     * Returns a new queue of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingQueue<Integer> populatedQueue(int n) {
# Line 62 | Line 72 | public class LinkedBlockingQueueTest ext
72      }
73  
74      /**
75 <     * Constructor throws IAE if capacity argument nonpositive
75 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
76       */
77      public void testConstructor2() {
78          try {
79 <            LinkedBlockingQueue q = new LinkedBlockingQueue(0);
79 >            new LinkedBlockingQueue(0);
80              shouldThrow();
81          } catch (IllegalArgumentException success) {}
82      }
83  
84      /**
85 <     * Initializing from null Collection throws NPE
85 >     * Initializing from null Collection throws NullPointerException
86       */
87      public void testConstructor3() {
88          try {
89 <            LinkedBlockingQueue q = new LinkedBlockingQueue(null);
89 >            new LinkedBlockingQueue(null);
90              shouldThrow();
91          } catch (NullPointerException success) {}
92      }
93  
94      /**
95 <     * Initializing from Collection of null elements throws NPE
95 >     * Initializing from Collection of null elements throws NullPointerException
96       */
97      public void testConstructor4() {
98 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
99          try {
100 <            Integer[] ints = new Integer[SIZE];
90 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
100 >            new LinkedBlockingQueue(elements);
101              shouldThrow();
102          } catch (NullPointerException success) {}
103      }
104  
105      /**
106 <     * Initializing from Collection with some null elements throws NPE
106 >     * Initializing from Collection with some null elements throws
107 >     * NullPointerException
108       */
109      public void testConstructor5() {
110 +        Integer[] ints = new Integer[SIZE];
111 +        for (int i = 0; i < SIZE - 1; ++i)
112 +            ints[i] = new Integer(i);
113 +        Collection<Integer> elements = Arrays.asList(ints);
114          try {
115 <            Integer[] ints = new Integer[SIZE];
101 <            for (int i = 0; i < SIZE-1; ++i)
102 <                ints[i] = new Integer(i);
103 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
115 >            new LinkedBlockingQueue(elements);
116              shouldThrow();
117          } catch (NullPointerException success) {}
118      }
# Line 136 | Line 148 | public class LinkedBlockingQueueTest ext
148       * remainingCapacity decreases on add, increases on remove
149       */
150      public void testRemainingCapacity() {
151 <        LinkedBlockingQueue q = populatedQueue(SIZE);
151 >        BlockingQueue q = populatedQueue(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153              assertEquals(i, q.remainingCapacity());
154 <            assertEquals(SIZE-i, q.size());
155 <            q.remove();
154 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
155 >            assertEquals(i, q.remove());
156          }
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(SIZE-i, q.remainingCapacity());
159 <            assertEquals(i, q.size());
160 <            q.add(new Integer(i));
158 >            assertEquals(SIZE - i, q.remainingCapacity());
159 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
160 >            assertTrue(q.add(i));
161          }
162      }
163  
164      /**
153     * offer(null) throws NPE
154     */
155    public void testOfferNull() {
156        try {
157            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
158            q.offer(null);
159            shouldThrow();
160        } catch (NullPointerException success) {}
161    }
162
163    /**
164     * add(null) throws NPE
165     */
166    public void testAddNull() {
167        try {
168            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
169            q.add(null);
170            shouldThrow();
171        } catch (NullPointerException success) {}
172    }
173
174    /**
165       * Offer succeeds if not full; fails if full
166       */
167      public void testOffer() {
# Line 181 | Line 171 | public class LinkedBlockingQueueTest ext
171      }
172  
173      /**
174 <     * add succeeds if not full; throws ISE if full
174 >     * add succeeds if not full; throws IllegalStateException if full
175       */
176      public void testAdd() {
177 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
178 +        for (int i = 0; i < SIZE; ++i)
179 +            assertTrue(q.add(new Integer(i)));
180 +        assertEquals(0, q.remainingCapacity());
181          try {
188            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
189            for (int i = 0; i < SIZE; ++i) {
190                assertTrue(q.add(new Integer(i)));
191            }
192            assertEquals(0, q.remainingCapacity());
182              q.add(new Integer(SIZE));
183              shouldThrow();
184          } catch (IllegalStateException success) {}
185      }
186  
187      /**
188 <     * addAll(null) throws NPE
200 <     */
201 <    public void testAddAll1() {
202 <        try {
203 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
204 <            q.addAll(null);
205 <            shouldThrow();
206 <        } catch (NullPointerException success) {}
207 <    }
208 <
209 <    /**
210 <     * addAll(this) throws IAE
188 >     * addAll(this) throws IllegalArgumentException
189       */
190      public void testAddAllSelf() {
191 +        LinkedBlockingQueue q = populatedQueue(SIZE);
192          try {
214            LinkedBlockingQueue q = populatedQueue(SIZE);
193              q.addAll(q);
194              shouldThrow();
195          } catch (IllegalArgumentException success) {}
196      }
197  
198      /**
221     * addAll of a collection with null elements throws NPE
222     */
223    public void testAddAll2() {
224        try {
225            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
226            Integer[] ints = new Integer[SIZE];
227            q.addAll(Arrays.asList(ints));
228            shouldThrow();
229        } catch (NullPointerException success) {}
230    }
231
232    /**
199       * addAll of a collection with any null elements throws NPE after
200       * possibly adding some elements
201       */
202      public void testAddAll3() {
203 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
204 +        Integer[] ints = new Integer[SIZE];
205 +        for (int i = 0; i < SIZE - 1; ++i)
206 +            ints[i] = new Integer(i);
207 +        Collection<Integer> elements = Arrays.asList(ints);
208          try {
209 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
239 <            Integer[] ints = new Integer[SIZE];
240 <            for (int i = 0; i < SIZE-1; ++i)
241 <                ints[i] = new Integer(i);
242 <            q.addAll(Arrays.asList(ints));
209 >            q.addAll(elements);
210              shouldThrow();
211          } catch (NullPointerException success) {}
212      }
213  
214      /**
215 <     * addAll throws ISE if not enough room
215 >     * addAll throws IllegalStateException if not enough room
216       */
217      public void testAddAll4() {
218 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
219 +        Integer[] ints = new Integer[SIZE];
220 +        for (int i = 0; i < SIZE; ++i)
221 +            ints[i] = new Integer(i);
222 +        Collection<Integer> elements = Arrays.asList(ints);
223          try {
224 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
253 <            Integer[] ints = new Integer[SIZE];
254 <            for (int i = 0; i < SIZE; ++i)
255 <                ints[i] = new Integer(i);
256 <            q.addAll(Arrays.asList(ints));
224 >            q.addAll(elements);
225              shouldThrow();
226          } catch (IllegalStateException success) {}
227      }
# Line 274 | Line 242 | public class LinkedBlockingQueueTest ext
242      }
243  
244      /**
277     * put(null) throws NPE
278     */
279    public void testPutNull() throws InterruptedException {
280        try {
281            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
282            q.put(null);
283            shouldThrow();
284        } catch (NullPointerException success) {}
285    }
286
287    /**
245       * all elements successfully put are contained
246       */
247      public void testPut() throws InterruptedException {
248          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
249          for (int i = 0; i < SIZE; ++i) {
250 <            Integer I = new Integer(i);
251 <            q.put(I);
252 <            assertTrue(q.contains(I));
250 >            Integer x = new Integer(i);
251 >            q.put(x);
252 >            assertTrue(q.contains(x));
253          }
254          assertEquals(0, q.remainingCapacity());
255      }
# Line 357 | Line 314 | public class LinkedBlockingQueueTest ext
314              }});
315  
316          await(pleaseTake);
317 <        assertEquals(q.remainingCapacity(), 0);
317 >        assertEquals(0, q.remainingCapacity());
318          assertEquals(0, q.take());
319  
320          await(pleaseInterrupt);
321          assertThreadStaysAlive(t);
322          t.interrupt();
323          awaitTermination(t);
324 <        assertEquals(q.remainingCapacity(), 0);
324 >        assertEquals(0, q.remainingCapacity());
325      }
326  
327      /**
# Line 549 | Line 506 | public class LinkedBlockingQueueTest ext
506      }
507  
508      /**
552     * remove(x) removes x and returns true if present
553     */
554    public void testRemoveElement() {
555        LinkedBlockingQueue q = populatedQueue(SIZE);
556        for (int i = 1; i < SIZE; i+=2) {
557            assertTrue(q.contains(i));
558            assertTrue(q.remove(i));
559            assertFalse(q.contains(i));
560            assertTrue(q.contains(i-1));
561        }
562        for (int i = 0; i < SIZE; i+=2) {
563            assertTrue(q.contains(i));
564            assertTrue(q.remove(i));
565            assertFalse(q.contains(i));
566            assertFalse(q.remove(i+1));
567            assertFalse(q.contains(i+1));
568        }
569        assertTrue(q.isEmpty());
570    }
571
572    /**
509       * An add following remove(x) succeeds
510       */
511      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 579 | Line 515 | public class LinkedBlockingQueueTest ext
515          assertTrue(q.remove(new Integer(1)));
516          assertTrue(q.remove(new Integer(2)));
517          assertTrue(q.add(new Integer(3)));
518 <        assertTrue(q.take() != null);
518 >        assertNotNull(q.take());
519      }
520  
521      /**
# Line 638 | Line 574 | public class LinkedBlockingQueueTest ext
574                  assertTrue(changed);
575  
576              assertTrue(q.containsAll(p));
577 <            assertEquals(SIZE-i, q.size());
577 >            assertEquals(SIZE - i, q.size());
578              p.remove();
579          }
580      }
# Line 651 | Line 587 | public class LinkedBlockingQueueTest ext
587              LinkedBlockingQueue q = populatedQueue(SIZE);
588              LinkedBlockingQueue p = populatedQueue(i);
589              assertTrue(q.removeAll(p));
590 <            assertEquals(SIZE-i, q.size());
590 >            assertEquals(SIZE - i, q.size());
591              for (int j = 0; j < i; ++j) {
592 <                Integer I = (Integer)(p.remove());
593 <                assertFalse(q.contains(I));
592 >                Integer x = (Integer)(p.remove());
593 >                assertFalse(q.contains(x));
594              }
595          }
596      }
# Line 682 | Line 618 | public class LinkedBlockingQueueTest ext
618      }
619  
620      /**
685     * toArray(null) throws NullPointerException
686     */
687    public void testToArray_NullArg() {
688        LinkedBlockingQueue q = populatedQueue(SIZE);
689        try {
690            q.toArray(null);
691            shouldThrow();
692        } catch (NullPointerException success) {}
693    }
694
695    /**
621       * toArray(incompatible array type) throws ArrayStoreException
622       */
623      public void testToArray1_BadArg() {
# Line 709 | Line 634 | public class LinkedBlockingQueueTest ext
634      public void testIterator() throws InterruptedException {
635          LinkedBlockingQueue q = populatedQueue(SIZE);
636          Iterator it = q.iterator();
637 <        while (it.hasNext()) {
637 >        int i;
638 >        for (i = 0; it.hasNext(); i++)
639 >            assertTrue(q.contains(it.next()));
640 >        assertEquals(i, SIZE);
641 >        assertIteratorExhausted(it);
642 >
643 >        it = q.iterator();
644 >        for (i = 0; it.hasNext(); i++)
645              assertEquals(it.next(), q.take());
646 <        }
646 >        assertEquals(i, SIZE);
647 >        assertIteratorExhausted(it);
648 >    }
649 >
650 >    /**
651 >     * iterator of empty collection has no elements
652 >     */
653 >    public void testEmptyIterator() {
654 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
655      }
656  
657      /**
# Line 829 | Line 769 | public class LinkedBlockingQueueTest ext
769       * A deserialized serialized queue has same elements in same order
770       */
771      public void testSerialization() throws Exception {
772 <        LinkedBlockingQueue q = populatedQueue(SIZE);
773 <
834 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
835 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
836 <        out.writeObject(q);
837 <        out.close();
772 >        Queue x = populatedQueue(SIZE);
773 >        Queue y = serialClone(x);
774  
775 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
776 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
777 <        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
778 <        assertEquals(q.size(), r.size());
779 <        while (!q.isEmpty())
780 <            assertEquals(q.remove(), r.remove());
781 <    }
782 <
783 <    /**
848 <     * drainTo(null) throws NPE
849 <     */
850 <    public void testDrainToNull() {
851 <        LinkedBlockingQueue q = populatedQueue(SIZE);
852 <        try {
853 <            q.drainTo(null);
854 <            shouldThrow();
855 <        } catch (NullPointerException success) {}
856 <    }
857 <
858 <    /**
859 <     * drainTo(this) throws IAE
860 <     */
861 <    public void testDrainToSelf() {
862 <        LinkedBlockingQueue q = populatedQueue(SIZE);
863 <        try {
864 <            q.drainTo(q);
865 <            shouldThrow();
866 <        } catch (IllegalArgumentException success) {}
775 >        assertNotSame(x, y);
776 >        assertEquals(x.size(), y.size());
777 >        assertEquals(x.toString(), y.toString());
778 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
779 >        while (!x.isEmpty()) {
780 >            assertFalse(y.isEmpty());
781 >            assertEquals(x.remove(), y.remove());
782 >        }
783 >        assertTrue(y.isEmpty());
784      }
785  
786      /**
# Line 873 | Line 790 | public class LinkedBlockingQueueTest ext
790          LinkedBlockingQueue q = populatedQueue(SIZE);
791          ArrayList l = new ArrayList();
792          q.drainTo(l);
793 <        assertEquals(q.size(), 0);
794 <        assertEquals(l.size(), SIZE);
793 >        assertEquals(0, q.size());
794 >        assertEquals(SIZE, l.size());
795          for (int i = 0; i < SIZE; ++i)
796              assertEquals(l.get(i), new Integer(i));
797          q.add(zero);
# Line 884 | Line 801 | public class LinkedBlockingQueueTest ext
801          assertTrue(q.contains(one));
802          l.clear();
803          q.drainTo(l);
804 <        assertEquals(q.size(), 0);
805 <        assertEquals(l.size(), 2);
804 >        assertEquals(0, q.size());
805 >        assertEquals(2, l.size());
806          for (int i = 0; i < 2; ++i)
807              assertEquals(l.get(i), new Integer(i));
808      }
# Line 897 | Line 814 | public class LinkedBlockingQueueTest ext
814          final LinkedBlockingQueue q = populatedQueue(SIZE);
815          Thread t = new Thread(new CheckedRunnable() {
816              public void realRun() throws InterruptedException {
817 <                q.put(new Integer(SIZE+1));
817 >                q.put(new Integer(SIZE + 1));
818              }});
819  
820          t.start();
# Line 911 | Line 828 | public class LinkedBlockingQueueTest ext
828      }
829  
830      /**
914     * drainTo(null, n) throws NPE
915     */
916    public void testDrainToNullN() {
917        LinkedBlockingQueue q = populatedQueue(SIZE);
918        try {
919            q.drainTo(null, 0);
920            shouldThrow();
921        } catch (NullPointerException success) {}
922    }
923
924    /**
925     * drainTo(this, n) throws IAE
926     */
927    public void testDrainToSelfN() {
928        LinkedBlockingQueue q = populatedQueue(SIZE);
929        try {
930            q.drainTo(q, 0);
931            shouldThrow();
932        } catch (IllegalArgumentException success) {}
933    }
934
935    /**
831       * drainTo(c, n) empties first min(n, size) elements of queue into c
832       */
833      public void testDrainToN() {
# Line 943 | Line 838 | public class LinkedBlockingQueueTest ext
838              ArrayList l = new ArrayList();
839              q.drainTo(l, i);
840              int k = (i < SIZE) ? i : SIZE;
841 <            assertEquals(l.size(), k);
842 <            assertEquals(q.size(), SIZE-k);
841 >            assertEquals(k, l.size());
842 >            assertEquals(SIZE - k, q.size());
843              for (int j = 0; j < k; ++j)
844                  assertEquals(l.get(j), new Integer(j));
845 <            while (q.poll() != null) ;
845 >            do {} while (q.poll() != null);
846 >        }
847 >    }
848 >
849 >    /**
850 >     * remove(null), contains(null) always return false
851 >     */
852 >    public void testNeverContainsNull() {
853 >        Collection<?>[] qs = {
854 >            new LinkedBlockingQueue<Object>(),
855 >            populatedQueue(2),
856 >        };
857 >
858 >        for (Collection<?> q : qs) {
859 >            assertFalse(q.contains(null));
860 >            assertFalse(q.remove(null));
861          }
862      }
863  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines