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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.66 by jsr166, Sun Oct 18 04:48:32 2015 UTC vs.
Revision 1.80 by jsr166, Tue Aug 15 20:30:30 2017 UTC

# Line 15 | Line 15 | import java.util.List;
15   import java.util.NoSuchElementException;
16   import java.util.Queue;
17   import java.util.concurrent.BlockingQueue;
18 + import java.util.concurrent.Callable;
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
# Line 24 | Line 25 | import junit.framework.Test;
25  
26   @SuppressWarnings({"unchecked", "rawtypes"})
27   public class LinkedTransferQueueTest extends JSR166TestCase {
27    static class Implementation implements CollectionImplementation {
28        public Class<?> klazz() { return LinkedTransferQueue.class; }
29        public Collection emptyCollection() { return new LinkedTransferQueue(); }
30        public Object makeElement(int i) { return i; }
31        public boolean isConcurrent() { return true; }
32        public boolean permitsNulls() { return false; }
33    }
34
28      public static class Generic extends BlockingQueueTest {
29          protected BlockingQueue emptyCollection() {
30              return new LinkedTransferQueue();
# Line 43 | Line 36 | public class LinkedTransferQueueTest ext
36      }
37  
38      public static Test suite() {
39 +        class Implementation implements CollectionImplementation {
40 +            public Class<?> klazz() { return LinkedTransferQueue.class; }
41 +            public Collection emptyCollection() { return new LinkedTransferQueue(); }
42 +            public Object makeElement(int i) { return i; }
43 +            public boolean isConcurrent() { return true; }
44 +            public boolean permitsNulls() { return false; }
45 +        }
46          return newTestSuite(LinkedTransferQueueTest.class,
47                              new Generic().testSuite(),
48                              CollectionTest.testSuite(new Implementation()));
# Line 183 | Line 183 | public class LinkedTransferQueueTest ext
183       * all elements successfully put are contained
184       */
185      public void testPut() {
186 <        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
186 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
187          for (int i = 0; i < SIZE; ++i) {
188              assertEquals(i, q.size());
189              q.put(i);
# Line 209 | Line 209 | public class LinkedTransferQueueTest ext
209          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
210          Thread t = newStartedThread(new CheckedRunnable() {
211              public void realRun() throws InterruptedException {
212 <                for (int i = 0; i < SIZE; ++i) {
213 <                    assertEquals(i, q.take());
214 <                }
212 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
213  
214                  Thread.currentThread().interrupt();
215                  try {
# Line 229 | Line 227 | public class LinkedTransferQueueTest ext
227              }});
228  
229          await(pleaseInterrupt);
230 <        assertThreadStaysAlive(t);
230 >        assertThreadBlocks(t, Thread.State.WAITING);
231          t.interrupt();
232          awaitTermination(t);
233      }
# Line 280 | Line 278 | public class LinkedTransferQueueTest ext
278       */
279      public void testInterruptedTimedPoll() throws InterruptedException {
280          final BlockingQueue<Integer> q = populatedQueue(SIZE);
281 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
281 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282          Thread t = newStartedThread(new CheckedRunnable() {
283              public void realRun() throws InterruptedException {
284                  long startTime = System.nanoTime();
285 <                for (int i = 0; i < SIZE; ++i)
285 >                for (int i = 0; i < SIZE; i++)
286                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
287 <                aboutToWait.countDown();
287 >
288 >                Thread.currentThread().interrupt();
289                  try {
290                      q.poll(LONG_DELAY_MS, MILLISECONDS);
291                      shouldThrow();
292                  } catch (InterruptedException success) {}
293 +                assertFalse(Thread.interrupted());
294 +
295 +                pleaseInterrupt.countDown();
296 +                try {
297 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
298 +                    shouldThrow();
299 +                } catch (InterruptedException success) {}
300 +                assertFalse(Thread.interrupted());
301 +
302                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
303              }});
304  
305 <        aboutToWait.await();
306 <        waitForThreadToEnterWaitState(t);
305 >        await(pleaseInterrupt);
306 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
307          t.interrupt();
308          awaitTermination(t);
309          checkEmpty(q);
# Line 317 | Line 325 | public class LinkedTransferQueueTest ext
325                      q.poll(LONG_DELAY_MS, MILLISECONDS);
326                      shouldThrow();
327                  } catch (InterruptedException success) {}
328 +                assertFalse(Thread.interrupted());
329                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
330              }});
331  
# Line 416 | Line 425 | public class LinkedTransferQueueTest ext
425       */
426      public void testContainsAll() {
427          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
428 <        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
428 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>();
429          for (int i = 0; i < SIZE; ++i) {
430              assertTrue(q.containsAll(p));
431              assertFalse(p.containsAll(q));
# Line 545 | Line 554 | public class LinkedTransferQueueTest ext
554       * iterator ordering is FIFO
555       */
556      public void testIteratorOrdering() {
557 <        final LinkedTransferQueue<Integer> q
549 <            = new LinkedTransferQueue<Integer>();
557 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
558          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
559          q.add(one);
560          q.add(two);
# Line 639 | Line 647 | public class LinkedTransferQueueTest ext
647      }
648  
649      /**
650 <     * A deserialized serialized queue has same elements in same order
650 >     * A deserialized/reserialized queue has same elements in same order
651       */
652      public void testSerialization() throws Exception {
653          Queue x = populatedQueue(SIZE);
# Line 741 | Line 749 | public class LinkedTransferQueueTest ext
749              }});
750  
751          threadStarted.await();
752 <        waitForThreadToEnterWaitState(t);
753 <        assertEquals(1, q.getWaitingConsumerCount());
754 <        assertTrue(q.hasWaitingConsumer());
752 >        Callable<Boolean> oneConsumer
753 >            = new Callable<Boolean>() { public Boolean call() {
754 >                return q.hasWaitingConsumer()
755 >                && q.getWaitingConsumerCount() == 1; }};
756 >        waitForThreadToEnterWaitState(t, oneConsumer);
757  
758          assertTrue(q.offer(one));
759          assertEquals(0, q.getWaitingConsumerCount());
# Line 764 | Line 774 | public class LinkedTransferQueueTest ext
774      }
775  
776      /**
777 <     * transfer waits until a poll occurs. The transfered element
778 <     * is returned by this associated poll.
777 >     * transfer waits until a poll occurs. The transferred element
778 >     * is returned by the associated poll.
779       */
780      public void testTransfer2() throws InterruptedException {
781 <        final LinkedTransferQueue<Integer> q
772 <            = new LinkedTransferQueue<Integer>();
781 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
782          final CountDownLatch threadStarted = new CountDownLatch(1);
783  
784          Thread t = newStartedThread(new CheckedRunnable() {
# Line 780 | Line 789 | public class LinkedTransferQueueTest ext
789              }});
790  
791          threadStarted.await();
792 <        waitForThreadToEnterWaitState(t);
793 <        assertEquals(1, q.size());
792 >        Callable<Boolean> oneElement
793 >            = new Callable<Boolean>() { public Boolean call() {
794 >                return !q.isEmpty() && q.size() == 1; }};
795 >        waitForThreadToEnterWaitState(t, oneElement);
796 >
797          assertSame(five, q.poll());
798          checkEmpty(q);
799          awaitTermination(t);
# Line 791 | Line 803 | public class LinkedTransferQueueTest ext
803       * transfer waits until a poll occurs, and then transfers in fifo order
804       */
805      public void testTransfer3() throws InterruptedException {
806 <        final LinkedTransferQueue<Integer> q
795 <            = new LinkedTransferQueue<Integer>();
806 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
807  
808          Thread first = newStartedThread(new CheckedRunnable() {
809              public void realRun() throws InterruptedException {
810                  q.transfer(four);
811 <                assertTrue(!q.contains(four));
811 >                assertFalse(q.contains(four));
812                  assertEquals(1, q.size());
813              }});
814  
# Line 844 | Line 855 | public class LinkedTransferQueueTest ext
855      }
856  
857      /**
858 <     * transfer waits until a take occurs. The transfered element
859 <     * is returned by this associated take.
858 >     * transfer waits until a take occurs. The transferred element
859 >     * is returned by the associated take.
860       */
861      public void testTransfer5() throws InterruptedException {
862 <        final LinkedTransferQueue<Integer> q
852 <            = new LinkedTransferQueue<Integer>();
862 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
863  
864          Thread t = newStartedThread(new CheckedRunnable() {
865              public void realRun() throws InterruptedException {
# Line 962 | Line 972 | public class LinkedTransferQueueTest ext
972              }});
973  
974          await(pleaseInterrupt);
975 <        assertThreadStaysAlive(t);
975 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
976          t.interrupt();
977          awaitTermination(t);
978          checkEmpty(q);
# Line 1030 | Line 1040 | public class LinkedTransferQueueTest ext
1040      }
1041  
1042      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1043 <        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1043 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
1044          checkEmpty(q);
1045          for (int i = 0; i < n; i++) {
1046              assertEquals(i, q.size());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines