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.13 by jsr166, Thu Nov 19 01:15:42 2009 UTC vs.
Revision 1.27 by jsr166, Thu Oct 28 17:57:26 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 28 | Line 42 | public class SynchronousQueueTest extend
42      public void testEmptyFull() {
43          SynchronousQueue q = new SynchronousQueue();
44          assertTrue(q.isEmpty());
45 <        assertEquals(0, q.size());
45 >        assertEquals(0, q.size());
46          assertEquals(0, q.remainingCapacity());
47          assertFalse(q.offer(zero));
48      }
# Line 39 | Line 53 | public class SynchronousQueueTest extend
53      public void testFairEmptyFull() {
54          SynchronousQueue q = new SynchronousQueue(true);
55          assertTrue(q.isEmpty());
56 <        assertEquals(0, q.size());
56 >        assertEquals(0, q.size());
57          assertEquals(0, q.remainingCapacity());
58          assertFalse(q.offer(zero));
59      }
# Line 48 | Line 62 | public class SynchronousQueueTest extend
62       * offer(null) throws NPE
63       */
64      public void testOfferNull() {
65 <        try {
65 >        try {
66              SynchronousQueue q = new SynchronousQueue();
67              q.offer(null);
68              shouldThrow();
# Line 59 | Line 73 | public class SynchronousQueueTest extend
73       * add(null) throws NPE
74       */
75      public void testAddNull() {
76 <        try {
76 >        try {
77              SynchronousQueue q = new SynchronousQueue();
78              q.add(null);
79              shouldThrow();
# Line 78 | Line 92 | public class SynchronousQueueTest extend
92       * add throws ISE if no active taker
93       */
94      public void testAdd() {
95 <        try {
95 >        try {
96              SynchronousQueue q = new SynchronousQueue();
97              assertEquals(0, q.remainingCapacity());
98              q.add(one);
# 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 137 | Line 152 | public class SynchronousQueueTest extend
152       * put(null) throws NPE
153       */
154      public void testPutNull() throws InterruptedException {
155 <        try {
155 >        try {
156              SynchronousQueue q = new SynchronousQueue();
157              q.put(null);
158              shouldThrow();
# Line 148 | Line 163 | public class SynchronousQueueTest extend
163       * put blocks interruptibly if no active taker
164       */
165      public void testBlockingPut() throws InterruptedException {
166 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
167 <            public void realRun() throws InterruptedException {
166 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
167 >            public void realRun() throws InterruptedException {
168                  SynchronousQueue q = new SynchronousQueue();
169                  q.put(zero);
170              }});
# Line 165 | Line 180 | public class SynchronousQueueTest extend
180       */
181      public void testPutWithTake() throws InterruptedException {
182          final SynchronousQueue q = new SynchronousQueue();
183 <        Thread t = new Thread(new CheckedRunnable() {
184 <            public void realRun() throws InterruptedException {
183 >        Thread t = new Thread(new CheckedRunnable() {
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 196 | Line 206 | public class SynchronousQueueTest extend
206       */
207      public void testTimedOffer() throws InterruptedException {
208          final SynchronousQueue q = new SynchronousQueue();
209 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
210 <            public void realRun() throws InterruptedException {
211 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
209 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
210 >            public void realRun() throws InterruptedException {
211 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
212                  q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
213              }});
214  
# Line 208 | Line 218 | public class SynchronousQueueTest extend
218          t.join();
219      }
220  
211
212    /**
213     * take blocks interruptibly when empty
214     */
215    public void testTakeFromEmpty() throws InterruptedException {
216        final SynchronousQueue q = new SynchronousQueue();
217        Thread t = new Thread(new CheckedInterruptedRunnable() {
218            public void realRun() throws InterruptedException {
219                q.take();
220            }});
221
222        t.start();
223        Thread.sleep(SHORT_DELAY_MS);
224        t.interrupt();
225        t.join();
226    }
227
228
221      /**
222       * put blocks interruptibly if no active taker
223       */
224      public void testFairBlockingPut() throws InterruptedException {
225 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
226 <            public void realRun() throws InterruptedException {
225 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
226 >            public void realRun() throws InterruptedException {
227                  SynchronousQueue q = new SynchronousQueue(true);
228                  q.put(zero);
229              }});
# Line 247 | Line 239 | public class SynchronousQueueTest extend
239       */
240      public void testFairPutWithTake() throws InterruptedException {
241          final SynchronousQueue q = new SynchronousQueue(true);
242 <        Thread t = new Thread(new CheckedRunnable() {
243 <            public void realRun() throws InterruptedException {
242 >        Thread t = new Thread(new CheckedRunnable() {
243 >            public void realRun() throws InterruptedException {
244                  int added = 0;
245                  try {
246 <                    q.put(new Object());
247 <                    ++added;
248 <                    q.put(new Object());
249 <                    ++added;
258 <                    q.put(new Object());
259 <                    ++added;
260 <                    q.put(new Object());
261 <                    ++added;
262 <                    threadShouldThrow();
246 >                    while (true) {
247 >                        q.put(added);
248 >                        ++added;
249 >                    }
250                  } catch (InterruptedException success) {
251 <                    assertTrue(added >= 1);
251 >                    assertEquals(1, added);
252                  }
253              }});
254  
255          t.start();
256          Thread.sleep(SHORT_DELAY_MS);
257 <        q.take();
257 >        assertEquals(0, q.take());
258          Thread.sleep(SHORT_DELAY_MS);
259          t.interrupt();
260          t.join();
# Line 278 | Line 265 | public class SynchronousQueueTest extend
265       */
266      public void testFairTimedOffer() throws InterruptedException {
267          final SynchronousQueue q = new SynchronousQueue(true);
268 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
269 <            public void realRun() throws InterruptedException {
270 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
268 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
269 >            public void realRun() throws InterruptedException {
270 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
271                  q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
272              }});
273  
# Line 296 | Line 283 | public class SynchronousQueueTest extend
283       */
284      public void testFairTakeFromEmpty() throws InterruptedException {
285          final SynchronousQueue q = new SynchronousQueue(true);
286 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
287 <            public void realRun() throws InterruptedException {
286 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
287 >            public void realRun() throws InterruptedException {
288                  q.take();
289              }});
290  
# Line 312 | Line 299 | public class SynchronousQueueTest extend
299       */
300      public void testPoll() {
301          SynchronousQueue q = new SynchronousQueue();
302 <        assertNull(q.poll());
302 >        assertNull(q.poll());
303      }
304  
305      /**
# Line 336 | Line 323 | public class SynchronousQueueTest extend
323       * returning timeout status
324       */
325      public void testInterruptedTimedPoll() throws InterruptedException {
326 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
327 <            public void realRun() throws InterruptedException {
328 <                SynchronousQueue q = new SynchronousQueue();
326 >        final SynchronousQueue q = new SynchronousQueue();
327 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
328 >            public void realRun() throws InterruptedException {
329                  q.poll(SMALL_DELAY_MS, MILLISECONDS);
330              }});
331  
# Line 349 | Line 336 | public class SynchronousQueueTest extend
336      }
337  
338      /**
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                threadAssertNull(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                    threadShouldThrow();
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    /**
339       * Interrupted timed poll throws InterruptedException instead of
340       * returning timeout status
341       */
342      public void testFairInterruptedTimedPoll() throws InterruptedException {
343 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
344 <            public void realRun() throws InterruptedException {
343 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
344 >            public void realRun() throws InterruptedException {
345                  SynchronousQueue q = new SynchronousQueue(true);
346                  q.poll(SMALL_DELAY_MS, MILLISECONDS);
347              }});
# Line 389 | Line 353 | public class SynchronousQueueTest extend
353      }
354  
355      /**
356 <     *  timed poll before a delayed offer fails; after offer succeeds;
357 <     *  on interruption throws
356 >     * timed poll before a delayed offer fails; after offer succeeds;
357 >     * on interruption throws
358       */
359      public void testFairTimedPollWithOffer() throws InterruptedException {
360          final SynchronousQueue q = new SynchronousQueue(true);
361 <        Thread t = new Thread(new CheckedRunnable() {
362 <            public void realRun() throws InterruptedException {
399 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
400 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
361 >        Thread t = new Thread(new CheckedRunnable() {
362 >            public void realRun() throws InterruptedException {
363                  try {
364 +                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
365 +                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
366                      q.poll(LONG_DELAY_MS, MILLISECONDS);
367                      threadShouldThrow();
368                  } catch (InterruptedException success) {}
# Line 417 | Line 381 | public class SynchronousQueueTest extend
381       */
382      public void testPeek() {
383          SynchronousQueue q = new SynchronousQueue();
384 <        assertNull(q.peek());
384 >        assertNull(q.peek());
385      }
386  
387      /**
# Line 439 | Line 403 | public class SynchronousQueueTest extend
403          try {
404              q.remove();
405              shouldThrow();
406 <        } catch (NoSuchElementException success) {
443 <        }
406 >        } catch (NoSuchElementException success) {}
407      }
408  
409      /**
# Line 508 | Line 471 | public class SynchronousQueueTest extend
471       */
472      public void testToArray() {
473          SynchronousQueue q = new SynchronousQueue();
474 <        Object[] o = q.toArray();
474 >        Object[] o = q.toArray();
475          assertEquals(o.length, 0);
476      }
477  
# Line 517 | Line 480 | public class SynchronousQueueTest extend
480       */
481      public void testToArray2() {
482          SynchronousQueue q = new SynchronousQueue();
483 <        Integer[] ints = new Integer[1];
483 >        Integer[] ints = new Integer[1];
484          assertNull(ints[0]);
485      }
486  
# Line 525 | Line 488 | public class SynchronousQueueTest extend
488       * toArray(null) throws NPE
489       */
490      public void testToArray_BadArg() {
491 <        try {
492 <            SynchronousQueue q = new SynchronousQueue();
493 <            Object o[] = q.toArray(null);
494 <            shouldThrow();
495 <        } catch (NullPointerException success) {}
491 >        SynchronousQueue q = new SynchronousQueue();
492 >        try {
493 >            Object o[] = q.toArray(null);
494 >            shouldThrow();
495 >        } catch (NullPointerException success) {}
496      }
497  
498  
# Line 538 | Line 501 | public class SynchronousQueueTest extend
501       */
502      public void testIterator() {
503          SynchronousQueue q = new SynchronousQueue();
504 <        Iterator it = q.iterator();
504 >        Iterator it = q.iterator();
505          assertFalse(it.hasNext());
506          try {
507              Object x = it.next();
# Line 551 | Line 514 | public class SynchronousQueueTest extend
514       */
515      public void testIteratorRemove() {
516          SynchronousQueue q = new SynchronousQueue();
517 <        Iterator it = q.iterator();
517 >        Iterator it = q.iterator();
518          try {
519              it.remove();
520              shouldThrow();
# Line 574 | Line 537 | public class SynchronousQueueTest extend
537      public void testOfferInExecutor() {
538          final SynchronousQueue q = new SynchronousQueue();
539          ExecutorService executor = Executors.newFixedThreadPool(2);
577        final Integer one = new Integer(1);
540  
541          executor.execute(new CheckedRunnable() {
542 <            public void realRun() throws InterruptedException {
543 <                threadAssertFalse(q.offer(one));
544 <                threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
545 <                threadAssertEquals(0, q.remainingCapacity());
542 >            public void realRun() throws InterruptedException {
543 >                assertFalse(q.offer(one));
544 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
545 >                assertEquals(0, q.remainingCapacity());
546              }});
547  
548          executor.execute(new CheckedRunnable() {
549 <            public void realRun() throws InterruptedException {
549 >            public void realRun() throws InterruptedException {
550                  Thread.sleep(SMALL_DELAY_MS);
551 <                threadAssertEquals(one, q.take());
551 >                assertSame(one, q.take());
552              }});
553  
554          joinPool(executor);
593
555      }
556  
557      /**
# Line 600 | Line 561 | public class SynchronousQueueTest extend
561          final SynchronousQueue q = new SynchronousQueue();
562          ExecutorService executor = Executors.newFixedThreadPool(2);
563          executor.execute(new CheckedRunnable() {
564 <            public void realRun() throws InterruptedException {
565 <                threadAssertNull(q.poll());
566 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
567 <                threadAssertTrue(q.isEmpty());
564 >            public void realRun() throws InterruptedException {
565 >                assertNull(q.poll());
566 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
567 >                assertTrue(q.isEmpty());
568              }});
569  
570          executor.execute(new CheckedRunnable() {
571 <            public void realRun() throws InterruptedException {
572 <                Thread.sleep(SMALL_DELAY_MS);
573 <                q.put(new Integer(1));
571 >            public void realRun() throws InterruptedException {
572 >                Thread.sleep(SHORT_DELAY_MS);
573 >                q.put(one);
574              }});
575  
576          joinPool(executor);
# Line 671 | Line 632 | public class SynchronousQueueTest extend
632       */
633      public void testDrainToWithActivePut() throws InterruptedException {
634          final SynchronousQueue q = new SynchronousQueue();
635 <        Thread t = new Thread(new CheckedRunnable() {
636 <            public void realRun() throws InterruptedException {
635 >        Thread t = new Thread(new CheckedRunnable() {
636 >            public void realRun() throws InterruptedException {
637                  q.put(new Integer(1));
638              }});
639  
# Line 706 | Line 667 | public class SynchronousQueueTest extend
667          try {
668              q.drainTo(q, 0);
669              shouldThrow();
670 <        } catch (IllegalArgumentException success) {
710 <        }
670 >        } catch (IllegalArgumentException success) {}
671      }
672  
673      /**
# Line 715 | Line 675 | public class SynchronousQueueTest extend
675       */
676      public void testDrainToN() throws InterruptedException {
677          final SynchronousQueue q = new SynchronousQueue();
678 <        Thread t1 = new Thread(new CheckedRunnable() {
679 <            public void realRun() throws InterruptedException {
678 >        Thread t1 = new Thread(new CheckedRunnable() {
679 >            public void realRun() throws InterruptedException {
680                  q.put(one);
681              }});
682  
683 <        Thread t2 = new Thread(new CheckedRunnable() {
684 <            public void realRun() throws InterruptedException {
683 >        Thread t2 = new Thread(new CheckedRunnable() {
684 >            public void realRun() throws InterruptedException {
685                  q.put(two);
686              }});
687  
# Line 730 | Line 690 | public class SynchronousQueueTest extend
690          ArrayList l = new ArrayList();
691          Thread.sleep(SHORT_DELAY_MS);
692          q.drainTo(l, 1);
693 <        assertTrue(l.size() == 1);
693 >        assertEquals(1, l.size());
694          q.drainTo(l, 1);
695 <        assertTrue(l.size() == 2);
695 >        assertEquals(2, l.size());
696          assertTrue(l.contains(one));
697          assertTrue(l.contains(two));
698          t1.join();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines