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.13 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.43 by jsr166, Sat May 21 06:24:33 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   */
# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17 +    public static class Unbounded extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new LinkedBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class Bounded extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new LinkedBlockingQueue(20);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32  
33      public static Test suite() {
34 <        return new TestSuite(LinkedBlockingQueueTest.class);
34 >        return newTestSuite(LinkedBlockingQueueTest.class,
35 >                            new Unbounded().testSuite(),
36 >                            new Bounded().testSuite());
37      }
38  
39  
# Line 26 | Line 41 | public class LinkedBlockingQueueTest ext
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private LinkedBlockingQueue populatedQueue(int n) {
45 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
44 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
45 >        LinkedBlockingQueue<Integer> q =
46 >            new LinkedBlockingQueue<Integer>(n);
47          assertTrue(q.isEmpty());
48 <        for (int i = 0; i < n; i++)
49 <            assertTrue(q.offer(new Integer(i)));
48 >        for (int i = 0; i < n; i++)
49 >            assertTrue(q.offer(new Integer(i)));
50          assertFalse(q.isEmpty());
51          assertEquals(0, q.remainingCapacity());
52 <        assertEquals(n, q.size());
52 >        assertEquals(n, q.size());
53          return q;
54      }
55  
# Line 47 | Line 63 | public class LinkedBlockingQueueTest ext
63      }
64  
65      /**
66 <     * Constructor throws IAE if  capacity argument nonpositive
66 >     * Constructor throws IAE if capacity argument nonpositive
67       */
68      public void testConstructor2() {
69          try {
70              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
71              shouldThrow();
72 <        }
57 <        catch (IllegalArgumentException success) {}
72 >        } catch (IllegalArgumentException success) {}
73      }
74  
75      /**
# Line 64 | Line 79 | public class LinkedBlockingQueueTest ext
79          try {
80              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
81              shouldThrow();
82 <        }
68 <        catch (NullPointerException success) {}
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 76 | Line 90 | public class LinkedBlockingQueueTest ext
90              Integer[] ints = new Integer[SIZE];
91              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
92              shouldThrow();
93 <        }
80 <        catch (NullPointerException success) {}
93 >        } catch (NullPointerException success) {}
94      }
95  
96      /**
# Line 90 | Line 103 | public class LinkedBlockingQueueTest ext
103                  ints[i] = new Integer(i);
104              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
105              shouldThrow();
106 <        }
94 <        catch (NullPointerException success) {}
106 >        } catch (NullPointerException success) {}
107      }
108  
109      /**
110       * Queue contains all elements of collection used to initialize
111       */
112      public void testConstructor6() {
113 <        try {
114 <            Integer[] ints = new Integer[SIZE];
115 <            for (int i = 0; i < SIZE; ++i)
116 <                ints[i] = new Integer(i);
117 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
118 <            for (int i = 0; i < SIZE; ++i)
107 <                assertEquals(ints[i], q.poll());
108 <        }
109 <        finally {}
113 >        Integer[] ints = new Integer[SIZE];
114 >        for (int i = 0; i < SIZE; ++i)
115 >            ints[i] = new Integer(i);
116 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
117 >        for (int i = 0; i < SIZE; ++i)
118 >            assertEquals(ints[i], q.poll());
119      }
120  
121      /**
# Line 145 | Line 154 | public class LinkedBlockingQueueTest ext
154       * offer(null) throws NPE
155       */
156      public void testOfferNull() {
157 <        try {
157 >        try {
158              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159              q.offer(null);
160              shouldThrow();
161 <        } catch (NullPointerException success) { }
161 >        } catch (NullPointerException success) {}
162      }
163  
164      /**
165       * add(null) throws NPE
166       */
167      public void testAddNull() {
168 <        try {
168 >        try {
169              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170              q.add(null);
171              shouldThrow();
172 <        } catch (NullPointerException success) { }
172 >        } catch (NullPointerException success) {}
173      }
174  
175      /**
# Line 176 | Line 185 | public class LinkedBlockingQueueTest ext
185       * add succeeds if not full; throws ISE if full
186       */
187      public void testAdd() {
188 <        try {
188 >        try {
189              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
190              for (int i = 0; i < SIZE; ++i) {
191                  assertTrue(q.add(new Integer(i)));
192              }
193              assertEquals(0, q.remainingCapacity());
194              q.add(new Integer(SIZE));
195 <        } catch (IllegalStateException success){
196 <        }
195 >            shouldThrow();
196 >        } catch (IllegalStateException success) {}
197      }
198  
199      /**
# Line 195 | Line 204 | public class LinkedBlockingQueueTest ext
204              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
205              q.addAll(null);
206              shouldThrow();
207 <        }
199 <        catch (NullPointerException success) {}
207 >        } catch (NullPointerException success) {}
208      }
209  
210      /**
# Line 207 | Line 215 | public class LinkedBlockingQueueTest ext
215              LinkedBlockingQueue q = populatedQueue(SIZE);
216              q.addAll(q);
217              shouldThrow();
218 <        }
211 <        catch (IllegalArgumentException success) {}
218 >        } catch (IllegalArgumentException success) {}
219      }
220  
221      /**
# Line 220 | Line 227 | public class LinkedBlockingQueueTest ext
227              Integer[] ints = new Integer[SIZE];
228              q.addAll(Arrays.asList(ints));
229              shouldThrow();
230 <        }
224 <        catch (NullPointerException success) {}
230 >        } catch (NullPointerException success) {}
231      }
232 +
233      /**
234       * addAll of a collection with any null elements throws NPE after
235       * possibly adding some elements
# Line 235 | Line 242 | public class LinkedBlockingQueueTest ext
242                  ints[i] = new Integer(i);
243              q.addAll(Arrays.asList(ints));
244              shouldThrow();
245 <        }
239 <        catch (NullPointerException success) {}
245 >        } catch (NullPointerException success) {}
246      }
247 +
248      /**
249       * addAll throws ISE if not enough room
250       */
# Line 249 | Line 256 | public class LinkedBlockingQueueTest ext
256                  ints[i] = new Integer(i);
257              q.addAll(Arrays.asList(ints));
258              shouldThrow();
259 <        }
253 <        catch (IllegalStateException success) {}
259 >        } catch (IllegalStateException success) {}
260      }
261 +
262      /**
263       * Queue contains all elements, in traversal order, of successful addAll
264       */
265      public void testAddAll5() {
266 <        try {
267 <            Integer[] empty = new Integer[0];
268 <            Integer[] ints = new Integer[SIZE];
269 <            for (int i = 0; i < SIZE; ++i)
270 <                ints[i] = new Integer(i);
271 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
272 <            assertFalse(q.addAll(Arrays.asList(empty)));
273 <            assertTrue(q.addAll(Arrays.asList(ints)));
274 <            for (int i = 0; i < SIZE; ++i)
268 <                assertEquals(ints[i], q.poll());
269 <        }
270 <        finally {}
266 >        Integer[] empty = new Integer[0];
267 >        Integer[] ints = new Integer[SIZE];
268 >        for (int i = 0; i < SIZE; ++i)
269 >            ints[i] = new Integer(i);
270 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
271 >        assertFalse(q.addAll(Arrays.asList(empty)));
272 >        assertTrue(q.addAll(Arrays.asList(ints)));
273 >        for (int i = 0; i < SIZE; ++i)
274 >            assertEquals(ints[i], q.poll());
275      }
276  
277      /**
278       * put(null) throws NPE
279       */
280 <     public void testPutNull() {
281 <        try {
280 >    public void testPutNull() throws InterruptedException {
281 >        try {
282              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283              q.put(null);
284              shouldThrow();
285 <        }
286 <        catch (NullPointerException success){
283 <        }
284 <        catch (InterruptedException ie) {
285 <            unexpectedException();
286 <        }
287 <     }
285 >        } catch (NullPointerException success) {}
286 >    }
287  
288      /**
289       * all elements successfully put are contained
290       */
291 <     public void testPut() {
292 <         try {
293 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
294 <             for (int i = 0; i < SIZE; ++i) {
295 <                 Integer I = new Integer(i);
296 <                 q.put(I);
298 <                 assertTrue(q.contains(I));
299 <             }
300 <             assertEquals(0, q.remainingCapacity());
301 <         }
302 <        catch (InterruptedException ie) {
303 <            unexpectedException();
291 >    public void testPut() throws InterruptedException {
292 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
293 >        for (int i = 0; i < SIZE; ++i) {
294 >            Integer I = new Integer(i);
295 >            q.put(I);
296 >            assertTrue(q.contains(I));
297          }
298 +        assertEquals(0, q.remainingCapacity());
299      }
300  
301      /**
302       * put blocks interruptibly if full
303       */
304 <    public void testBlockingPut() {
305 <        Thread t = new Thread(new Runnable() {
306 <                public void run() {
307 <                    int added = 0;
308 <                    try {
309 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
310 <                        for (int i = 0; i < SIZE; ++i) {
311 <                            q.put(new Integer(i));
312 <                            ++added;
313 <                        }
314 <                        q.put(new Integer(SIZE));
315 <                        threadShouldThrow();
316 <                    } catch (InterruptedException ie){
317 <                        threadAssertEquals(added, SIZE);
324 <                    }
325 <                }});
304 >    public void testBlockingPut() throws InterruptedException {
305 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
306 >        Thread t = new Thread(new CheckedRunnable() {
307 >            public void realRun() throws InterruptedException {
308 >                for (int i = 0; i < SIZE; ++i)
309 >                    q.put(i);
310 >                assertEquals(SIZE, q.size());
311 >                assertEquals(0, q.remainingCapacity());
312 >                try {
313 >                    q.put(99);
314 >                    shouldThrow();
315 >                } catch (InterruptedException success) {}
316 >            }});
317 >
318          t.start();
319 <        try {
320 <           Thread.sleep(SHORT_DELAY_MS);
321 <           t.interrupt();
322 <           t.join();
323 <        }
332 <        catch (InterruptedException ie) {
333 <            unexpectedException();
334 <        }
319 >        delay(SHORT_DELAY_MS);
320 >        t.interrupt();
321 >        t.join();
322 >        assertEquals(SIZE, q.size());
323 >        assertEquals(0, q.remainingCapacity());
324      }
325  
326      /**
327       * put blocks waiting for take when full
328       */
329 <    public void testPutWithTake() {
329 >    public void testPutWithTake() throws InterruptedException {
330 >        final int capacity = 2;
331          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
332 <        Thread t = new Thread(new Runnable() {
333 <                public void run() {
334 <                    int added = 0;
335 <                    try {
336 <                        q.put(new Object());
337 <                        ++added;
338 <                        q.put(new Object());
339 <                        ++added;
340 <                        q.put(new Object());
341 <                        ++added;
342 <                        q.put(new Object());
343 <                        ++added;
344 <                        threadShouldThrow();
345 <                    } catch (InterruptedException e){
346 <                        threadAssertTrue(added >= 2);
347 <                    }
348 <                }
349 <            });
360 <        try {
361 <            t.start();
362 <            Thread.sleep(SHORT_DELAY_MS);
363 <            q.take();
364 <            t.interrupt();
365 <            t.join();
366 <        } catch (Exception e){
367 <            unexpectedException();
368 <        }
332 >        Thread t = new Thread(new CheckedRunnable() {
333 >            public void realRun() throws InterruptedException {
334 >                for (int i = 0; i < capacity + 1; i++)
335 >                    q.put(i);
336 >                try {
337 >                    q.put(99);
338 >                    shouldThrow();
339 >                } catch (InterruptedException success) {}
340 >            }});
341 >
342 >        t.start();
343 >        delay(SHORT_DELAY_MS);
344 >        assertEquals(q.remainingCapacity(), 0);
345 >        assertEquals(0, q.take());
346 >        delay(SHORT_DELAY_MS);
347 >        t.interrupt();
348 >        t.join();
349 >        assertEquals(q.remainingCapacity(), 0);
350      }
351  
352      /**
# Line 373 | Line 354 | public class LinkedBlockingQueueTest ext
354       */
355      public void testTimedOffer() {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new Thread(new Runnable() {
358 <                public void run() {
359 <                    try {
360 <                        q.put(new Object());
361 <                        q.put(new Object());
362 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
363 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
364 <                        threadShouldThrow();
365 <                    } catch (InterruptedException success){}
366 <                }
367 <            });
368 <
369 <        try {
370 <            t.start();
371 <            Thread.sleep(SMALL_DELAY_MS);
372 <            t.interrupt();
373 <            t.join();
374 <        } catch (Exception e){
394 <            unexpectedException();
395 <        }
357 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
358 >        Thread t = newStartedThread(new CheckedRunnable() {
359 >            public void realRun() throws InterruptedException {
360 >                q.put(new Object());
361 >                q.put(new Object());
362 >                long startTime = System.nanoTime();
363 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
364 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
365 >                pleaseInterrupt.countDown();
366 >                try {
367 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
368 >                    shouldThrow();
369 >                } catch (InterruptedException success) {}
370 >            }});
371 >
372 >        await(pleaseInterrupt);
373 >        t.interrupt();
374 >        awaitTermination(t);
375      }
376  
377      /**
378       * take retrieves elements in FIFO order
379       */
380 <    public void testTake() {
381 <        try {
382 <            LinkedBlockingQueue q = populatedQueue(SIZE);
383 <            for (int i = 0; i < SIZE; ++i) {
405 <                assertEquals(i, ((Integer)q.take()).intValue());
406 <            }
407 <        } catch (InterruptedException e){
408 <            unexpectedException();
409 <        }
410 <    }
411 <
412 <    /**
413 <     * take blocks interruptibly when empty
414 <     */
415 <    public void testTakeFromEmpty() {
416 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
417 <        Thread t = new Thread(new Runnable() {
418 <                public void run() {
419 <                    try {
420 <                        q.take();
421 <                        threadShouldThrow();
422 <                    } catch (InterruptedException success){ }
423 <                }
424 <            });
425 <        try {
426 <            t.start();
427 <            Thread.sleep(SHORT_DELAY_MS);
428 <            t.interrupt();
429 <            t.join();
430 <        } catch (Exception e){
431 <            unexpectedException();
380 >    public void testTake() throws InterruptedException {
381 >        LinkedBlockingQueue q = populatedQueue(SIZE);
382 >        for (int i = 0; i < SIZE; ++i) {
383 >            assertEquals(i, q.take());
384          }
385      }
386  
387      /**
388       * Take removes existing elements until empty, then blocks interruptibly
389       */
390 <    public void testBlockingTake() {
391 <        Thread t = new Thread(new Runnable() {
392 <                public void run() {
393 <                    try {
394 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
395 <                        for (int i = 0; i < SIZE; ++i) {
396 <                            assertEquals(i, ((Integer)q.take()).intValue());
397 <                        }
398 <                        q.take();
399 <                        threadShouldThrow();
400 <                    } catch (InterruptedException success){
401 <                    }
402 <                }});
390 >    public void testBlockingTake() throws InterruptedException {
391 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
392 >        Thread t = new Thread(new CheckedRunnable() {
393 >            public void realRun() throws InterruptedException {
394 >                for (int i = 0; i < SIZE; ++i) {
395 >                    assertEquals(i, q.take());
396 >                }
397 >                try {
398 >                    q.take();
399 >                    shouldThrow();
400 >                } catch (InterruptedException success) {}
401 >            }});
402 >
403          t.start();
404 <        try {
405 <           Thread.sleep(SHORT_DELAY_MS);
406 <           t.interrupt();
455 <           t.join();
456 <        }
457 <        catch (InterruptedException ie) {
458 <            unexpectedException();
459 <        }
404 >        delay(SHORT_DELAY_MS);
405 >        t.interrupt();
406 >        t.join();
407      }
408  
462
409      /**
410       * poll succeeds unless empty
411       */
412      public void testPoll() {
413          LinkedBlockingQueue q = populatedQueue(SIZE);
414          for (int i = 0; i < SIZE; ++i) {
415 <            assertEquals(i, ((Integer)q.poll()).intValue());
415 >            assertEquals(i, q.poll());
416          }
417 <        assertNull(q.poll());
417 >        assertNull(q.poll());
418      }
419  
420      /**
421 <     * timed pool with zero timeout succeeds when non-empty, else times out
421 >     * timed poll with zero timeout succeeds when non-empty, else times out
422       */
423 <    public void testTimedPoll0() {
424 <        try {
425 <            LinkedBlockingQueue q = populatedQueue(SIZE);
426 <            for (int i = 0; i < SIZE; ++i) {
427 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
428 <            }
483 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484 <        } catch (InterruptedException e){
485 <            unexpectedException();
486 <        }
423 >    public void testTimedPoll0() throws InterruptedException {
424 >        LinkedBlockingQueue q = populatedQueue(SIZE);
425 >        for (int i = 0; i < SIZE; ++i) {
426 >            assertEquals(i, q.poll(0, MILLISECONDS));
427 >        }
428 >        assertNull(q.poll(0, MILLISECONDS));
429      }
430  
431      /**
432 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
432 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
433       */
434 <    public void testTimedPoll() {
435 <        try {
436 <            LinkedBlockingQueue q = populatedQueue(SIZE);
437 <            for (int i = 0; i < SIZE; ++i) {
438 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
439 <            }
498 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499 <        } catch (InterruptedException e){
500 <            unexpectedException();
501 <        }
434 >    public void testTimedPoll() throws InterruptedException {
435 >        LinkedBlockingQueue q = populatedQueue(SIZE);
436 >        for (int i = 0; i < SIZE; ++i) {
437 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
438 >        }
439 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
440      }
441  
442      /**
443       * Interrupted timed poll throws InterruptedException instead of
444       * returning timeout status
445       */
446 <    public void testInterruptedTimedPoll() {
447 <        Thread t = new Thread(new Runnable() {
448 <                public void run() {
449 <                    try {
450 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
451 <                        for (int i = 0; i < SIZE; ++i) {
452 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
453 <                        }
454 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
517 <                    } catch (InterruptedException success){
518 <                    }
519 <                }});
520 <        t.start();
521 <        try {
522 <           Thread.sleep(SHORT_DELAY_MS);
523 <           t.interrupt();
524 <           t.join();
525 <        }
526 <        catch (InterruptedException ie) {
527 <            unexpectedException();
528 <        }
529 <    }
530 <
531 <    /**
532 <     *  timed poll before a delayed offer fails; after offer succeeds;
533 <     *  on interruption throws
534 <     */
535 <    public void testTimedPollWithOffer() {
536 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
537 <        Thread t = new Thread(new Runnable() {
538 <                public void run() {
539 <                    try {
540 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
541 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
542 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
543 <                        threadShouldThrow();
544 <                    } catch (InterruptedException success) { }
446 >    public void testInterruptedTimedPoll() throws InterruptedException {
447 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
448 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
449 >        Thread t = newStartedThread(new CheckedRunnable() {
450 >            public void realRun() throws InterruptedException {
451 >                for (int i = 0; i < SIZE; ++i) {
452 >                    long t0 = System.nanoTime();
453 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
454 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
455                  }
456 <            });
457 <        try {
458 <            t.start();
459 <            Thread.sleep(SMALL_DELAY_MS);
460 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
461 <            t.interrupt();
462 <            t.join();
463 <        } catch (Exception e){
464 <            unexpectedException();
465 <        }
456 >                long t0 = System.nanoTime();
457 >                aboutToWait.countDown();
458 >                try {
459 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
460 >                    shouldThrow();
461 >                } catch (InterruptedException success) {
462 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
463 >                }
464 >            }});
465 >
466 >        aboutToWait.await();
467 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
468 >        t.interrupt();
469 >        awaitTermination(t, MEDIUM_DELAY_MS);
470 >        checkEmpty(q);
471      }
472  
473      /**
# Line 561 | Line 476 | public class LinkedBlockingQueueTest ext
476      public void testPeek() {
477          LinkedBlockingQueue q = populatedQueue(SIZE);
478          for (int i = 0; i < SIZE; ++i) {
479 <            assertEquals(i, ((Integer)q.peek()).intValue());
480 <            q.poll();
479 >            assertEquals(i, q.peek());
480 >            assertEquals(i, q.poll());
481              assertTrue(q.peek() == null ||
482 <                       i != ((Integer)q.peek()).intValue());
482 >                       !q.peek().equals(i));
483          }
484 <        assertNull(q.peek());
484 >        assertNull(q.peek());
485      }
486  
487      /**
# Line 575 | Line 490 | public class LinkedBlockingQueueTest ext
490      public void testElement() {
491          LinkedBlockingQueue q = populatedQueue(SIZE);
492          for (int i = 0; i < SIZE; ++i) {
493 <            assertEquals(i, ((Integer)q.element()).intValue());
494 <            q.poll();
493 >            assertEquals(i, q.element());
494 >            assertEquals(i, q.poll());
495          }
496          try {
497              q.element();
498              shouldThrow();
499 <        }
585 <        catch (NoSuchElementException success) {}
499 >        } catch (NoSuchElementException success) {}
500      }
501  
502      /**
# Line 591 | Line 505 | public class LinkedBlockingQueueTest ext
505      public void testRemove() {
506          LinkedBlockingQueue q = populatedQueue(SIZE);
507          for (int i = 0; i < SIZE; ++i) {
508 <            assertEquals(i, ((Integer)q.remove()).intValue());
508 >            assertEquals(i, q.remove());
509          }
510          try {
511              q.remove();
512              shouldThrow();
513 <        } catch (NoSuchElementException success){
600 <        }
513 >        } catch (NoSuchElementException success) {}
514      }
515  
516      /**
# Line 606 | Line 519 | public class LinkedBlockingQueueTest ext
519      public void testRemoveElement() {
520          LinkedBlockingQueue q = populatedQueue(SIZE);
521          for (int i = 1; i < SIZE; i+=2) {
522 <            assertTrue(q.remove(new Integer(i)));
522 >            assertTrue(q.contains(i));
523 >            assertTrue(q.remove(i));
524 >            assertFalse(q.contains(i));
525 >            assertTrue(q.contains(i-1));
526          }
527          for (int i = 0; i < SIZE; i+=2) {
528 <            assertTrue(q.remove(new Integer(i)));
529 <            assertFalse(q.remove(new Integer(i+1)));
528 >            assertTrue(q.contains(i));
529 >            assertTrue(q.remove(i));
530 >            assertFalse(q.contains(i));
531 >            assertFalse(q.remove(i+1));
532 >            assertFalse(q.contains(i+1));
533          }
534          assertTrue(q.isEmpty());
535      }
# Line 618 | Line 537 | public class LinkedBlockingQueueTest ext
537      /**
538       * An add following remove(x) succeeds
539       */
540 <    public void testRemoveElementAndAdd() {
541 <        try {
542 <            LinkedBlockingQueue q = new LinkedBlockingQueue();
543 <            assertTrue(q.add(new Integer(1)));
544 <            assertTrue(q.add(new Integer(2)));
545 <            assertTrue(q.remove(new Integer(1)));
546 <            assertTrue(q.remove(new Integer(2)));
547 <            assertTrue(q.add(new Integer(3)));
629 <            assertTrue(q.take() != null);
630 <        } catch (Exception e){
631 <            unexpectedException();
632 <        }
540 >    public void testRemoveElementAndAdd() throws InterruptedException {
541 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
542 >        assertTrue(q.add(new Integer(1)));
543 >        assertTrue(q.add(new Integer(2)));
544 >        assertTrue(q.remove(new Integer(1)));
545 >        assertTrue(q.remove(new Integer(2)));
546 >        assertTrue(q.add(new Integer(3)));
547 >        assertTrue(q.take() != null);
548      }
549  
550      /**
# Line 710 | Line 625 | public class LinkedBlockingQueueTest ext
625      }
626  
627      /**
628 <     * toArray contains all elements
628 >     * toArray contains all elements in FIFO order
629       */
630      public void testToArray() {
631          LinkedBlockingQueue q = populatedQueue(SIZE);
632 <        Object[] o = q.toArray();
633 <        try {
634 <        for (int i = 0; i < o.length; i++)
720 <            assertEquals(o[i], q.take());
721 <        } catch (InterruptedException e){
722 <            unexpectedException();
723 <        }
632 >        Object[] o = q.toArray();
633 >        for (int i = 0; i < o.length; i++)
634 >            assertSame(o[i], q.poll());
635      }
636  
637      /**
638 <     * toArray(a) contains all elements
638 >     * toArray(a) contains all elements in FIFO order
639       */
640 <    public void testToArray2() {
641 <        LinkedBlockingQueue q = populatedQueue(SIZE);
642 <        Integer[] ints = new Integer[SIZE];
643 <        ints = (Integer[])q.toArray(ints);
644 <        try {
645 <            for (int i = 0; i < ints.length; i++)
646 <                assertEquals(ints[i], q.take());
736 <        } catch (InterruptedException e){
737 <            unexpectedException();
738 <        }
640 >    public void testToArray2() throws InterruptedException {
641 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
642 >        Integer[] ints = new Integer[SIZE];
643 >        Integer[] array = q.toArray(ints);
644 >        assertSame(ints, array);
645 >        for (int i = 0; i < ints.length; i++)
646 >            assertSame(ints[i], q.poll());
647      }
648  
649      /**
650 <     * toArray(null) throws NPE
650 >     * toArray(null) throws NullPointerException
651       */
652 <    public void testToArray_BadArg() {
653 <        try {
654 <            LinkedBlockingQueue q = populatedQueue(SIZE);
655 <            Object o[] = q.toArray(null);
656 <            shouldThrow();
657 <        } catch (NullPointerException success){}
652 >    public void testToArray_NullArg() {
653 >        LinkedBlockingQueue q = populatedQueue(SIZE);
654 >        try {
655 >            q.toArray(null);
656 >            shouldThrow();
657 >        } catch (NullPointerException success) {}
658      }
659  
660      /**
661 <     * toArray with incompatible array type throws CCE
661 >     * toArray(incompatible array type) throws ArrayStoreException
662       */
663      public void testToArray1_BadArg() {
664 <        try {
665 <            LinkedBlockingQueue q = populatedQueue(SIZE);
666 <            Object o[] = q.toArray(new String[10] );
667 <            shouldThrow();
668 <        } catch (ArrayStoreException  success){}
664 >        LinkedBlockingQueue q = populatedQueue(SIZE);
665 >        try {
666 >            q.toArray(new String[10]);
667 >            shouldThrow();
668 >        } catch (ArrayStoreException success) {}
669      }
670  
671  
672      /**
673       * iterator iterates through all elements
674       */
675 <    public void testIterator() {
675 >    public void testIterator() throws InterruptedException {
676          LinkedBlockingQueue q = populatedQueue(SIZE);
677 <        Iterator it = q.iterator();
678 <        try {
679 <            while (it.hasNext()){
680 <                assertEquals(it.next(), q.take());
773 <            }
774 <        } catch (InterruptedException e){
775 <            unexpectedException();
776 <        }
677 >        Iterator it = q.iterator();
678 >        while (it.hasNext()) {
679 >            assertEquals(it.next(), q.take());
680 >        }
681      }
682  
683      /**
684       * iterator.remove removes current element
685       */
686 <    public void testIteratorRemove () {
686 >    public void testIteratorRemove() {
687          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
688          q.add(two);
689          q.add(one);
# Line 790 | Line 694 | public class LinkedBlockingQueueTest ext
694          it.remove();
695  
696          it = q.iterator();
697 <        assertEquals(it.next(), one);
698 <        assertEquals(it.next(), three);
697 >        assertSame(it.next(), one);
698 >        assertSame(it.next(), three);
699          assertFalse(it.hasNext());
700      }
701  
# Line 807 | Line 711 | public class LinkedBlockingQueueTest ext
711          assertEquals(0, q.remainingCapacity());
712          int k = 0;
713          for (Iterator it = q.iterator(); it.hasNext();) {
714 <            int i = ((Integer)(it.next())).intValue();
811 <            assertEquals(++k, i);
714 >            assertEquals(++k, it.next());
715          }
716          assertEquals(3, k);
717      }
# Line 816 | Line 719 | public class LinkedBlockingQueueTest ext
719      /**
720       * Modifications do not cause iterators to fail
721       */
722 <    public void testWeaklyConsistentIteration () {
722 >    public void testWeaklyConsistentIteration() {
723          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
724          q.add(one);
725          q.add(two);
726          q.add(three);
727 <        try {
728 <            for (Iterator it = q.iterator(); it.hasNext();) {
729 <                q.remove();
827 <                it.next();
828 <            }
829 <        }
830 <        catch (ConcurrentModificationException e) {
831 <            unexpectedException();
727 >        for (Iterator it = q.iterator(); it.hasNext();) {
728 >            q.remove();
729 >            it.next();
730          }
731          assertEquals(0, q.size());
732      }
# Line 854 | Line 752 | public class LinkedBlockingQueueTest ext
752          q.add(one);
753          q.add(two);
754          ExecutorService executor = Executors.newFixedThreadPool(2);
755 <        executor.execute(new Runnable() {
756 <            public void run() {
757 <                threadAssertFalse(q.offer(three));
758 <                try {
759 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
760 <                    threadAssertEquals(0, q.remainingCapacity());
761 <                }
762 <                catch (InterruptedException e) {
763 <                    threadUnexpectedException();
764 <                }
765 <            }
766 <        });
869 <
870 <        executor.execute(new Runnable() {
871 <            public void run() {
872 <                try {
873 <                    Thread.sleep(SMALL_DELAY_MS);
874 <                    threadAssertEquals(one, q.take());
875 <                }
876 <                catch (InterruptedException e) {
877 <                    threadUnexpectedException();
878 <                }
879 <            }
880 <        });
755 >        executor.execute(new CheckedRunnable() {
756 >            public void realRun() throws InterruptedException {
757 >                assertFalse(q.offer(three));
758 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
759 >                assertEquals(0, q.remainingCapacity());
760 >            }});
761 >
762 >        executor.execute(new CheckedRunnable() {
763 >            public void realRun() throws InterruptedException {
764 >                delay(SMALL_DELAY_MS);
765 >                assertSame(one, q.take());
766 >            }});
767  
768          joinPool(executor);
769      }
# Line 888 | Line 774 | public class LinkedBlockingQueueTest ext
774      public void testPollInExecutor() {
775          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
776          ExecutorService executor = Executors.newFixedThreadPool(2);
777 <        executor.execute(new Runnable() {
778 <            public void run() {
779 <                threadAssertNull(q.poll());
780 <                try {
781 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
782 <                    threadAssertTrue(q.isEmpty());
783 <                }
784 <                catch (InterruptedException e) {
785 <                    threadUnexpectedException();
786 <                }
787 <            }
788 <        });
903 <
904 <        executor.execute(new Runnable() {
905 <            public void run() {
906 <                try {
907 <                    Thread.sleep(SMALL_DELAY_MS);
908 <                    q.put(one);
909 <                }
910 <                catch (InterruptedException e) {
911 <                    threadUnexpectedException();
912 <                }
913 <            }
914 <        });
777 >        executor.execute(new CheckedRunnable() {
778 >            public void realRun() throws InterruptedException {
779 >                assertNull(q.poll());
780 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
781 >                assertTrue(q.isEmpty());
782 >            }});
783 >
784 >        executor.execute(new CheckedRunnable() {
785 >            public void realRun() throws InterruptedException {
786 >                delay(SMALL_DELAY_MS);
787 >                q.put(one);
788 >            }});
789  
790          joinPool(executor);
791      }
# Line 919 | Line 793 | public class LinkedBlockingQueueTest ext
793      /**
794       * A deserialized serialized queue has same elements in same order
795       */
796 <    public void testSerialization() {
796 >    public void testSerialization() throws Exception {
797          LinkedBlockingQueue q = populatedQueue(SIZE);
798  
799 <        try {
800 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
801 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
802 <            out.writeObject(q);
803 <            out.close();
804 <
805 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
806 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
807 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
808 <            assertEquals(q.size(), r.size());
809 <            while (!q.isEmpty())
936 <                assertEquals(q.remove(), r.remove());
937 <        } catch (Exception e){
938 <            unexpectedException();
939 <        }
799 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
800 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
801 >        out.writeObject(q);
802 >        out.close();
803 >
804 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
805 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
806 >        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
807 >        assertEquals(q.size(), r.size());
808 >        while (!q.isEmpty())
809 >            assertEquals(q.remove(), r.remove());
810      }
811  
812      /**
# Line 947 | Line 817 | public class LinkedBlockingQueueTest ext
817          try {
818              q.drainTo(null);
819              shouldThrow();
820 <        } catch (NullPointerException success) {
951 <        }
820 >        } catch (NullPointerException success) {}
821      }
822  
823      /**
# Line 959 | Line 828 | public class LinkedBlockingQueueTest ext
828          try {
829              q.drainTo(q);
830              shouldThrow();
831 <        } catch (IllegalArgumentException success) {
963 <        }
831 >        } catch (IllegalArgumentException success) {}
832      }
833  
834      /**
# Line 990 | Line 858 | public class LinkedBlockingQueueTest ext
858      /**
859       * drainTo empties full queue, unblocking a waiting put.
860       */
861 <    public void testDrainToWithActivePut() {
861 >    public void testDrainToWithActivePut() throws InterruptedException {
862          final LinkedBlockingQueue q = populatedQueue(SIZE);
863 <        Thread t = new Thread(new Runnable() {
864 <                public void run() {
865 <                    try {
866 <                        q.put(new Integer(SIZE+1));
867 <                    } catch (InterruptedException ie){
868 <                        threadUnexpectedException();
869 <                    }
870 <                }
871 <            });
872 <        try {
873 <            t.start();
874 <            ArrayList l = new ArrayList();
875 <            q.drainTo(l);
1008 <            assertTrue(l.size() >= SIZE);
1009 <            for (int i = 0; i < SIZE; ++i)
1010 <                assertEquals(l.get(i), new Integer(i));
1011 <            t.join();
1012 <            assertTrue(q.size() + l.size() >= SIZE);
1013 <        } catch (Exception e){
1014 <            unexpectedException();
1015 <        }
863 >        Thread t = new Thread(new CheckedRunnable() {
864 >            public void realRun() throws InterruptedException {
865 >                q.put(new Integer(SIZE+1));
866 >            }});
867 >
868 >        t.start();
869 >        ArrayList l = new ArrayList();
870 >        q.drainTo(l);
871 >        assertTrue(l.size() >= SIZE);
872 >        for (int i = 0; i < SIZE; ++i)
873 >            assertEquals(l.get(i), new Integer(i));
874 >        t.join();
875 >        assertTrue(q.size() + l.size() >= SIZE);
876      }
877  
878      /**
# Line 1023 | Line 883 | public class LinkedBlockingQueueTest ext
883          try {
884              q.drainTo(null, 0);
885              shouldThrow();
886 <        } catch (NullPointerException success) {
1027 <        }
886 >        } catch (NullPointerException success) {}
887      }
888  
889      /**
# Line 1035 | Line 894 | public class LinkedBlockingQueueTest ext
894          try {
895              q.drainTo(q, 0);
896              shouldThrow();
897 <        } catch (IllegalArgumentException success) {
1039 <        }
897 >        } catch (IllegalArgumentException success) {}
898      }
899  
900      /**
901 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
901 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
902       */
903      public void testDrainToN() {
904          LinkedBlockingQueue q = new LinkedBlockingQueue();
# Line 1049 | Line 907 | public class LinkedBlockingQueueTest ext
907                  assertTrue(q.offer(new Integer(j)));
908              ArrayList l = new ArrayList();
909              q.drainTo(l, i);
910 <            int k = (i < SIZE)? i : SIZE;
910 >            int k = (i < SIZE) ? i : SIZE;
911              assertEquals(l.size(), k);
912              assertEquals(q.size(), SIZE-k);
913              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines