1 /*
   2  * Copyright (c) 2007, 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 6450200 6450205 6450207 6450211
  27  * @summary Test proper handling of tasks that terminate abruptly
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.security.*;
  32 import java.util.*;
  33 import java.util.concurrent.*;
  34 import java.util.concurrent.atomic.*;
  35 import java.lang.reflect.Field;
  36 import sun.misc.Unsafe;
  37 
  38 public class ThrowingTasks {
  39     static final Unsafe UNSAFE;
  40     static {
  41         try {
  42             Field f = Unsafe.class.getDeclaredField("theUnsafe");
  43             f.setAccessible(true);
  44             UNSAFE = (Unsafe)f.get(null);
  45         } catch (NoSuchFieldException | IllegalAccessException e) {
  46             throw new RuntimeException(e);
  47         }
  48     }
  49 
  50     static final Random rnd = new Random();
  51 
  52     @SuppressWarnings("serial")
  53     static class UncaughtExceptions
  54         extends ConcurrentHashMap<Class<?>, Integer> {
  55 
  56         void inc(Class<?> key) {
  57             for (;;) {
  58                 Integer i = get(key);
  59                 if (i == null) {
  60                     if (putIfAbsent(key, 1) == null)
  61                         return;
  62                 } else {
  63                     if (replace(key, i, i + 1))
  64                         return;
  65                 }
  66             }
  67         }
  68     }
  69 
  70     @SuppressWarnings("serial")
  71     static class UncaughtExceptionsTable
  72         extends Hashtable<Class<?>, Integer> {
  73 
  74         synchronized void inc(Class<?> key) {
  75             Integer i = get(key);
  76             put(key, (i == null) ? 1 : i + 1);
  77         }
  78     }
  79 
  80     static final UncaughtExceptions uncaughtExceptions
  81         = new UncaughtExceptions();
  82     static final UncaughtExceptionsTable uncaughtExceptionsTable
  83         = new UncaughtExceptionsTable();
  84     static final AtomicLong totalUncaughtExceptions
  85         = new AtomicLong(0);
  86     static final CountDownLatch uncaughtExceptionsLatch
  87         = new CountDownLatch(24);
  88 
  89     static final Thread.UncaughtExceptionHandler handler
  90         = new Thread.UncaughtExceptionHandler() {
  91                 public void uncaughtException(Thread t, Throwable e) {
  92                     check(! Thread.currentThread().isInterrupted());
  93                     totalUncaughtExceptions.getAndIncrement();
  94                     uncaughtExceptions.inc(e.getClass());
  95                     uncaughtExceptionsTable.inc(e.getClass());
  96                     uncaughtExceptionsLatch.countDown();
  97                 }};
  98 
  99     static final ThreadGroup tg = new ThreadGroup("Flaky");
 100 
 101     static final ThreadFactory tf = new ThreadFactory() {
 102             public Thread newThread(Runnable r) {
 103                 Thread t = new Thread(tg, r);
 104                 t.setUncaughtExceptionHandler(handler);
 105                 return t;
 106             }};
 107 
 108     static final RuntimeException rte = new RuntimeException();
 109     static final Error error = new Error();
 110     static final Throwable weird = new Throwable();
 111     static final Exception checkedException = new Exception();
 112 
 113     static class Thrower implements Runnable {
 114         Throwable t;
 115         Thrower(Throwable t) { this.t = t; }
 116         @SuppressWarnings("deprecation")
 117         public void run() { if (t != null) UNSAFE.throwException(t); }
 118     }
 119 
 120     static final Thrower noThrower      = new Thrower(null);
 121     static final Thrower rteThrower     = new Thrower(rte);
 122     static final Thrower errorThrower   = new Thrower(error);
 123     static final Thrower weirdThrower   = new Thrower(weird);
 124     static final Thrower checkedThrower = new Thrower(checkedException);
 125 
 126     static final List<Thrower> throwers = Arrays.asList(
 127         noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
 128 
 129     static class Flaky implements Runnable {
 130         final Runnable beforeExecute;
 131         final Runnable execute;
 132         Flaky(Runnable beforeExecute,
 133               Runnable execute) {
 134             this.beforeExecute = beforeExecute;
 135             this.execute = execute;
 136         }
 137         public void run() { execute.run(); }
 138     }
 139 
 140     static final List<Flaky> flakes = new ArrayList<Flaky>();
 141     static {
 142         for (Thrower x : throwers)
 143             for (Thrower y : throwers)
 144                 flakes.add(new Flaky(x, y));
 145         Collections.shuffle(flakes);
 146     }
 147 
 148     static final CountDownLatch allStarted = new CountDownLatch(flakes.size());
 149     static final CountDownLatch allContinue = new CountDownLatch(1);
 150 
 151     static class PermissiveSecurityManger extends SecurityManager {
 152         public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
 153     }
 154 
 155     static void checkTerminated(ThreadPoolExecutor tpe) {
 156         try {
 157             check(tpe.getQueue().isEmpty());
 158             check(tpe.isShutdown());
 159             check(tpe.isTerminated());
 160             check(! tpe.isTerminating());
 161             equal(tpe.getActiveCount(), 0);
 162             equal(tpe.getPoolSize(), 0);
 163             equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
 164             check(tpe.awaitTermination(0, TimeUnit.SECONDS));
 165         } catch (Throwable t) { unexpected(t); }
 166     }
 167 
 168     static class CheckingExecutor extends ThreadPoolExecutor {
 169         CheckingExecutor() {
 170             super(10, 10,
 171                   1L, TimeUnit.HOURS,
 172                   new LinkedBlockingQueue<Runnable>(),
 173                   tf);
 174         }
 175         @Override protected void beforeExecute(Thread t, Runnable r) {
 176             allStarted.countDown();
 177             if (allStarted.getCount() < getCorePoolSize())
 178                 try { allContinue.await(); }
 179                 catch (InterruptedException x) { unexpected(x); }
 180             beforeExecuteCount.getAndIncrement();
 181             check(! isTerminated());
 182             ((Flaky)r).beforeExecute.run();
 183         }
 184         @Override protected void afterExecute(Runnable r, Throwable t) {
 185             //System.out.println(tg.activeCount());
 186             afterExecuteCount.getAndIncrement();
 187             check(((Thrower)((Flaky)r).execute).t == t);
 188             check(! isTerminated());
 189         }
 190         @Override protected void terminated() {
 191             try {
 192                 terminatedCount.getAndIncrement();
 193                 if (rnd.nextBoolean()) {
 194                     check(isShutdown());
 195                     check(isTerminating());
 196                     check(! isTerminated());
 197                     check(! awaitTermination(0L, TimeUnit.MINUTES));
 198                 }
 199             } catch (Throwable t) { unexpected(t); }
 200         }
 201     }
 202 
 203     static final AtomicInteger beforeExecuteCount = new AtomicInteger(0);
 204     static final AtomicInteger afterExecuteCount  = new AtomicInteger(0);
 205     static final AtomicInteger terminatedCount    = new AtomicInteger(0);
 206 
 207     private static void realMain(String[] args) throws Throwable {
 208         if (rnd.nextBoolean())
 209             System.setSecurityManager(new PermissiveSecurityManger());
 210 
 211         CheckingExecutor tpe = new CheckingExecutor();
 212 
 213         for (Runnable task : flakes)
 214             tpe.execute(task);
 215 
 216         if (rnd.nextBoolean()) {
 217             allStarted.await();
 218             equal(tpe.getTaskCount(),
 219                   (long) flakes.size());
 220             equal(tpe.getCompletedTaskCount(),
 221                   (long) flakes.size() - tpe.getCorePoolSize());
 222         }
 223         allContinue.countDown();
 224 
 225         //System.out.printf("thread count = %d%n", tg.activeCount());
 226         uncaughtExceptionsLatch.await();
 227 
 228         while (tg.activeCount() != tpe.getCorePoolSize() ||
 229                tg.activeCount() != tpe.getCorePoolSize())
 230             Thread.sleep(10);
 231         equal(tg.activeCount(), tpe.getCorePoolSize());
 232 
 233         tpe.shutdown();
 234 
 235         check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
 236         checkTerminated(tpe);
 237 
 238         //while (tg.activeCount() > 0) Thread.sleep(10);
 239         //System.out.println(uncaughtExceptions);
 240         List<Map<Class<?>, Integer>> maps
 241             = new ArrayList<Map<Class<?>, Integer>>();
 242         maps.add(uncaughtExceptions);
 243         maps.add(uncaughtExceptionsTable);
 244         for (Map<Class<?>, Integer> map : maps) {
 245             equal(map.get(Exception.class), throwers.size());
 246             equal(map.get(weird.getClass()), throwers.size());
 247             equal(map.get(Error.class), throwers.size() + 1 + 2);
 248             equal(map.get(RuntimeException.class), throwers.size() + 1);
 249             equal(map.size(), 4);
 250         }
 251         equal(totalUncaughtExceptions.get(), 4L*throwers.size() + 4L);
 252 
 253         equal(beforeExecuteCount.get(), flakes.size());
 254         equal(afterExecuteCount.get(), throwers.size());
 255         equal(tpe.getCompletedTaskCount(), (long) flakes.size());
 256         equal(terminatedCount.get(), 1);
 257 
 258         // check for termination operation idempotence
 259         tpe.shutdown();
 260         tpe.shutdownNow();
 261         check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
 262         checkTerminated(tpe);
 263         equal(terminatedCount.get(), 1);
 264     }
 265 
 266     //--------------------- Infrastructure ---------------------------
 267     static volatile int passed = 0, failed = 0;
 268     static void pass() {passed++;}
 269     static void fail() {failed++; Thread.dumpStack();}
 270     static void fail(String msg) {System.out.println(msg); fail();}
 271     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 272     static void check(boolean cond) {if (cond) pass(); else fail();}
 273     static void equal(Object x, Object y) {
 274         if (x == null ? y == null : x.equals(y)) pass();
 275         else fail(x + " not equal to " + y);}
 276     public static void main(String[] args) throws Throwable {
 277         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 278         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 279         if (failed > 0) throw new AssertionError("Some tests failed");}
 280 }