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.43 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.44 by jsr166, Fri May 27 20:07:24 2011 UTC

# 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 <        delay(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 <        delay(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 <        delay(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 439 | Line 448 | public class PriorityBlockingQueueTest e
448                  long t0 = System.nanoTime();
449                  aboutToWait.countDown();
450                  try {
451 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
451 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
452                      shouldThrow();
453                  } catch (InterruptedException success) {
454                      assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
# Line 671 | Line 680 | public class PriorityBlockingQueueTest e
680          assertFalse(it.hasNext());
681      }
682  
674
683      /**
684       * toString contains toStrings of elements
685       */
# Line 679 | 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 <                delay(SMALL_DELAY_MS);
711 >                threadsStarted.await();
712                  q.put(one);
713              }});
714  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines