1 /*
   2  * Copyright (c) 2006, 2014, 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 6384064
  27  * @summary Check proper handling of interrupts
  28  * @author Martin Buchholz
  29  * @library /test/lib
  30  */
  31 
  32 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  33 
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 import java.util.concurrent.ArrayBlockingQueue;
  37 import java.util.concurrent.BlockingDeque;
  38 import java.util.concurrent.BlockingQueue;
  39 import java.util.concurrent.LinkedBlockingDeque;
  40 import java.util.concurrent.LinkedBlockingQueue;
  41 import java.util.concurrent.SynchronousQueue;
  42 import java.util.concurrent.Executor;
  43 import java.util.concurrent.ScheduledThreadPoolExecutor;
  44 import jdk.test.lib.Utils;
  45 
  46 public class Interrupt {
  47     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  48 
  49     static void checkInterrupted0(Iterable<Fun> fs, Executor ex) {
  50         for (Fun f : fs) {
  51             try {
  52                 ex.execute(new Runnable() {
  53                         final Thread thisThread = Thread.currentThread();
  54                         public void run() { thisThread.interrupt(); }});
  55                 f.f();
  56                 fail("Expected InterruptedException not thrown");
  57             } catch (InterruptedException e) {
  58                 check(! Thread.interrupted());
  59             } catch (Throwable t) { unexpected(t); }
  60         }
  61     }
  62 
  63     static void checkInterrupted(Iterable<Fun> fs)
  64             throws InterruptedException {
  65         final Executor immediateExecutor = new Executor() {
  66                 public void execute(Runnable r) {
  67                     r.run(); }};
  68         final ScheduledThreadPoolExecutor stpe
  69             = new ScheduledThreadPoolExecutor(1);
  70         final Executor delayedExecutor = new Executor() {
  71                 public void execute(Runnable r) {
  72                     stpe.schedule(r, 20, MILLISECONDS); }};
  73         checkInterrupted0(fs, immediateExecutor);
  74         checkInterrupted0(fs, delayedExecutor);
  75         stpe.shutdown();
  76         check(stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  77     }
  78 
  79     static void testQueue(final BlockingQueue<Object> q) {
  80         try {
  81             final BlockingDeque<Object> deq =
  82                 (q instanceof BlockingDeque<?>) ?
  83                 (BlockingDeque<Object>) q : null;
  84             q.clear();
  85             List<Fun> fs = new ArrayList<>();
  86             fs.add(() -> q.take());
  87             fs.add(() -> q.poll(LONG_DELAY_MS, MILLISECONDS));
  88             if (deq != null) {
  89                 fs.add(() -> deq.takeFirst());
  90                 fs.add(() -> deq.takeLast());
  91                 fs.add(() -> deq.pollFirst(LONG_DELAY_MS, MILLISECONDS));
  92                 fs.add(() -> deq.pollLast(LONG_DELAY_MS, MILLISECONDS));
  93             }
  94 
  95             checkInterrupted(fs);
  96 
  97             // fill q to capacity, to ensure insertions will block
  98             while (q.remainingCapacity() > 0)
  99                 try { q.put(1); }
 100                 catch (Throwable t) { unexpected(t); }
 101 
 102             fs.clear();
 103             fs.add(() -> q.put(1));
 104             fs.add(() -> q.offer(1, LONG_DELAY_MS, MILLISECONDS));
 105             if (deq != null) {
 106                 fs.add(() -> deq.putFirst(1));
 107                 fs.add(() -> deq.putLast(1));
 108                 fs.add(() -> deq.offerFirst(1, LONG_DELAY_MS, MILLISECONDS));
 109                 fs.add(() -> deq.offerLast(1, LONG_DELAY_MS, MILLISECONDS));
 110             }
 111             checkInterrupted(fs);
 112         } catch (Throwable t) {
 113             System.out.printf("Failed: %s%n", q.getClass().getSimpleName());
 114             unexpected(t);
 115         } finally {
 116             Thread.interrupted();       // clear interrupts, just in case
 117         }
 118     }
 119 
 120     private static void realMain(final String[] args) throws Throwable {
 121         testQueue(new SynchronousQueue<Object>());
 122         testQueue(new ArrayBlockingQueue<Object>(1,false));
 123         testQueue(new ArrayBlockingQueue<Object>(1,true));
 124         testQueue(new LinkedBlockingQueue<Object>(1));
 125         testQueue(new LinkedBlockingDeque<Object>(1));
 126     }
 127 
 128     //--------------------- Infrastructure ---------------------------
 129     static volatile int passed = 0, failed = 0;
 130     static void pass() {passed++;}
 131     static void fail() {failed++; Thread.dumpStack();}
 132     static void fail(String msg) {System.out.println(msg); fail();}
 133     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 134     static void check(boolean cond) {if (cond) pass(); else fail();}
 135     static void equal(Object x, Object y) {
 136         if (x == null ? y == null : x.equals(y)) pass();
 137         else fail(x + " not equal to " + y);}
 138     public static void main(String[] args) throws Throwable {
 139         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 140         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 141         if (failed > 0) throw new AssertionError("Some tests failed");}
 142     interface Fun {void f() throws Throwable;}
 143 }