test/jdk/java/util/concurrent/BlockingQueue/OfferDrainToLoops.java
changeset 58138 1e4270f875ee
parent 48541 946e34c2dec9
equal deleted inserted replaced
58137:6a556bcd94fc 58138:1e4270f875ee
    32  */
    32  */
    33 
    33 
    34 /*
    34 /*
    35  * @test
    35  * @test
    36  * @bug 6805775 6815766
    36  * @bug 6805775 6815766
       
    37  * @library /test/lib
    37  * @run main OfferDrainToLoops 100
    38  * @run main OfferDrainToLoops 100
    38  * @summary Test concurrent offer vs. drainTo
    39  * @summary Test concurrent offer vs. drainTo
    39  */
    40  */
    40 
    41 
    41 import java.util.ArrayList;
    42 import java.util.ArrayList;
    45 import java.util.concurrent.BlockingQueue;
    46 import java.util.concurrent.BlockingQueue;
    46 import java.util.concurrent.LinkedBlockingDeque;
    47 import java.util.concurrent.LinkedBlockingDeque;
    47 import java.util.concurrent.LinkedBlockingQueue;
    48 import java.util.concurrent.LinkedBlockingQueue;
    48 import java.util.concurrent.LinkedTransferQueue;
    49 import java.util.concurrent.LinkedTransferQueue;
    49 import java.util.concurrent.atomic.AtomicLong;
    50 import java.util.concurrent.atomic.AtomicLong;
       
    51 import jdk.test.lib.Utils;
    50 
    52 
    51 @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
    53 @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
    52 public class OfferDrainToLoops {
    54 public class OfferDrainToLoops {
    53     final long testDurationMillisDefault = 10L * 1000L;
    55     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
       
    56     final long testDurationMillisDefault = 10_000L;
    54     final long testDurationMillis;
    57     final long testDurationMillis;
    55 
    58 
    56     OfferDrainToLoops(String[] args) {
    59     OfferDrainToLoops(String[] args) {
    57         testDurationMillis = (args.length > 0) ?
    60         testDurationMillis = (args.length > 0) ?
    58             Long.valueOf(args[0]) : testDurationMillisDefault;
    61             Long.valueOf(args[0]) : testDurationMillisDefault;
    74 
    77 
    75     void test(final BlockingQueue q) throws Throwable {
    78     void test(final BlockingQueue q) throws Throwable {
    76         System.out.println(q.getClass().getSimpleName());
    79         System.out.println(q.getClass().getSimpleName());
    77         final long testDurationNanos = testDurationMillis * 1000L * 1000L;
    80         final long testDurationNanos = testDurationMillis * 1000L * 1000L;
    78         final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
    81         final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
    79         final long timeoutMillis = 10L * 1000L;
       
    80         final SplittableRandom rnd = new SplittableRandom();
    82         final SplittableRandom rnd = new SplittableRandom();
    81 
    83 
    82         // Poor man's bounded buffer.
    84         // Poor man's bounded buffer.
    83         final AtomicLong approximateCount = new AtomicLong(0L);
    85         final AtomicLong approximateCount = new AtomicLong(0L);
    84 
    86 
   153                     }
   155                     }
   154                     Thread.yield();
   156                     Thread.yield();
   155                 }}};
   157                 }}};
   156 
   158 
   157         for (Thread thread : new Thread[] { offerer, drainer, scanner }) {
   159         for (Thread thread : new Thread[] { offerer, drainer, scanner }) {
   158             thread.join(timeoutMillis + testDurationMillis);
   160             thread.join(LONG_DELAY_MS + testDurationMillis);
   159             if (thread.isAlive()) {
   161             if (thread.isAlive()) {
   160                 System.err.printf("Hung thread: %s%n", thread.getName());
   162                 System.err.printf("Hung thread: %s%n", thread.getName());
   161                 failed++;
   163                 failed++;
   162                 for (StackTraceElement e : thread.getStackTrace())
   164                 for (StackTraceElement e : thread.getStackTrace())
   163                     System.err.println(e);
   165                     System.err.println(e);
   164                 thread.join(timeoutMillis);
   166                 thread.join(LONG_DELAY_MS);
   165             }
   167             }
   166         }
   168         }
   167     }
   169     }
   168 
   170 
   169     //--------------------- Infrastructure ---------------------------
   171     //--------------------- Infrastructure ---------------------------