jdk/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
child 43522 f9c6f543c4db
equal deleted inserted replaced
36232:7a020ad42ac0 36233:f85ed703cf7e
    33 
    33 
    34 /*
    34 /*
    35  * @test
    35  * @test
    36  * @bug 4486658
    36  * @bug 4486658
    37  * @summary Checks for responsiveness of blocking queues to cancellation.
    37  * @summary Checks for responsiveness of blocking queues to cancellation.
       
    38  * @library /lib/testlibrary/
    38  */
    39  */
       
    40 
       
    41 import static java.util.concurrent.TimeUnit.MILLISECONDS;
    39 
    42 
    40 import java.util.ArrayList;
    43 import java.util.ArrayList;
    41 import java.util.List;
    44 import java.util.List;
    42 import java.util.SplittableRandom;
    45 import java.util.SplittableRandom;
    43 import java.util.concurrent.ArrayBlockingQueue;
    46 import java.util.concurrent.ArrayBlockingQueue;
    51 import java.util.concurrent.Future;
    54 import java.util.concurrent.Future;
    52 import java.util.concurrent.LinkedBlockingDeque;
    55 import java.util.concurrent.LinkedBlockingDeque;
    53 import java.util.concurrent.LinkedBlockingQueue;
    56 import java.util.concurrent.LinkedBlockingQueue;
    54 import java.util.concurrent.SynchronousQueue;
    57 import java.util.concurrent.SynchronousQueue;
    55 import java.util.concurrent.TimeUnit;
    58 import java.util.concurrent.TimeUnit;
       
    59 import jdk.testlibrary.Utils;
    56 
    60 
    57 public class CancelledProducerConsumerLoops {
    61 public class CancelledProducerConsumerLoops {
       
    62     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
    58     static ExecutorService pool;
    63     static ExecutorService pool;
    59 
    64 
    60     public static void main(String[] args) throws Exception {
    65     public static void main(String[] args) throws Exception {
    61         final int maxPairs = (args.length > 0) ? Integer.parseInt(args[0]) : 5;
    66         final int maxPairs = (args.length > 0) ? Integer.parseInt(args[0]) : 5;
    62 
    67 
    71             // PriorityBlockingQueue, LinkedTransferQueue
    76             // PriorityBlockingQueue, LinkedTransferQueue
    72             for (BlockingQueue<Integer> queue : queues)
    77             for (BlockingQueue<Integer> queue : queues)
    73                 new CancelledProducerConsumerLoops(i, queue).run();
    78                 new CancelledProducerConsumerLoops(i, queue).run();
    74         }
    79         }
    75         pool.shutdown();
    80         pool.shutdown();
    76         if (! pool.awaitTermination(10L, TimeUnit.SECONDS))
    81         if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
    77             throw new AssertionError("timed out");
    82             throw new AssertionError("timed out");
    78         pool = null;
    83         pool = null;
    79     }
    84     }
    80 
    85 
    81     final int npairs;
    86     final int npairs;
   115         for (int i = 1; i < npairs; i++) {
   120         for (int i = 1; i < npairs; i++) {
   116             assertCancelled(prods[i]);
   121             assertCancelled(prods[i]);
   117             assertCancelled(cons[i]);
   122             assertCancelled(cons[i]);
   118         }
   123         }
   119 
   124 
   120         if (!producersInterrupted.await(10L, TimeUnit.SECONDS))
   125         if (!producersInterrupted.await(LONG_DELAY_MS, MILLISECONDS))
   121             throw new AssertionError("timed out");
   126             throw new AssertionError("timed out");
   122         if (!consumersInterrupted.await(10L, TimeUnit.SECONDS))
   127         if (!consumersInterrupted.await(LONG_DELAY_MS, MILLISECONDS))
   123             throw new AssertionError("timed out");
   128             throw new AssertionError("timed out");
   124         if (prods[0].isDone() || prods[0].isCancelled())
   129         if (prods[0].isDone() || prods[0].isCancelled())
   125             throw new AssertionError("completed too early");
   130             throw new AssertionError("completed too early");
   126 
   131 
   127         done = true;
   132         done = true;
   128 
   133 
   129         if (! (prods[0].get(10L, TimeUnit.SECONDS) instanceof Integer))
   134         if (! (prods[0].get(LONG_DELAY_MS, MILLISECONDS) instanceof Integer))
   130             throw new AssertionError("expected Integer");
   135             throw new AssertionError("expected Integer");
   131         if (! (cons[0].get(10L, TimeUnit.SECONDS) instanceof Integer))
   136         if (! (cons[0].get(LONG_DELAY_MS, MILLISECONDS) instanceof Integer))
   132             throw new AssertionError("expected Integer");
   137             throw new AssertionError("expected Integer");
   133     }
   138     }
   134 
   139 
   135     void assertCancelled(Future<?> future) throws Exception {
   140     void assertCancelled(Future<?> future) throws Exception {
   136         if (!future.isDone())
   141         if (!future.isDone())
   137             throw new AssertionError("not done");
   142             throw new AssertionError("not done");
   138         if (!future.isCancelled())
   143         if (!future.isCancelled())
   139             throw new AssertionError("not cancelled");
   144             throw new AssertionError("not cancelled");
   140         try {
   145         try {
   141             future.get(10L, TimeUnit.SECONDS);
   146             future.get(LONG_DELAY_MS, MILLISECONDS);
   142             throw new AssertionError("should throw CancellationException");
   147             throw new AssertionError("should throw CancellationException");
   143         } catch (CancellationException success) {}
   148         } catch (CancellationException success) {}
   144     }
   149     }
   145 
   150 
   146     class Producer implements Callable<Integer> {
   151     class Producer implements Callable<Integer> {