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

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

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

revision 1.6, Fri May 6 11:22:07 2011 UTC revision 1.7, Fri May 27 19:32:04 2011 UTC
# Line 37  Line 37 
37      protected abstract BlockingQueue emptyCollection();      protected abstract BlockingQueue emptyCollection();
38    
39      /**      /**
40       * timed poll before a delayed offer fails; after offer succeeds;       * timed poll before a delayed offer times out; after offer succeeds;
41       * on interruption throws       * on interruption throws
42       */       */
43      public void testTimedPollWithOffer() throws InterruptedException {      public void testTimedPollWithOffer() throws InterruptedException {
# Line 45  Line 45 
45          final CheckedBarrier barrier = new CheckedBarrier(2);          final CheckedBarrier barrier = new CheckedBarrier(2);
46          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
47              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
48                  assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));                  long startTime = System.nanoTime();
49                    assertNull(q.poll(timeoutMillis(), MILLISECONDS));
50                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
51    
52                  barrier.await();                  barrier.await();
53                  assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));  
54                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
55    
56                  Thread.currentThread().interrupt();                  Thread.currentThread().interrupt();
57                  try {                  try {
58                      q.poll(SHORT_DELAY_MS, MILLISECONDS);                      q.poll(LONG_DELAY_MS, MILLISECONDS);
59                      shouldThrow();                      shouldThrow();
60                  } catch (InterruptedException success) {}                  } catch (InterruptedException success) {}
61                    assertFalse(Thread.interrupted());
62    
63                  barrier.await();                  barrier.await();
64                  try {                  try {
65                      q.poll(MEDIUM_DELAY_MS, MILLISECONDS);                      q.poll(LONG_DELAY_MS, MILLISECONDS);
66                      shouldThrow();                      shouldThrow();
67                  } catch (InterruptedException success) {}                  } catch (InterruptedException success) {}
68                    assertFalse(Thread.interrupted());
69              }});              }});
70    
71          barrier.await();          barrier.await();
72          assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));          long startTime = System.nanoTime();
73            assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
74            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
75    
76          barrier.await();          barrier.await();
77          sleep(SHORT_DELAY_MS);          assertThreadStaysAlive(t);
78          t.interrupt();          t.interrupt();
79          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t);
80      }      }
81    
82      /**      /**
83       * take() blocks interruptibly when empty       * take() blocks interruptibly when empty
84       */       */
85      public void testTakeFromEmptyBlocksInterruptibly()      public void testTakeFromEmptyBlocksInterruptibly() {
             throws InterruptedException {  
86          final BlockingQueue q = emptyCollection();          final BlockingQueue q = emptyCollection();
87          final CountDownLatch threadStarted = new CountDownLatch(1);          final CountDownLatch threadStarted = new CountDownLatch(1);
88          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
89              public void realRun() {              public void realRun() {
                 long t0 = System.nanoTime();  
90                  threadStarted.countDown();                  threadStarted.countDown();
91                  try {                  try {
92                      q.take();                      q.take();
93                      shouldThrow();                      shouldThrow();
94                  } catch (InterruptedException success) {}                  } catch (InterruptedException success) {}
95                  assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);                  assertFalse(Thread.interrupted());
96              }});              }});
97    
98          threadStarted.await();          await(threadStarted);
99          delay(SHORT_DELAY_MS);          assertThreadStaysAlive(t);
         assertTrue(t.isAlive());  
100          t.interrupt();          t.interrupt();
101          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t);
102      }      }
103    
104      /**      /**
105       * take() throws InterruptedException immediately if interrupted       * take() throws InterruptedException immediately if interrupted
106       * before waiting       * before waiting
107       */       */
108      public void testTakeFromEmptyAfterInterrupt()      public void testTakeFromEmptyAfterInterrupt() {
             throws InterruptedException {  
109          final BlockingQueue q = emptyCollection();          final BlockingQueue q = emptyCollection();
110          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
111              public void realRun() {              public void realRun() {
                 long t0 = System.nanoTime();  
112                  Thread.currentThread().interrupt();                  Thread.currentThread().interrupt();
113                  try {                  try {
114                      q.take();                      q.take();
115                      shouldThrow();                      shouldThrow();
116                  } catch (InterruptedException success) {}                  } catch (InterruptedException success) {}
117                  assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);                  assertFalse(Thread.interrupted());
118                }});
119    
120            awaitTermination(t);
121        }
122    
123        /**
124         * timed poll() blocks interruptibly when empty
125         */
126        public void testTimedPollFromEmptyBlocksInterruptibly() {
127            final BlockingQueue q = emptyCollection();
128            final CountDownLatch threadStarted = new CountDownLatch(1);
129            Thread t = newStartedThread(new CheckedRunnable() {
130                public void realRun() {
131                    threadStarted.countDown();
132                    try {
133                        q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
134                        shouldThrow();
135                    } catch (InterruptedException success) {}
136                    assertFalse(Thread.interrupted());
137                }});
138    
139            await(threadStarted);
140            assertThreadStaysAlive(t);
141            t.interrupt();
142            awaitTermination(t);
143        }
144    
145        /**
146         * timed poll() throws InterruptedException immediately if
147         * interrupted before waiting
148         */
149        public void testTimedPollFromEmptyAfterInterrupt() {
150            final BlockingQueue q = emptyCollection();
151            Thread t = newStartedThread(new CheckedRunnable() {
152                public void realRun() {
153                    Thread.currentThread().interrupt();
154                    try {
155                        q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
156                        shouldThrow();
157                    } catch (InterruptedException success) {}
158                    assertFalse(Thread.interrupted());
159              }});              }});
160    
161          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t);
162      }      }
163    
164      /** For debugging. */      /** For debugging. */

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8