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.34 by jsr166, Wed Nov 3 07:54:52 2010 UTC vs.
Revision 1.46 by jsr166, Tue May 31 16:16:24 2011 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 Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.LinkedBlockingQueue;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 import java.io.*;
22  
23   public class LinkedBlockingQueueTest extends JSR166TestCase {
24  
# Line 36 | Line 44 | public class LinkedBlockingQueueTest ext
44                              new Bounded().testSuite());
45      }
46  
39
47      /**
48       * Create a queue of given size containing consecutive
49       * Integers 0 ... n.
50       */
51 <    private LinkedBlockingQueue populatedQueue(int n) {
52 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
51 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
52 >        LinkedBlockingQueue<Integer> q =
53 >            new LinkedBlockingQueue<Integer>(n);
54          assertTrue(q.isEmpty());
55          for (int i = 0; i < n; i++)
56              assertTrue(q.offer(new Integer(i)));
# Line 62 | Line 70 | public class LinkedBlockingQueueTest ext
70      }
71  
72      /**
73 <     * Constructor throws IAE if capacity argument nonpositive
73 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
74       */
75      public void testConstructor2() {
76          try {
77 <            LinkedBlockingQueue q = new LinkedBlockingQueue(0);
77 >            new LinkedBlockingQueue(0);
78              shouldThrow();
79          } catch (IllegalArgumentException success) {}
80      }
81  
82      /**
83 <     * Initializing from null Collection throws NPE
83 >     * Initializing from null Collection throws NullPointerException
84       */
85      public void testConstructor3() {
86          try {
87 <            LinkedBlockingQueue q = new LinkedBlockingQueue(null);
87 >            new LinkedBlockingQueue(null);
88              shouldThrow();
89          } catch (NullPointerException success) {}
90      }
91  
92      /**
93 <     * Initializing from Collection of null elements throws NPE
93 >     * Initializing from Collection of null elements throws NullPointerException
94       */
95      public void testConstructor4() {
96 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
97          try {
98 <            Integer[] ints = new Integer[SIZE];
90 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
98 >            new LinkedBlockingQueue(elements);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
102  
103      /**
104 <     * Initializing from Collection with some null elements throws NPE
104 >     * Initializing from Collection with some null elements throws
105 >     * NullPointerException
106       */
107      public void testConstructor5() {
108 +        Integer[] ints = new Integer[SIZE];
109 +        for (int i = 0; i < SIZE-1; ++i)
110 +            ints[i] = new Integer(i);
111 +        Collection<Integer> elements = Arrays.asList(ints);
112          try {
113 <            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));
113 >            new LinkedBlockingQueue(elements);
114              shouldThrow();
115          } catch (NullPointerException success) {}
116      }
# Line 150 | Line 160 | public class LinkedBlockingQueueTest ext
160      }
161  
162      /**
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    /**
163       * Offer succeeds if not full; fails if full
164       */
165      public void testOffer() {
# Line 181 | Line 169 | public class LinkedBlockingQueueTest ext
169      }
170  
171      /**
172 <     * add succeeds if not full; throws ISE if full
172 >     * add succeeds if not full; throws IllegalStateException if full
173       */
174      public void testAdd() {
175 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
176 +        for (int i = 0; i < SIZE; ++i)
177 +            assertTrue(q.add(new Integer(i)));
178 +        assertEquals(0, q.remainingCapacity());
179          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());
180              q.add(new Integer(SIZE));
181              shouldThrow();
182          } catch (IllegalStateException success) {}
183      }
184  
185      /**
186 <     * 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
186 >     * addAll(this) throws IllegalArgumentException
187       */
188      public void testAddAllSelf() {
189 +        LinkedBlockingQueue q = populatedQueue(SIZE);
190          try {
214            LinkedBlockingQueue q = populatedQueue(SIZE);
191              q.addAll(q);
192              shouldThrow();
193          } catch (IllegalArgumentException success) {}
194      }
195  
196      /**
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    /**
197       * addAll of a collection with any null elements throws NPE after
198       * possibly adding some elements
199       */
200      public void testAddAll3() {
201 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
202 +        Integer[] ints = new Integer[SIZE];
203 +        for (int i = 0; i < SIZE-1; ++i)
204 +            ints[i] = new Integer(i);
205 +        Collection<Integer> elements = Arrays.asList(ints);
206          try {
207 <            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));
207 >            q.addAll(elements);
208              shouldThrow();
209          } catch (NullPointerException success) {}
210      }
211  
212      /**
213 <     * addAll throws ISE if not enough room
213 >     * addAll throws IllegalStateException if not enough room
214       */
215      public void testAddAll4() {
216 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
217 +        Integer[] ints = new Integer[SIZE];
218 +        for (int i = 0; i < SIZE; ++i)
219 +            ints[i] = new Integer(i);
220 +        Collection<Integer> elements = Arrays.asList(ints);
221          try {
222 <            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));
222 >            q.addAll(elements);
223              shouldThrow();
224          } catch (IllegalStateException success) {}
225      }
# Line 274 | Line 240 | public class LinkedBlockingQueueTest ext
240      }
241  
242      /**
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    /**
243       * all elements successfully put are contained
244       */
245      public void testPut() throws InterruptedException {
# Line 302 | Line 257 | public class LinkedBlockingQueueTest ext
257       */
258      public void testBlockingPut() throws InterruptedException {
259          final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
260 <        Thread t = new Thread(new CheckedRunnable() {
260 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
261 >        Thread t = newStartedThread(new CheckedRunnable() {
262              public void realRun() throws InterruptedException {
263                  for (int i = 0; i < SIZE; ++i)
264                      q.put(i);
265                  assertEquals(SIZE, q.size());
266                  assertEquals(0, q.remainingCapacity());
267 +
268 +                Thread.currentThread().interrupt();
269 +                try {
270 +                    q.put(99);
271 +                    shouldThrow();
272 +                } catch (InterruptedException success) {}
273 +                assertFalse(Thread.interrupted());
274 +
275 +                pleaseInterrupt.countDown();
276                  try {
277                      q.put(99);
278                      shouldThrow();
279                  } catch (InterruptedException success) {}
280 +                assertFalse(Thread.interrupted());
281              }});
282  
283 <        t.start();
284 <        Thread.sleep(SHORT_DELAY_MS);
283 >        await(pleaseInterrupt);
284 >        assertThreadStaysAlive(t);
285          t.interrupt();
286 <        t.join();
286 >        awaitTermination(t);
287          assertEquals(SIZE, q.size());
288          assertEquals(0, q.remainingCapacity());
289      }
290  
291      /**
292 <     * put blocks waiting for take when full
292 >     * put blocks interruptibly waiting for take when full
293       */
294      public void testPutWithTake() throws InterruptedException {
295          final int capacity = 2;
296          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
297 <        Thread t = new Thread(new CheckedRunnable() {
297 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
298 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
299 >        Thread t = newStartedThread(new CheckedRunnable() {
300              public void realRun() throws InterruptedException {
301 <                for (int i = 0; i < capacity + 1; i++)
301 >                for (int i = 0; i < capacity; i++)
302                      q.put(i);
303 +                pleaseTake.countDown();
304 +                q.put(86);
305 +
306 +                pleaseInterrupt.countDown();
307                  try {
308                      q.put(99);
309                      shouldThrow();
310                  } catch (InterruptedException success) {}
311 +                assertFalse(Thread.interrupted());
312              }});
313  
314 <        t.start();
342 <        Thread.sleep(SHORT_DELAY_MS);
314 >        await(pleaseTake);
315          assertEquals(q.remainingCapacity(), 0);
316          assertEquals(0, q.take());
317 <        Thread.sleep(SHORT_DELAY_MS);
317 >
318 >        await(pleaseInterrupt);
319 >        assertThreadStaysAlive(t);
320          t.interrupt();
321 <        t.join();
321 >        awaitTermination(t);
322          assertEquals(q.remainingCapacity(), 0);
323      }
324  
325      /**
326       * timed offer times out if full and elements not taken
327       */
328 <    public void testTimedOffer() throws InterruptedException {
328 >    public void testTimedOffer() {
329          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
330 <        Thread t = new Thread(new CheckedRunnable() {
330 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
331 >        Thread t = newStartedThread(new CheckedRunnable() {
332              public void realRun() throws InterruptedException {
333                  q.put(new Object());
334                  q.put(new Object());
335 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
335 >                long startTime = System.nanoTime();
336 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
337 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
338 >                pleaseInterrupt.countDown();
339                  try {
340 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
340 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
341                      shouldThrow();
342                  } catch (InterruptedException success) {}
343              }});
344  
345 <        t.start();
346 <        Thread.sleep(SMALL_DELAY_MS);
345 >        await(pleaseInterrupt);
346 >        assertThreadStaysAlive(t);
347          t.interrupt();
348 <        t.join();
348 >        awaitTermination(t);
349      }
350  
351      /**
# Line 384 | Line 362 | public class LinkedBlockingQueueTest ext
362       * Take removes existing elements until empty, then blocks interruptibly
363       */
364      public void testBlockingTake() throws InterruptedException {
365 <        final LinkedBlockingQueue q = populatedQueue(SIZE);
366 <        Thread t = new Thread(new CheckedRunnable() {
365 >        final BlockingQueue q = populatedQueue(SIZE);
366 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
367 >        Thread t = newStartedThread(new CheckedRunnable() {
368              public void realRun() throws InterruptedException {
369                  for (int i = 0; i < SIZE; ++i) {
370                      assertEquals(i, q.take());
371                  }
372 +
373 +                Thread.currentThread().interrupt();
374                  try {
375                      q.take();
376                      shouldThrow();
377                  } catch (InterruptedException success) {}
378 +                assertFalse(Thread.interrupted());
379 +
380 +                pleaseInterrupt.countDown();
381 +                try {
382 +                    q.take();
383 +                    shouldThrow();
384 +                } catch (InterruptedException success) {}
385 +                assertFalse(Thread.interrupted());
386              }});
387  
388 <        t.start();
389 <        Thread.sleep(SHORT_DELAY_MS);
388 >        await(pleaseInterrupt);
389 >        assertThreadStaysAlive(t);
390          t.interrupt();
391 <        t.join();
391 >        awaitTermination(t);
392      }
393  
394      /**
# Line 428 | Line 417 | public class LinkedBlockingQueueTest ext
417       * timed poll with nonzero timeout succeeds when non-empty, else times out
418       */
419      public void testTimedPoll() throws InterruptedException {
420 <        LinkedBlockingQueue q = populatedQueue(SIZE);
420 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
421          for (int i = 0; i < SIZE; ++i) {
422 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
423 <        }
424 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422 >            long startTime = System.nanoTime();
423 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
424 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
425 >        }
426 >        long startTime = System.nanoTime();
427 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
428 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
429 >        checkEmpty(q);
430      }
431  
432      /**
# Line 440 | Line 434 | public class LinkedBlockingQueueTest ext
434       * returning timeout status
435       */
436      public void testInterruptedTimedPoll() throws InterruptedException {
437 <        Thread t = new Thread(new CheckedRunnable() {
437 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
438 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
439 >        Thread t = newStartedThread(new CheckedRunnable() {
440              public void realRun() throws InterruptedException {
445                LinkedBlockingQueue q = populatedQueue(SIZE);
441                  for (int i = 0; i < SIZE; ++i) {
442 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
442 >                    long t0 = System.nanoTime();
443 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
444 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
445                  }
446 +                long t0 = System.nanoTime();
447 +                aboutToWait.countDown();
448                  try {
449 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
449 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
450                      shouldThrow();
451 <                } catch (InterruptedException success) {}
451 >                } catch (InterruptedException success) {
452 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
453 >                }
454              }});
455  
456 <        t.start();
457 <        Thread.sleep(SHORT_DELAY_MS);
456 >        aboutToWait.await();
457 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
458          t.interrupt();
459 <        t.join();
459 >        awaitTermination(t, MEDIUM_DELAY_MS);
460 >        checkEmpty(q);
461      }
462  
463      /**
# Line 507 | Line 509 | public class LinkedBlockingQueueTest ext
509      public void testRemoveElement() {
510          LinkedBlockingQueue q = populatedQueue(SIZE);
511          for (int i = 1; i < SIZE; i+=2) {
512 <            assertTrue(q.remove(new Integer(i)));
512 >            assertTrue(q.contains(i));
513 >            assertTrue(q.remove(i));
514 >            assertFalse(q.contains(i));
515 >            assertTrue(q.contains(i-1));
516          }
517          for (int i = 0; i < SIZE; i+=2) {
518 <            assertTrue(q.remove(new Integer(i)));
519 <            assertFalse(q.remove(new Integer(i+1)));
518 >            assertTrue(q.contains(i));
519 >            assertTrue(q.remove(i));
520 >            assertFalse(q.contains(i));
521 >            assertFalse(q.remove(i+1));
522 >            assertFalse(q.contains(i+1));
523          }
524          assertTrue(q.isEmpty());
525      }
# Line 607 | Line 615 | public class LinkedBlockingQueueTest ext
615      }
616  
617      /**
618 <     * toArray contains all elements
618 >     * toArray contains all elements in FIFO order
619       */
620 <    public void testToArray() throws InterruptedException {
620 >    public void testToArray() {
621          LinkedBlockingQueue q = populatedQueue(SIZE);
622          Object[] o = q.toArray();
623          for (int i = 0; i < o.length; i++)
624 <            assertEquals(o[i], q.take());
624 >            assertSame(o[i], q.poll());
625      }
626  
627      /**
628 <     * toArray(a) contains all elements
628 >     * toArray(a) contains all elements in FIFO order
629       */
630      public void testToArray2() throws InterruptedException {
631 <        LinkedBlockingQueue q = populatedQueue(SIZE);
631 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
632          Integer[] ints = new Integer[SIZE];
633 <        ints = (Integer[])q.toArray(ints);
633 >        Integer[] array = q.toArray(ints);
634 >        assertSame(ints, array);
635          for (int i = 0; i < ints.length; i++)
636 <            assertEquals(ints[i], q.take());
628 <    }
629 <
630 <    /**
631 <     * toArray(null) throws NPE
632 <     */
633 <    public void testToArray_BadArg() {
634 <        LinkedBlockingQueue q = populatedQueue(SIZE);
635 <        try {
636 <            Object o[] = q.toArray(null);
637 <            shouldThrow();
638 <        } catch (NullPointerException success) {}
636 >            assertSame(ints[i], q.poll());
637      }
638  
639      /**
# Line 649 | Line 647 | public class LinkedBlockingQueueTest ext
647          } catch (ArrayStoreException success) {}
648      }
649  
652
650      /**
651       * iterator iterates through all elements
652       */
# Line 680 | Line 677 | public class LinkedBlockingQueueTest ext
677          assertFalse(it.hasNext());
678      }
679  
683
680      /**
681       * iterator ordering is FIFO
682       */
# Line 712 | Line 708 | public class LinkedBlockingQueueTest ext
708          assertEquals(0, q.size());
709      }
710  
715
711      /**
712       * toString contains toStrings of elements
713       */
# Line 720 | Line 715 | public class LinkedBlockingQueueTest ext
715          LinkedBlockingQueue q = populatedQueue(SIZE);
716          String s = q.toString();
717          for (int i = 0; i < SIZE; ++i) {
718 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
718 >            assertTrue(s.contains(String.valueOf(i)));
719          }
720      }
721  
727
722      /**
723       * offer transfers elements across Executor tasks
724       */
# Line 733 | Line 727 | public class LinkedBlockingQueueTest ext
727          q.add(one);
728          q.add(two);
729          ExecutorService executor = Executors.newFixedThreadPool(2);
730 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
731          executor.execute(new CheckedRunnable() {
732              public void realRun() throws InterruptedException {
733                  assertFalse(q.offer(three));
734 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
734 >                threadsStarted.await();
735 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
736                  assertEquals(0, q.remainingCapacity());
737              }});
738  
739          executor.execute(new CheckedRunnable() {
740              public void realRun() throws InterruptedException {
741 <                Thread.sleep(SMALL_DELAY_MS);
741 >                threadsStarted.await();
742                  assertSame(one, q.take());
743              }});
744  
# Line 750 | Line 746 | public class LinkedBlockingQueueTest ext
746      }
747  
748      /**
749 <     * poll retrieves elements across Executor threads
749 >     * timed poll retrieves elements across Executor threads
750       */
751      public void testPollInExecutor() {
752          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
753 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
754          ExecutorService executor = Executors.newFixedThreadPool(2);
755          executor.execute(new CheckedRunnable() {
756              public void realRun() throws InterruptedException {
757                  assertNull(q.poll());
758 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
759 <                assertTrue(q.isEmpty());
758 >                threadsStarted.await();
759 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
760 >                checkEmpty(q);
761              }});
762  
763          executor.execute(new CheckedRunnable() {
764              public void realRun() throws InterruptedException {
765 <                Thread.sleep(SMALL_DELAY_MS);
765 >                threadsStarted.await();
766                  q.put(one);
767              }});
768  
# Line 775 | Line 773 | public class LinkedBlockingQueueTest ext
773       * A deserialized serialized queue has same elements in same order
774       */
775      public void testSerialization() throws Exception {
776 <        LinkedBlockingQueue q = populatedQueue(SIZE);
776 >        Queue x = populatedQueue(SIZE);
777 >        Queue y = serialClone(x);
778  
779 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
780 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
781 <        out.writeObject(q);
782 <        out.close();
783 <
784 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
785 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
786 <        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
787 <        assertEquals(q.size(), r.size());
789 <        while (!q.isEmpty())
790 <            assertEquals(q.remove(), r.remove());
791 <    }
792 <
793 <    /**
794 <     * drainTo(null) throws NPE
795 <     */
796 <    public void testDrainToNull() {
797 <        LinkedBlockingQueue q = populatedQueue(SIZE);
798 <        try {
799 <            q.drainTo(null);
800 <            shouldThrow();
801 <        } catch (NullPointerException success) {}
802 <    }
803 <
804 <    /**
805 <     * drainTo(this) throws IAE
806 <     */
807 <    public void testDrainToSelf() {
808 <        LinkedBlockingQueue q = populatedQueue(SIZE);
809 <        try {
810 <            q.drainTo(q);
811 <            shouldThrow();
812 <        } catch (IllegalArgumentException success) {}
779 >        assertTrue(x != y);
780 >        assertEquals(x.size(), y.size());
781 >        assertEquals(x.toString(), y.toString());
782 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
783 >        while (!x.isEmpty()) {
784 >            assertFalse(y.isEmpty());
785 >            assertEquals(x.remove(), y.remove());
786 >        }
787 >        assertTrue(y.isEmpty());
788      }
789  
790      /**
# Line 857 | Line 832 | public class LinkedBlockingQueueTest ext
832      }
833  
834      /**
860     * drainTo(null, n) throws NPE
861     */
862    public void testDrainToNullN() {
863        LinkedBlockingQueue q = populatedQueue(SIZE);
864        try {
865            q.drainTo(null, 0);
866            shouldThrow();
867        } catch (NullPointerException success) {}
868    }
869
870    /**
871     * drainTo(this, n) throws IAE
872     */
873    public void testDrainToSelfN() {
874        LinkedBlockingQueue q = populatedQueue(SIZE);
875        try {
876            q.drainTo(q, 0);
877            shouldThrow();
878        } catch (IllegalArgumentException success) {}
879    }
880
881    /**
835       * drainTo(c, n) empties first min(n, size) elements of queue into c
836       */
837      public void testDrainToN() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines