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.39 by jsr166, Thu Nov 18 20:21:53 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 296 | 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 323 | 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);
334 <        assertEquals(q.size(), size);
332 >        awaitTermination(t);
333 >        assertEquals(size, q.size());
334          q.take();
336        t.interrupt();
337        t.join();
335      }
336  
337      /**
# Line 342 | 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 350 | Line 347 | public class PriorityBlockingQueueTest e
347                  assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
348              }});
349  
350 <        t.start();
354 <        Thread.sleep(SMALL_DELAY_MS);
355 <        t.interrupt();
356 <        t.join();
350 >        awaitTermination(t);
351      }
352  
353      /**
# Line 371 | 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  
391
396      /**
397       * poll succeeds unless empty
398       */
# Line 415 | 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 427 | 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 {
432                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 664 | Line 680 | public class PriorityBlockingQueueTest e
680          assertFalse(it.hasNext());
681      }
682  
667
683      /**
684       * toString contains toStrings of elements
685       */
# Line 672 | 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