ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.18 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.26 by jsr166, Sat Oct 9 19:30:35 2010 UTC

# Line 14 | Line 14 | import java.io.*;
14  
15   public class SynchronousQueueTest extends JSR166TestCase {
16  
17 +    public static class Fair extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new SynchronousQueue(true);
20 +        }
21 +    }
22 +
23 +    public static class NonFair extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new SynchronousQueue(false);
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(SynchronousQueueTest.class);
34 >        return newTestSuite(SynchronousQueueTest.class,
35 >                            new Fair().testSuite(),
36 >                            new NonFair().testSuite());
37      }
38  
39      /**
# Line 119 | Line 133 | public class SynchronousQueueTest extend
133              shouldThrow();
134          } catch (NullPointerException success) {}
135      }
136 +
137      /**
138       * addAll throws ISE if no active taker
139       */
# Line 169 | Line 184 | public class SynchronousQueueTest extend
184              public void realRun() throws InterruptedException {
185                  int added = 0;
186                  try {
187 <                    q.put(new Object());
188 <                    ++added;
189 <                    q.put(new Object());
190 <                    ++added;
176 <                    q.put(new Object());
177 <                    ++added;
178 <                    q.put(new Object());
179 <                    ++added;
180 <                    threadShouldThrow();
187 >                    while (true) {
188 >                        q.put(added);
189 >                        ++added;
190 >                    }
191                  } catch (InterruptedException success) {
192 <                    assertTrue(added >= 1);
192 >                    assertEquals(1, added);
193                  }
194              }});
195  
196          t.start();
197          Thread.sleep(SHORT_DELAY_MS);
198 <        q.take();
198 >        assertEquals(0, q.take());
199          Thread.sleep(SHORT_DELAY_MS);
200          t.interrupt();
201          t.join();
# Line 251 | Line 261 | public class SynchronousQueueTest extend
261              public void realRun() throws InterruptedException {
262                  int added = 0;
263                  try {
264 <                    q.put(new Object());
265 <                    ++added;
266 <                    q.put(new Object());
267 <                    ++added;
258 <                    q.put(new Object());
259 <                    ++added;
260 <                    q.put(new Object());
261 <                    ++added;
262 <                    threadShouldThrow();
264 >                    while (true) {
265 >                        q.put(added);
266 >                        ++added;
267 >                    }
268                  } catch (InterruptedException success) {
269 <                    assertTrue(added >= 1);
269 >                    assertEquals(1, added);
270                  }
271              }});
272  
273          t.start();
274          Thread.sleep(SHORT_DELAY_MS);
275 <        q.take();
275 >        assertEquals(0, q.take());
276          Thread.sleep(SHORT_DELAY_MS);
277          t.interrupt();
278          t.join();
# Line 280 | Line 285 | public class SynchronousQueueTest extend
285          final SynchronousQueue q = new SynchronousQueue(true);
286          Thread t = new Thread(new CheckedInterruptedRunnable() {
287              public void realRun() throws InterruptedException {
288 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
288 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
289                  q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
290              }});
291  
# Line 349 | Line 354 | public class SynchronousQueueTest extend
354      }
355  
356      /**
352     *  timed poll before a delayed offer fails; after offer succeeds;
353     *  on interruption throws
354     */
355    public void testTimedPollWithOffer() throws InterruptedException {
356        final SynchronousQueue q = new SynchronousQueue();
357        Thread t = new Thread(new CheckedRunnable() {
358            public void realRun() throws InterruptedException {
359                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
360                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
361                try {
362                    q.poll(LONG_DELAY_MS, MILLISECONDS);
363                    shouldThrow();
364                } catch (InterruptedException success) {}
365            }});
366
367        t.start();
368        Thread.sleep(SMALL_DELAY_MS);
369        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
370        t.interrupt();
371        t.join();
372    }
373
374    /**
357       * Interrupted timed poll throws InterruptedException instead of
358       * returning timeout status
359       */
# Line 389 | Line 371 | public class SynchronousQueueTest extend
371      }
372  
373      /**
374 <     *  timed poll before a delayed offer fails; after offer succeeds;
375 <     *  on interruption throws
374 >     * timed poll before a delayed offer fails; after offer succeeds;
375 >     * on interruption throws
376       */
377      public void testFairTimedPollWithOffer() throws InterruptedException {
378          final SynchronousQueue q = new SynchronousQueue(true);
379          Thread t = new Thread(new CheckedRunnable() {
380              public void realRun() throws InterruptedException {
399                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
400                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
381                  try {
382 +                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
383 +                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
384                      q.poll(LONG_DELAY_MS, MILLISECONDS);
385                      threadShouldThrow();
386                  } catch (InterruptedException success) {}
# Line 573 | Line 555 | public class SynchronousQueueTest extend
555      public void testOfferInExecutor() {
556          final SynchronousQueue q = new SynchronousQueue();
557          ExecutorService executor = Executors.newFixedThreadPool(2);
576        final Integer one = new Integer(1);
558  
559          executor.execute(new CheckedRunnable() {
560              public void realRun() throws InterruptedException {
561 <                threadAssertFalse(q.offer(one));
562 <                threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
563 <                threadAssertEquals(0, q.remainingCapacity());
561 >                assertFalse(q.offer(one));
562 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
563 >                assertEquals(0, q.remainingCapacity());
564              }});
565  
566          executor.execute(new CheckedRunnable() {
567              public void realRun() throws InterruptedException {
568                  Thread.sleep(SMALL_DELAY_MS);
569 <                threadAssertEquals(one, q.take());
569 >                assertSame(one, q.take());
570              }});
571  
572          joinPool(executor);
592
573      }
574  
575      /**
# Line 600 | Line 580 | public class SynchronousQueueTest extend
580          ExecutorService executor = Executors.newFixedThreadPool(2);
581          executor.execute(new CheckedRunnable() {
582              public void realRun() throws InterruptedException {
583 <                threadAssertNull(q.poll());
584 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
585 <                threadAssertTrue(q.isEmpty());
583 >                assertNull(q.poll());
584 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
585 >                assertTrue(q.isEmpty());
586              }});
587  
588          executor.execute(new CheckedRunnable() {
589              public void realRun() throws InterruptedException {
590 <                Thread.sleep(SMALL_DELAY_MS);
591 <                q.put(new Integer(1));
590 >                Thread.sleep(SHORT_DELAY_MS);
591 >                q.put(one);
592              }});
593  
594          joinPool(executor);
# Line 728 | Line 708 | public class SynchronousQueueTest extend
708          ArrayList l = new ArrayList();
709          Thread.sleep(SHORT_DELAY_MS);
710          q.drainTo(l, 1);
711 <        assertTrue(l.size() == 1);
711 >        assertEquals(1, l.size());
712          q.drainTo(l, 1);
713 <        assertTrue(l.size() == 2);
713 >        assertEquals(2, l.size());
714          assertTrue(l.contains(one));
715          assertTrue(l.contains(two));
716          t1.join();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines