1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6316155 6595669 6871697 6868712
  27  * @summary Test concurrent offer vs. remove
  28  * @library /test/lib
  29  * @run main OfferRemoveLoops 100
  30  * @author Martin Buchholz
  31  */
  32 
  33 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  34 
  35 import java.util.Arrays;
  36 import java.util.Queue;
  37 import java.util.SplittableRandom;
  38 import java.util.concurrent.ArrayBlockingQueue;
  39 import java.util.concurrent.CountDownLatch;
  40 import java.util.concurrent.ConcurrentLinkedDeque;
  41 import java.util.concurrent.ConcurrentLinkedQueue;
  42 import java.util.concurrent.LinkedBlockingDeque;
  43 import java.util.concurrent.LinkedBlockingQueue;
  44 import java.util.concurrent.LinkedTransferQueue;
  45 import java.util.concurrent.PriorityBlockingQueue;
  46 import java.util.concurrent.Semaphore;
  47 import jdk.test.lib.Utils;
  48 
  49 @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
  50 public class OfferRemoveLoops {
  51     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  52     final long testDurationMillisDefault = 10_000L;
  53     final long testDurationMillis;
  54 
  55     OfferRemoveLoops(String[] args) {
  56         testDurationMillis = (args.length > 0) ?
  57             Long.valueOf(args[0]) : testDurationMillisDefault;
  58     }
  59 
  60     void checkNotContainsNull(Iterable it) {
  61         for (Object x : it)
  62             check(x != null);
  63     }
  64 
  65     void test(String[] args) throws Throwable {
  66         testQueue(new LinkedBlockingQueue(10));
  67         testQueue(new LinkedBlockingQueue());
  68         testQueue(new LinkedBlockingDeque(10));
  69         testQueue(new LinkedBlockingDeque());
  70         testQueue(new ArrayBlockingQueue(10));
  71         testQueue(new PriorityBlockingQueue(10));
  72         testQueue(new ConcurrentLinkedDeque());
  73         testQueue(new ConcurrentLinkedQueue());
  74         testQueue(new LinkedTransferQueue());
  75     }
  76 
  77     void testQueue(final Queue q) throws Throwable {
  78         System.err.println(q.getClass().getSimpleName());
  79         final long testDurationNanos = testDurationMillis * 1000L * 1000L;
  80         final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
  81         final int maxChunkSize = 1042;
  82         final int maxQueueSize = 10 * maxChunkSize;
  83         final CountDownLatch done = new CountDownLatch(3);
  84         final SplittableRandom rnd = new SplittableRandom();
  85 
  86         // Poor man's bounded buffer; prevents unbounded queue expansion.
  87         final Semaphore offers = new Semaphore(maxQueueSize);
  88 
  89         abstract class CheckedThread extends Thread {
  90             final SplittableRandom rnd;
  91 
  92             CheckedThread(String name, SplittableRandom rnd) {
  93                 super(name);
  94                 this.rnd = rnd;
  95                 setDaemon(true);
  96                 start();
  97             }
  98             /** Polls for quitting time. */
  99             protected boolean quittingTime() {
 100                 return System.nanoTime() - quittingTimeNanos > 0;
 101             }
 102             /** Polls occasionally for quitting time. */
 103             protected boolean quittingTime(long i) {
 104                 return (i % 1024) == 0 && quittingTime();
 105             }
 106             protected abstract void realRun() throws Exception;
 107             public void run() {
 108                 try { realRun(); } catch (Throwable t) { unexpected(t); }
 109             }
 110         }
 111 
 112         Thread offerer = new CheckedThread("offerer", rnd.split()) {
 113             protected void realRun() throws InterruptedException {
 114                 final int chunkSize = rnd.nextInt(maxChunkSize) + 20;
 115                 long c = 0;
 116                 while (! quittingTime()) {
 117                     if (q.offer(Long.valueOf(c))) {
 118                         if ((++c % chunkSize) == 0) {
 119                             offers.acquire(chunkSize);
 120                         }
 121                     } else {
 122                         Thread.yield();
 123                     }
 124                 }
 125                 done.countDown();
 126             }};
 127 
 128         Thread remover = new CheckedThread("remover", rnd.split()) {
 129             protected void realRun() {
 130                 final int chunkSize = rnd.nextInt(maxChunkSize) + 20;
 131                 long c = 0;
 132                 while (! quittingTime()) {
 133                     if (q.remove(Long.valueOf(c))) {
 134                         if ((++c % chunkSize) == 0) {
 135                             offers.release(chunkSize);
 136                         }
 137                     } else {
 138                         Thread.yield();
 139                     }
 140                 }
 141                 q.clear();
 142                 offers.release(1<<30);  // Releases waiting offerer thread
 143                 done.countDown();
 144             }};
 145 
 146         Thread scanner = new CheckedThread("scanner", rnd.split()) {
 147             protected void realRun() {
 148                 while (! quittingTime()) {
 149                     switch (rnd.nextInt(3)) {
 150                     case 0: checkNotContainsNull(q); break;
 151                     case 1: q.size(); break;
 152                     case 2: checkNotContainsNull
 153                             (Arrays.asList(q.toArray(new Long[0])));
 154                         break;
 155                     }
 156                     Thread.yield();
 157                 }
 158                 done.countDown();
 159             }};
 160 
 161         if (! done.await(LONG_DELAY_MS + testDurationMillis, MILLISECONDS)) {
 162             for (Thread thread : new Thread[] { offerer, remover, scanner }) {
 163                 if (thread.isAlive()) {
 164                     System.err.printf("Hung thread: %s%n", thread.getName());
 165                     failed++;
 166                     for (StackTraceElement e : thread.getStackTrace())
 167                         System.err.println(e);
 168                     thread.interrupt();
 169                 }
 170             }
 171         }
 172     }
 173 
 174     //--------------------- Infrastructure ---------------------------
 175     volatile int passed = 0, failed = 0;
 176     void pass() {passed++;}
 177     void fail() {failed++; Thread.dumpStack();}
 178     void fail(String msg) {System.err.println(msg); fail();}
 179     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 180     void check(boolean cond) {if (cond) pass(); else fail();}
 181     void equal(Object x, Object y) {
 182         if (x == null ? y == null : x.equals(y)) pass();
 183         else fail(x + " not equal to " + y);}
 184     public static void main(String[] args) throws Throwable {
 185         new OfferRemoveLoops(args).instanceMain(args);}
 186     public void instanceMain(String[] args) throws Throwable {
 187         try {test(args);} catch (Throwable t) {unexpected(t);}
 188         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 189         if (failed > 0) throw new AssertionError("Some tests failed");}
 190 }