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.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.24 by jsr166, Tue Dec 1 06:03:49 2009 UTC

# Line 284 | Line 284 | public class LinkedBlockingQueueTest ext
284       * put blocks interruptibly if full
285       */
286      public void testBlockingPut() throws InterruptedException {
287 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
288          Thread t = new Thread(new CheckedRunnable() {
289 <            public void realRun() {
290 <                int added = 0;
289 >            public void realRun() throws InterruptedException {
290 >                for (int i = 0; i < SIZE; ++i)
291 >                    q.put(i);
292 >                assertEquals(SIZE, q.size());
293 >                assertEquals(0, q.remainingCapacity());
294                  try {
295 <                    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
296 <                    for (int i = 0; i < SIZE; ++i) {
297 <                        q.put(new Integer(i));
294 <                        ++added;
295 <                    }
296 <                    q.put(new Integer(SIZE));
297 <                    threadShouldThrow();
298 <                } catch (InterruptedException success) {
299 <                    threadAssertEquals(added, SIZE);
300 <                }
295 >                    q.put(99);
296 >                    shouldThrow();
297 >                } catch (InterruptedException success) {}
298              }});
299  
300          t.start();
301          Thread.sleep(SHORT_DELAY_MS);
302          t.interrupt();
303          t.join();
304 +        assertEquals(SIZE, q.size());
305 +        assertEquals(0, q.remainingCapacity());
306      }
307  
308      /**
309       * put blocks waiting for take when full
310       */
311      public void testPutWithTake() throws InterruptedException {
312 +        final int capacity = 2;
313          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
314          Thread t = new Thread(new CheckedRunnable() {
315 <            public void realRun() {
316 <                int added = 0;
315 >            public void realRun() throws InterruptedException {
316 >                for (int i = 0; i < capacity + 1; i++)
317 >                    q.put(i);
318                  try {
319 <                    q.put(new Object());
320 <                    ++added;
321 <                    q.put(new Object());
321 <                    ++added;
322 <                    q.put(new Object());
323 <                    ++added;
324 <                    q.put(new Object());
325 <                    ++added;
326 <                    threadShouldThrow();
327 <                } catch (InterruptedException success) {
328 <                    threadAssertTrue(added >= 2);
329 <                }
319 >                    q.put(99);
320 >                    shouldThrow();
321 >                } catch (InterruptedException success) {}
322              }});
323  
324          t.start();
325          Thread.sleep(SHORT_DELAY_MS);
326 <        q.take();
326 >        assertEquals(q.remainingCapacity(), 0);
327 >        assertEquals(0, q.take());
328 >        Thread.sleep(SHORT_DELAY_MS);
329          t.interrupt();
330          t.join();
331 +        assertEquals(q.remainingCapacity(), 0);
332      }
333  
334      /**
# Line 341 | Line 336 | public class LinkedBlockingQueueTest ext
336       */
337      public void testTimedOffer() throws InterruptedException {
338          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
339 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
339 >        Thread t = new Thread(new CheckedRunnable() {
340              public void realRun() throws InterruptedException {
341                  q.put(new Object());
342                  q.put(new Object());
343 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
344 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
345 <            }};
343 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
344 >                try {
345 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
346 >                    shouldThrow();
347 >                } catch (InterruptedException success) {}
348 >            }});
349  
350          t.start();
351          Thread.sleep(SMALL_DELAY_MS);
# Line 361 | Line 359 | public class LinkedBlockingQueueTest ext
359      public void testTake() throws InterruptedException {
360          LinkedBlockingQueue q = populatedQueue(SIZE);
361          for (int i = 0; i < SIZE; ++i) {
362 <            assertEquals(i, ((Integer)q.take()).intValue());
362 >            assertEquals(i, q.take());
363          }
364      }
365  
# Line 385 | Line 383 | public class LinkedBlockingQueueTest ext
383       * Take removes existing elements until empty, then blocks interruptibly
384       */
385      public void testBlockingTake() throws InterruptedException {
386 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
386 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
387 >        Thread t = new Thread(new CheckedRunnable() {
388              public void realRun() throws InterruptedException {
390                LinkedBlockingQueue q = populatedQueue(SIZE);
389                  for (int i = 0; i < SIZE; ++i) {
390 <                    assertEquals(i, ((Integer)q.take()).intValue());
390 >                    assertEquals(i, q.take());
391                  }
392 <                q.take();
393 <            }};
392 >                try {
393 >                    q.take();
394 >                    shouldThrow();
395 >                } catch (InterruptedException success) {}
396 >            }});
397  
398          t.start();
399          Thread.sleep(SHORT_DELAY_MS);
# Line 400 | Line 401 | public class LinkedBlockingQueueTest ext
401          t.join();
402      }
403  
403
404      /**
405       * poll succeeds unless empty
406       */
407      public void testPoll() {
408          LinkedBlockingQueue q = populatedQueue(SIZE);
409          for (int i = 0; i < SIZE; ++i) {
410 <            assertEquals(i, ((Integer)q.poll()).intValue());
410 >            assertEquals(i, q.poll());
411          }
412          assertNull(q.poll());
413      }
# Line 418 | Line 418 | public class LinkedBlockingQueueTest ext
418      public void testTimedPoll0() throws InterruptedException {
419          LinkedBlockingQueue q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
421 >            assertEquals(i, q.poll(0, MILLISECONDS));
422          }
423          assertNull(q.poll(0, MILLISECONDS));
424      }
# Line 429 | Line 429 | public class LinkedBlockingQueueTest ext
429      public void testTimedPoll() throws InterruptedException {
430          LinkedBlockingQueue q = populatedQueue(SIZE);
431          for (int i = 0; i < SIZE; ++i) {
432 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
432 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
433          }
434          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
435      }
# Line 439 | Line 439 | public class LinkedBlockingQueueTest ext
439       * returning timeout status
440       */
441      public void testInterruptedTimedPoll() throws InterruptedException {
442 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
442 >        Thread t = new Thread(new CheckedRunnable() {
443              public void realRun() throws InterruptedException {
444                  LinkedBlockingQueue q = populatedQueue(SIZE);
445                  for (int i = 0; i < SIZE; ++i) {
446 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
446 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
447                  }
448 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
449 <            }};
448 >                try {
449 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
450 >                    shouldThrow();
451 >                } catch (InterruptedException success) {}
452 >            }});
453  
454          t.start();
455          Thread.sleep(SHORT_DELAY_MS);
# Line 460 | Line 463 | public class LinkedBlockingQueueTest ext
463       */
464      public void testTimedPollWithOffer() throws InterruptedException {
465          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
466 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
466 >        Thread t = new Thread(new CheckedRunnable() {
467              public void realRun() throws InterruptedException {
468 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
469 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
470 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
471 <            }};
468 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
469 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
470 >                try {
471 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
472 >                    shouldThrow();
473 >                } catch (InterruptedException success) {}
474 >            }});
475  
476          t.start();
477          Thread.sleep(SMALL_DELAY_MS);
# Line 480 | Line 486 | public class LinkedBlockingQueueTest ext
486      public void testPeek() {
487          LinkedBlockingQueue q = populatedQueue(SIZE);
488          for (int i = 0; i < SIZE; ++i) {
489 <            assertEquals(i, ((Integer)q.peek()).intValue());
490 <            q.poll();
489 >            assertEquals(i, q.peek());
490 >            assertEquals(i, q.poll());
491              assertTrue(q.peek() == null ||
492 <                       i != ((Integer)q.peek()).intValue());
492 >                       !q.peek().equals(i));
493          }
494          assertNull(q.peek());
495      }
# Line 494 | Line 500 | public class LinkedBlockingQueueTest ext
500      public void testElement() {
501          LinkedBlockingQueue q = populatedQueue(SIZE);
502          for (int i = 0; i < SIZE; ++i) {
503 <            assertEquals(i, ((Integer)q.element()).intValue());
504 <            q.poll();
503 >            assertEquals(i, q.element());
504 >            assertEquals(i, q.poll());
505          }
506          try {
507              q.element();
# Line 509 | Line 515 | public class LinkedBlockingQueueTest ext
515      public void testRemove() {
516          LinkedBlockingQueue q = populatedQueue(SIZE);
517          for (int i = 0; i < SIZE; ++i) {
518 <            assertEquals(i, ((Integer)q.remove()).intValue());
518 >            assertEquals(i, q.remove());
519          }
520          try {
521              q.remove();
# Line 647 | Line 653 | public class LinkedBlockingQueueTest ext
653       * toArray(null) throws NPE
654       */
655      public void testToArray_BadArg() {
656 +        LinkedBlockingQueue q = populatedQueue(SIZE);
657          try {
651            LinkedBlockingQueue q = populatedQueue(SIZE);
658              Object o[] = q.toArray(null);
659              shouldThrow();
660          } catch (NullPointerException success) {}
# Line 658 | Line 664 | public class LinkedBlockingQueueTest ext
664       * toArray with incompatible array type throws CCE
665       */
666      public void testToArray1_BadArg() {
667 +        LinkedBlockingQueue q = populatedQueue(SIZE);
668          try {
669 <            LinkedBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
669 >            Object o[] = q.toArray(new String[10]);
670              shouldThrow();
671          } catch (ArrayStoreException success) {}
672      }
# Line 708 | Line 714 | public class LinkedBlockingQueueTest ext
714          assertEquals(0, q.remainingCapacity());
715          int k = 0;
716          for (Iterator it = q.iterator(); it.hasNext();) {
717 <            int i = ((Integer)(it.next())).intValue();
712 <            assertEquals(++k, i);
717 >            assertEquals(++k, it.next());
718          }
719          assertEquals(3, k);
720      }
# Line 752 | Line 757 | public class LinkedBlockingQueueTest ext
757          ExecutorService executor = Executors.newFixedThreadPool(2);
758          executor.execute(new CheckedRunnable() {
759              public void realRun() throws InterruptedException {
760 <                threadAssertFalse(q.offer(three));
761 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
762 <                threadAssertEquals(0, q.remainingCapacity());
760 >                assertFalse(q.offer(three));
761 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
762 >                assertEquals(0, q.remainingCapacity());
763              }});
764  
765          executor.execute(new CheckedRunnable() {
766              public void realRun() throws InterruptedException {
767                  Thread.sleep(SMALL_DELAY_MS);
768 <                threadAssertEquals(one, q.take());
768 >                assertSame(one, q.take());
769              }});
770  
771          joinPool(executor);
# Line 774 | Line 779 | public class LinkedBlockingQueueTest ext
779          ExecutorService executor = Executors.newFixedThreadPool(2);
780          executor.execute(new CheckedRunnable() {
781              public void realRun() throws InterruptedException {
782 <                threadAssertNull(q.poll());
783 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
784 <                threadAssertTrue(q.isEmpty());
782 >                assertNull(q.poll());
783 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
784 >                assertTrue(q.isEmpty());
785              }});
786  
787          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines