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.15 by jsr166, Sat Nov 21 19:11:53 2009 UTC vs.
Revision 1.29 by jsr166, Thu Nov 18 20:18:43 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      /**
40 <     * A SynchronousQueue is both empty and full
40 >     * Any SynchronousQueue is both empty and full
41       */
42 <    public void testEmptyFull() {
29 <        SynchronousQueue q = new SynchronousQueue();
42 >    public void testEmptyFull(SynchronousQueue q) {
43          assertTrue(q.isEmpty());
44          assertEquals(0, q.size());
45          assertEquals(0, q.remainingCapacity());
# Line 34 | Line 47 | public class SynchronousQueueTest extend
47      }
48  
49      /**
50 +     * A non-fair SynchronousQueue is both empty and full
51 +     */
52 +    public void testEmptyFull() {
53 +        testEmptyFull(new SynchronousQueue());
54 +    }
55 +
56 +    /**
57       * A fair SynchronousQueue is both empty and full
58       */
59      public void testFairEmptyFull() {
60 <        SynchronousQueue q = new SynchronousQueue(true);
41 <        assertTrue(q.isEmpty());
42 <        assertEquals(0, q.size());
43 <        assertEquals(0, q.remainingCapacity());
44 <        assertFalse(q.offer(zero));
60 >        testEmptyFull(new SynchronousQueue(true));
61      }
62  
63      /**
# Line 119 | Line 135 | public class SynchronousQueueTest extend
135              shouldThrow();
136          } catch (NullPointerException success) {}
137      }
138 +
139      /**
140       * addAll throws ISE if no active taker
141       */
# Line 169 | Line 186 | public class SynchronousQueueTest extend
186              public void realRun() throws InterruptedException {
187                  int added = 0;
188                  try {
189 <                    q.put(new Object());
190 <                    ++added;
191 <                    q.put(new Object());
192 <                    ++added;
176 <                    q.put(new Object());
177 <                    ++added;
178 <                    q.put(new Object());
179 <                    ++added;
180 <                    threadShouldThrow();
189 >                    while (true) {
190 >                        q.put(added);
191 >                        ++added;
192 >                    }
193                  } catch (InterruptedException success) {
194 <                    assertTrue(added >= 1);
194 >                    assertEquals(1, added);
195                  }
196              }});
197  
198          t.start();
199          Thread.sleep(SHORT_DELAY_MS);
200 <        q.take();
200 >        assertEquals(0, q.take());
201          Thread.sleep(SHORT_DELAY_MS);
202          t.interrupt();
203          t.join();
# Line 194 | Line 206 | public class SynchronousQueueTest extend
206      /**
207       * timed offer times out if elements not taken
208       */
209 <    public void testTimedOffer() throws InterruptedException {
210 <        final SynchronousQueue q = new SynchronousQueue();
211 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
212 <            public void realRun() throws InterruptedException {
213 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
214 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
209 >    public void testTimedOffer(final SynchronousQueue q)
210 >            throws InterruptedException {
211 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
212 >        Thread t = newStartedThread(new CheckedRunnable() {
213 >            public void realRun() throws InterruptedException {
214 >                long t0 = System.nanoTime();
215 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
216 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
217 >                pleaseInterrupt.countDown();
218 >                t0 = System.nanoTime();
219 >                try {
220 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
221 >                    shouldThrow();
222 >                } catch (InterruptedException success) {}
223 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
224              }});
225  
226 <        t.start();
206 <        Thread.sleep(SMALL_DELAY_MS);
226 >        assertTrue(pleaseInterrupt.await(MEDIUM_DELAY_MS, MILLISECONDS));
227          t.interrupt();
228 <        t.join();
228 >        awaitTermination(t, MEDIUM_DELAY_MS);
229      }
230  
211
231      /**
232 <     * take blocks interruptibly when empty
232 >     * timed offer times out if elements not taken
233       */
234 <    public void testTakeFromEmpty() throws InterruptedException {
235 <        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();
234 >    public void testTimedOffer() throws InterruptedException {
235 >        testTimedOffer(new SynchronousQueue());
236      }
237  
238 +    /**
239 +     * timed offer times out if elements not taken
240 +     */
241 +    public void testFairTimedOffer() throws InterruptedException {
242 +        testTimedOffer(new SynchronousQueue(true));
243 +    }
244  
245      /**
246       * put blocks interruptibly if no active taker
# Line 251 | Line 267 | public class SynchronousQueueTest extend
267              public void realRun() throws InterruptedException {
268                  int added = 0;
269                  try {
270 <                    q.put(new Object());
271 <                    ++added;
272 <                    q.put(new Object());
273 <                    ++added;
258 <                    q.put(new Object());
259 <                    ++added;
260 <                    q.put(new Object());
261 <                    ++added;
262 <                    threadShouldThrow();
270 >                    while (true) {
271 >                        q.put(added);
272 >                        ++added;
273 >                    }
274                  } catch (InterruptedException success) {
275 <                    assertTrue(added >= 1);
275 >                    assertEquals(1, added);
276                  }
277              }});
278  
279          t.start();
280          Thread.sleep(SHORT_DELAY_MS);
281 <        q.take();
281 >        assertEquals(0, q.take());
282          Thread.sleep(SHORT_DELAY_MS);
283          t.interrupt();
284          t.join();
285      }
286  
287      /**
277     * timed offer times out if elements not taken
278     */
279    public void testFairTimedOffer() throws InterruptedException {
280        final SynchronousQueue q = new SynchronousQueue(true);
281        Thread t = new Thread(new CheckedInterruptedRunnable() {
282            public void realRun() throws InterruptedException {
283                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
284                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
285            }});
286
287        t.start();
288        Thread.sleep(SMALL_DELAY_MS);
289        t.interrupt();
290        t.join();
291    }
292
293
294    /**
288       * take blocks interruptibly when empty
289       */
290      public void testFairTakeFromEmpty() throws InterruptedException {
# Line 308 | Line 301 | public class SynchronousQueueTest extend
301      }
302  
303      /**
304 <     * poll fails unless active taker
304 >     * poll return null if no active putter
305       */
306      public void testPoll() {
307          SynchronousQueue q = new SynchronousQueue();
# Line 316 | Line 309 | public class SynchronousQueueTest extend
309      }
310  
311      /**
312 <     * timed pool with zero timeout times out if no active taker
312 >     * timed poll with zero timeout times out if no active putter
313       */
314      public void testTimedPoll0() throws InterruptedException {
315          SynchronousQueue q = new SynchronousQueue();
# Line 324 | Line 317 | public class SynchronousQueueTest extend
317      }
318  
319      /**
320 <     * timed pool with nonzero timeout times out if no active taker
320 >     * timed poll with nonzero timeout times out if no active putter
321       */
322      public void testTimedPoll() throws InterruptedException {
323          SynchronousQueue q = new SynchronousQueue();
324 +        long t0 = System.nanoTime();
325          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
326 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
327      }
328  
329      /**
330       * Interrupted timed poll throws InterruptedException instead of
331       * returning timeout status
332       */
333 <    public void testInterruptedTimedPoll() throws InterruptedException {
334 <        final SynchronousQueue q = new SynchronousQueue();
335 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
333 >    public void testInterruptedTimedPoll(final SynchronousQueue q)
334 >            throws InterruptedException {
335 >        final CountDownLatch threadStarted = new CountDownLatch(1);
336 >        Thread t = newStartedThread(new CheckedRunnable() {
337              public void realRun() throws InterruptedException {
338 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
338 >                long t0 = System.nanoTime();
339 >                threadStarted.countDown();
340 >                try {
341 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
342 >                    shouldThrow();
343 >                } catch (InterruptedException success) {}
344 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
345 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
346              }});
347  
348 <        t.start();
348 >        threadStarted.await();
349          Thread.sleep(SHORT_DELAY_MS);
350          t.interrupt();
351 <        t.join();
351 >        awaitTermination(t, MEDIUM_DELAY_MS);
352      }
353  
354      /**
355 <     *  timed poll before a delayed offer fails; after offer succeeds;
356 <     *  on interruption throws
355 >     * Interrupted timed poll throws InterruptedException instead of
356 >     * returning timeout status
357       */
358 <    public void testTimedPollWithOffer() throws InterruptedException {
359 <        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();
358 >    public void testInterruptedTimedPoll() throws InterruptedException {
359 >        testInterruptedTimedPoll(new SynchronousQueue());
360      }
361  
362      /**
# Line 376 | Line 364 | public class SynchronousQueueTest extend
364       * returning timeout status
365       */
366      public void testFairInterruptedTimedPoll() throws InterruptedException {
367 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
380 <            public void realRun() throws InterruptedException {
381 <                SynchronousQueue q = new SynchronousQueue(true);
382 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
383 <            }});
384 <
385 <        t.start();
386 <        Thread.sleep(SHORT_DELAY_MS);
387 <        t.interrupt();
388 <        t.join();
367 >        testInterruptedTimedPoll(new SynchronousQueue(true));
368      }
369  
370      /**
371 <     *  timed poll before a delayed offer fails; after offer succeeds;
372 <     *  on interruption throws
371 >     * timed poll before a delayed offer times out, returning null;
372 >     * after offer succeeds; on interruption throws
373       */
374      public void testFairTimedPollWithOffer() throws InterruptedException {
375          final SynchronousQueue q = new SynchronousQueue(true);
376 <        Thread t = new Thread(new CheckedRunnable() {
376 >        final CountDownLatch pleaseOffer = new CountDownLatch(1);
377 >        Thread t = newStartedThread(new CheckedRunnable() {
378              public void realRun() throws InterruptedException {
379 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
379 >                long t0 = System.nanoTime();
380 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
381 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
382 >
383 >                pleaseOffer.countDown();
384 >                t0 = System.nanoTime();
385                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
386 +                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
387 +
388 +                t0 = System.nanoTime();
389                  try {
390                      q.poll(LONG_DELAY_MS, MILLISECONDS);
391 <                    threadShouldThrow();
391 >                    shouldThrow();
392                  } catch (InterruptedException success) {}
393 +                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
394              }});
395  
396 <        t.start();
397 <        Thread.sleep(SMALL_DELAY_MS);
398 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
396 >        assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS));
397 >        long t0 = System.nanoTime();
398 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
399 >        assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
400 >        
401          t.interrupt();
402 <        t.join();
402 >        awaitTermination(t, MEDIUM_DELAY_MS);
403      }
404  
414
405      /**
406 <     * peek returns null
406 >     * peek() returns null if no active putter
407       */
408      public void testPeek() {
409          SynchronousQueue q = new SynchronousQueue();
# Line 421 | Line 411 | public class SynchronousQueueTest extend
411      }
412  
413      /**
414 <     * element throws NSEE
414 >     * element() throws NSEE if no active putter
415       */
416      public void testElement() {
417          SynchronousQueue q = new SynchronousQueue();
# Line 432 | Line 422 | public class SynchronousQueueTest extend
422      }
423  
424      /**
425 <     * remove throws NSEE if no active taker
425 >     * remove() throws NSEE if no active putter
426       */
427      public void testRemove() {
428          SynchronousQueue q = new SynchronousQueue();
429          try {
430              q.remove();
431              shouldThrow();
432 <        } catch (NoSuchElementException success) {
443 <        }
432 >        } catch (NoSuchElementException success) {}
433      }
434  
435      /**
# Line 525 | Line 514 | public class SynchronousQueueTest extend
514       * toArray(null) throws NPE
515       */
516      public void testToArray_BadArg() {
517 +        SynchronousQueue q = new SynchronousQueue();
518          try {
529            SynchronousQueue q = new SynchronousQueue();
519              Object o[] = q.toArray(null);
520              shouldThrow();
521          } catch (NullPointerException success) {}
# Line 574 | Line 563 | public class SynchronousQueueTest extend
563      public void testOfferInExecutor() {
564          final SynchronousQueue q = new SynchronousQueue();
565          ExecutorService executor = Executors.newFixedThreadPool(2);
577        final Integer one = new Integer(1);
566  
567          executor.execute(new CheckedRunnable() {
568              public void realRun() throws InterruptedException {
569 <                threadAssertFalse(q.offer(one));
570 <                threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
571 <                threadAssertEquals(0, q.remainingCapacity());
569 >                assertFalse(q.offer(one));
570 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
571 >                assertEquals(0, q.remainingCapacity());
572              }});
573  
574          executor.execute(new CheckedRunnable() {
575              public void realRun() throws InterruptedException {
576                  Thread.sleep(SMALL_DELAY_MS);
577 <                threadAssertEquals(one, q.take());
577 >                assertSame(one, q.take());
578              }});
579  
580          joinPool(executor);
593
581      }
582  
583      /**
# Line 601 | Line 588 | public class SynchronousQueueTest extend
588          ExecutorService executor = Executors.newFixedThreadPool(2);
589          executor.execute(new CheckedRunnable() {
590              public void realRun() throws InterruptedException {
591 <                threadAssertNull(q.poll());
592 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
593 <                threadAssertTrue(q.isEmpty());
591 >                assertNull(q.poll());
592 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
593 >                assertTrue(q.isEmpty());
594              }});
595  
596          executor.execute(new CheckedRunnable() {
597              public void realRun() throws InterruptedException {
598 <                Thread.sleep(SMALL_DELAY_MS);
599 <                q.put(new Integer(1));
598 >                Thread.sleep(SHORT_DELAY_MS);
599 >                q.put(one);
600              }});
601  
602          joinPool(executor);
# Line 706 | Line 693 | public class SynchronousQueueTest extend
693          try {
694              q.drainTo(q, 0);
695              shouldThrow();
696 <        } catch (IllegalArgumentException success) {
710 <        }
696 >        } catch (IllegalArgumentException success) {}
697      }
698  
699      /**
# Line 730 | Line 716 | public class SynchronousQueueTest extend
716          ArrayList l = new ArrayList();
717          Thread.sleep(SHORT_DELAY_MS);
718          q.drainTo(l, 1);
719 <        assertTrue(l.size() == 1);
719 >        assertEquals(1, l.size());
720          q.drainTo(l, 1);
721 <        assertTrue(l.size() == 2);
721 >        assertEquals(2, l.size());
722          assertTrue(l.contains(one));
723          assertTrue(l.contains(two));
724          t1.join();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines