[cvs] / jsr166 / src / test / tck / ArrayBlockingQueueTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/ArrayBlockingQueueTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.26, Sun Nov 22 18:57:16 2009 UTC revision 1.37, Thu Oct 28 22:20:47 2010 UTC
# Line 14  Line 14 
14  import java.io.*;  import java.io.*;
15    
16  public class ArrayBlockingQueueTest extends JSR166TestCase {  public class ArrayBlockingQueueTest extends JSR166TestCase {
17    
18        public static class Fair extends BlockingQueueTest {
19            protected BlockingQueue emptyCollection() {
20                return new ArrayBlockingQueue(20, true);
21            }
22        }
23    
24        public static class NonFair extends BlockingQueueTest {
25            protected BlockingQueue emptyCollection() {
26                return new ArrayBlockingQueue(20, false);
27            }
28        }
29    
30      public static void main(String[] args) {      public static void main(String[] args) {
31          junit.textui.TestRunner.run (suite());          junit.textui.TestRunner.run (suite());
32      }      }
33    
34      public static Test suite() {      public static Test suite() {
35          return new TestSuite(ArrayBlockingQueueTest.class);          return newTestSuite(ArrayBlockingQueueTest.class,
36                                new Fair().testSuite(),
37                                new NonFair().testSuite());
38      }      }
39    
40      /**      /**
# Line 224  Line 240 
240              shouldThrow();              shouldThrow();
241          } catch (NullPointerException success) {}          } catch (NullPointerException success) {}
242      }      }
243    
244      /**      /**
245       * addAll of a collection with any null elements throws NPE after       * addAll of a collection with any null elements throws NPE after
246       * possibly adding some elements       * possibly adding some elements
# Line 238  Line 255 
255              shouldThrow();              shouldThrow();
256          } catch (NullPointerException success) {}          } catch (NullPointerException success) {}
257      }      }
258    
259      /**      /**
260       * addAll throws ISE if not enough room       * addAll throws ISE if not enough room
261       */       */
# Line 251  Line 269 
269              shouldThrow();              shouldThrow();
270          } catch (IllegalStateException success) {}          } catch (IllegalStateException success) {}
271      }      }
272    
273      /**      /**
274       * Queue contains all elements, in traversal order, of successful addAll       * Queue contains all elements, in traversal order, of successful addAll
275       */       */
# Line 319  Line 338 
338       * put blocks waiting for take when full       * put blocks waiting for take when full
339       */       */
340      public void testPutWithTake() throws InterruptedException {      public void testPutWithTake() throws InterruptedException {
341          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);          final int capacity = 2;
342            final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
343          Thread t = new Thread(new CheckedRunnable() {          Thread t = new Thread(new CheckedRunnable() {
344              public void realRun() {              public void realRun() throws InterruptedException {
345                  int added = 0;                  for (int i = 0; i < capacity + 1; i++)
346                        q.put(i);
347                  try {                  try {
348                      q.put(new Object());                      q.put(99);
349                      ++added;                      shouldThrow();
350                      q.put(new Object());                  } catch (InterruptedException success) {}
                     ++added;  
                     q.put(new Object());  
                     ++added;  
                     q.put(new Object());  
                     ++added;  
                     threadShouldThrow();  
                 } catch (InterruptedException success) {  
                     threadAssertTrue(added >= 2);  
                 }  
351              }});              }});
352    
353          t.start();          t.start();
354          Thread.sleep(SHORT_DELAY_MS);          Thread.sleep(SHORT_DELAY_MS);
355          q.take();          assertEquals(q.remainingCapacity(), 0);
356            assertEquals(0, q.take());
357            Thread.sleep(SHORT_DELAY_MS);
358          t.interrupt();          t.interrupt();
359          t.join();          t.join();
360            assertEquals(q.remainingCapacity(), 0);
361      }      }
362    
363      /**      /**
# Line 378  Line 393 
393      }      }
394    
395      /**      /**
      * take blocks interruptibly when empty  
      */  
     public void testTakeFromEmpty() throws InterruptedException {  
         final ArrayBlockingQueue q = new ArrayBlockingQueue(2);  
         Thread t = new ThreadShouldThrow(InterruptedException.class) {  
             public void realRun() throws InterruptedException {  
                 q.take();  
             }};  
   
         t.start();  
         Thread.sleep(SHORT_DELAY_MS);  
         t.interrupt();  
         t.join();  
     }  
   
     /**  
396       * Take removes existing elements until empty, then blocks interruptibly       * Take removes existing elements until empty, then blocks interruptibly
397       */       */
398      public void testBlockingTake() throws InterruptedException {      public void testBlockingTake() throws InterruptedException {
# Line 428  Line 427 
427      }      }
428    
429      /**      /**
430       * timed pool with zero timeout succeeds when non-empty, else times out       * timed poll with zero timeout succeeds when non-empty, else times out
431       */       */
432      public void testTimedPoll0() throws InterruptedException {      public void testTimedPoll0() throws InterruptedException {
433          ArrayBlockingQueue q = populatedQueue(SIZE);          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 439  Line 438 
438      }      }
439    
440      /**      /**
441       * timed pool with nonzero timeout succeeds when non-empty, else times out       * timed poll with nonzero timeout succeeds when non-empty, else times out
442       */       */
443      public void testTimedPoll() throws InterruptedException {      public void testTimedPoll() throws InterruptedException {
444          ArrayBlockingQueue q = populatedQueue(SIZE);          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 473  Line 472 
472      }      }
473    
474      /**      /**
      *  timed poll before a delayed offer fails; after offer succeeds;  
      *  on interruption throws  
      */  
     public void testTimedPollWithOffer() throws InterruptedException {  
         final ArrayBlockingQueue q = new ArrayBlockingQueue(2);  
         Thread t = new Thread(new CheckedRunnable() {  
             public void realRun() throws InterruptedException {  
                 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));  
                 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));  
                 try {  
                     q.poll(LONG_DELAY_MS, MILLISECONDS);  
                     shouldThrow();  
                 } catch (InterruptedException success) {}  
             }});  
   
         t.start();  
         Thread.sleep(SMALL_DELAY_MS);  
         assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));  
         t.interrupt();  
         t.join();  
     }  
   
   
     /**  
475       * peek returns next element, or null if empty       * peek returns next element, or null if empty
476       */       */
477      public void testPeek() {      public void testPeek() {
# Line 700  Line 675 
675          it.remove();          it.remove();
676    
677          it = q.iterator();          it = q.iterator();
678          assertEquals(it.next(), one);          assertSame(it.next(), one);
679          assertEquals(it.next(), three);          assertSame(it.next(), three);
680          assertFalse(it.hasNext());          assertFalse(it.hasNext());
681      }      }
682    
# Line 761  Line 736 
736          ExecutorService executor = Executors.newFixedThreadPool(2);          ExecutorService executor = Executors.newFixedThreadPool(2);
737          executor.execute(new CheckedRunnable() {          executor.execute(new CheckedRunnable() {
738              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
739                  threadAssertFalse(q.offer(three));                  assertFalse(q.offer(three));
740                  threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));                  assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
741                  threadAssertEquals(0, q.remainingCapacity());                  assertEquals(0, q.remainingCapacity());
742              }});              }});
743    
744          executor.execute(new CheckedRunnable() {          executor.execute(new CheckedRunnable() {
745              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
746                  Thread.sleep(SMALL_DELAY_MS);                  Thread.sleep(SMALL_DELAY_MS);
747                  threadAssertEquals(one, q.take());                  assertSame(one, q.take());
748              }});              }});
749    
750          joinPool(executor);          joinPool(executor);
   
751      }      }
752    
753      /**      /**
# Line 784  Line 758 
758          ExecutorService executor = Executors.newFixedThreadPool(2);          ExecutorService executor = Executors.newFixedThreadPool(2);
759          executor.execute(new CheckedRunnable() {          executor.execute(new CheckedRunnable() {
760              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
761                  threadAssertNull(q.poll());                  assertNull(q.poll());
762                  threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));                  assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
763                  threadAssertTrue(q.isEmpty());                  assertTrue(q.isEmpty());
764              }});              }});
765    
766          executor.execute(new CheckedRunnable() {          executor.execute(new CheckedRunnable() {
# Line 906  Line 880 
880      }      }
881    
882      /**      /**
883       * drainTo(c, n) empties first max {n, size} elements of queue into c       * drainTo(c, n) empties first min(n, size) elements of queue into c
884       */       */
885      public void testDrainToN() {      public void testDrainToN() {
886          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);

Legend:
Removed from v.1.26  
changed lines
  Added in v.1.37

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8