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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.36 by jsr166, Wed Nov 3 16:46:34 2010 UTC vs.
Revision 1.44 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 49 | Line 49 | public class PriorityBlockingQueueTest e
49       * Create a queue of given size containing consecutive
50       * Integers 0 ... n.
51       */
52 <    private PriorityBlockingQueue populatedQueue(int n) {
53 <        PriorityBlockingQueue q = new PriorityBlockingQueue(n);
52 >    private PriorityBlockingQueue<Integer> populatedQueue(int n) {
53 >        PriorityBlockingQueue<Integer> q =
54 >            new PriorityBlockingQueue<Integer>(n);
55          assertTrue(q.isEmpty());
56          for (int i = n-1; i >= 0; i-=2)
57              assertTrue(q.offer(new Integer(i)));
# Line 295 | Line 296 | public class PriorityBlockingQueueTest e
296      /**
297       * put(null) throws NPE
298       */
299 <     public void testPutNull() {
299 >    public void testPutNull() {
300          try {
301              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
302              q.put(null);
303              shouldThrow();
304          } catch (NullPointerException success) {}
305 <     }
305 >    }
306  
307      /**
308       * all elements successfully put are contained
309       */
310 <     public void testPut() {
311 <         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
312 <         for (int i = 0; i < SIZE; ++i) {
313 <             Integer I = new Integer(i);
314 <             q.put(I);
315 <             assertTrue(q.contains(I));
316 <         }
317 <         assertEquals(SIZE, q.size());
310 >    public void testPut() {
311 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
312 >        for (int i = 0; i < SIZE; ++i) {
313 >            Integer I = new Integer(i);
314 >            q.put(I);
315 >            assertTrue(q.contains(I));
316 >        }
317 >        assertEquals(SIZE, q.size());
318      }
319  
320      /**
# Line 322 | Line 323 | public class PriorityBlockingQueueTest e
323      public void testPutWithTake() throws InterruptedException {
324          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
325          final int size = 4;
326 <        Thread t = new Thread(new CheckedRunnable() {
326 >        Thread t = newStartedThread(new CheckedRunnable() {
327              public void realRun() {
328                  for (int i = 0; i < size; i++)
329                      q.put(new Integer(0));
330              }});
331  
332 <        t.start();
333 <        Thread.sleep(SHORT_DELAY_MS);
333 <        assertEquals(q.size(), size);
332 >        awaitTermination(t);
333 >        assertEquals(size, q.size());
334          q.take();
335        t.interrupt();
336        t.join();
335      }
336  
337      /**
# Line 341 | Line 339 | public class PriorityBlockingQueueTest e
339       */
340      public void testTimedOffer() throws InterruptedException {
341          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
342 <        Thread t = new Thread(new CheckedRunnable() {
342 >        Thread t = newStartedThread(new CheckedRunnable() {
343              public void realRun() {
344                  q.put(new Integer(0));
345                  q.put(new Integer(0));
# Line 349 | Line 347 | public class PriorityBlockingQueueTest e
347                  assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
348              }});
349  
350 <        t.start();
353 <        Thread.sleep(SMALL_DELAY_MS);
354 <        t.interrupt();
355 <        t.join();
350 >        awaitTermination(t);
351      }
352  
353      /**
# Line 370 | Line 365 | public class PriorityBlockingQueueTest e
365       */
366      public void testBlockingTake() throws InterruptedException {
367          final PriorityBlockingQueue q = populatedQueue(SIZE);
368 <        Thread t = new Thread(new CheckedRunnable() {
368 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 >        Thread t = newStartedThread(new CheckedRunnable() {
370              public void realRun() throws InterruptedException {
371                  for (int i = 0; i < SIZE; ++i) {
372                      assertEquals(i, q.take());
373                  }
374 +
375 +                Thread.currentThread().interrupt();
376                  try {
377                      q.take();
378                      shouldThrow();
379                  } catch (InterruptedException success) {}
380 +                assertFalse(Thread.interrupted());
381 +
382 +                pleaseInterrupt.countDown();
383 +                try {
384 +                    q.take();
385 +                    shouldThrow();
386 +                } catch (InterruptedException success) {}
387 +                assertFalse(Thread.interrupted());
388              }});
389  
390 <        t.start();
391 <        Thread.sleep(SHORT_DELAY_MS);
390 >        await(pleaseInterrupt);
391 >        assertThreadStaysAlive(t);
392          t.interrupt();
393 <        t.join();
393 >        awaitTermination(t);
394      }
395  
390
396      /**
397       * poll succeeds unless empty
398       */
# Line 414 | Line 419 | public class PriorityBlockingQueueTest e
419       * timed poll with nonzero timeout succeeds when non-empty, else times out
420       */
421      public void testTimedPoll() throws InterruptedException {
422 <        PriorityBlockingQueue q = populatedQueue(SIZE);
422 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
425 <        }
426 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
424 >            long startTime = System.nanoTime();
425 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
426 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
427 >        }
428 >        long startTime = System.nanoTime();
429 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
430 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
431 >        checkEmpty(q);
432      }
433  
434      /**
# Line 426 | Line 436 | public class PriorityBlockingQueueTest e
436       * returning timeout status
437       */
438      public void testInterruptedTimedPoll() throws InterruptedException {
439 <        Thread t = new Thread(new CheckedRunnable() {
439 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
440 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
441 >        Thread t = newStartedThread(new CheckedRunnable() {
442              public void realRun() throws InterruptedException {
431                PriorityBlockingQueue q = populatedQueue(SIZE);
443                  for (int i = 0; i < SIZE; ++i) {
444 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
444 >                    long t0 = System.nanoTime();
445 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
446 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
447                  }
448 +                long t0 = System.nanoTime();
449 +                aboutToWait.countDown();
450                  try {
451 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
451 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
452                      shouldThrow();
453 <                } catch (InterruptedException success) {}
453 >                } catch (InterruptedException success) {
454 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
455 >                }
456              }});
457  
458 <        t.start();
459 <        Thread.sleep(SHORT_DELAY_MS);
458 >        aboutToWait.await();
459 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
460          t.interrupt();
461 <        t.join();
461 >        awaitTermination(t, MEDIUM_DELAY_MS);
462      }
463  
464      /**
# Line 493 | Line 510 | public class PriorityBlockingQueueTest e
510      public void testRemoveElement() {
511          PriorityBlockingQueue q = populatedQueue(SIZE);
512          for (int i = 1; i < SIZE; i+=2) {
513 <            assertTrue(q.remove(new Integer(i)));
513 >            assertTrue(q.contains(i));
514 >            assertTrue(q.remove(i));
515 >            assertFalse(q.contains(i));
516 >            assertTrue(q.contains(i-1));
517          }
518          for (int i = 0; i < SIZE; i+=2) {
519 <            assertTrue(q.remove(new Integer(i)));
520 <            assertFalse(q.remove(new Integer(i+1)));
519 >            assertTrue(q.contains(i));
520 >            assertTrue(q.remove(i));
521 >            assertFalse(q.contains(i));
522 >            assertFalse(q.remove(i+1));
523 >            assertFalse(q.contains(i+1));
524          }
525          assertTrue(q.isEmpty());
526      }
# Line 586 | Line 609 | public class PriorityBlockingQueueTest e
609          Object[] o = q.toArray();
610          Arrays.sort(o);
611          for (int i = 0; i < o.length; i++)
612 <            assertEquals(o[i], q.take());
612 >            assertSame(o[i], q.take());
613      }
614  
615      /**
616       * toArray(a) contains all elements
617       */
618      public void testToArray2() throws InterruptedException {
619 <        PriorityBlockingQueue q = populatedQueue(SIZE);
619 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
620          Integer[] ints = new Integer[SIZE];
621 <        ints = (Integer[])q.toArray(ints);
621 >        Integer[] array = q.toArray(ints);
622 >        assertSame(ints, array);
623          Arrays.sort(ints);
624          for (int i = 0; i < ints.length; i++)
625 <            assertEquals(ints[i], q.take());
625 >            assertSame(ints[i], q.take());
626      }
627  
628      /**
# Line 656 | Line 680 | public class PriorityBlockingQueueTest e
680          assertFalse(it.hasNext());
681      }
682  
659
683      /**
684       * toString contains toStrings of elements
685       */
# Line 664 | Line 687 | public class PriorityBlockingQueueTest e
687          PriorityBlockingQueue q = populatedQueue(SIZE);
688          String s = q.toString();
689          for (int i = 0; i < SIZE; ++i) {
690 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
690 >            assertTrue(s.contains(String.valueOf(i)));
691          }
692      }
693  
694      /**
695 <     * offer transfers elements across Executor tasks
695 >     * timed poll transfers elements across Executor tasks
696       */
697      public void testPollInExecutor() {
698          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
699 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
700          ExecutorService executor = Executors.newFixedThreadPool(2);
701          executor.execute(new CheckedRunnable() {
702              public void realRun() throws InterruptedException {
703                  assertNull(q.poll());
704 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705 <                assertTrue(q.isEmpty());
704 >                threadsStarted.await();
705 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
706 >                checkEmpty(q);
707              }});
708  
709          executor.execute(new CheckedRunnable() {
710              public void realRun() throws InterruptedException {
711 <                Thread.sleep(SMALL_DELAY_MS);
711 >                threadsStarted.await();
712                  q.put(one);
713              }});
714  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines