Wed, 23 Jan 2013 13:27:24 -0800
8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com
1 /*
2 * Copyright (c) 2010, 2013, 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 */
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ThreadFactory;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicInteger;
31 import javax.tools.JavaCompiler;
32 import javax.tools.StandardJavaFileManager;
33 import javax.tools.ToolProvider;
35 /**
36 * An abstract superclass for threaded tests.
37 *
38 * This class will try to read a property named test.concurrency.
39 * The property can be provided by passing this option to jtreg:
40 * -javaoption:-Dtest.concurrency=#
41 *
42 * If the property is not set the class will use a heuristic to determine the
43 * maximum number of threads that can be fired to execute a given test.
44 */
45 public abstract class JavacTestingAbstractThreadedTest {
47 protected static int getThreadPoolSize() {
48 Integer testConc = Integer.getInteger("test.concurrency");
49 if (testConc != null) return testConc;
50 int cores = Runtime.getRuntime().availableProcessors();
51 return Math.max(2, Math.min(8, cores / 2));
52 }
54 protected static void checkAfterExec() throws InterruptedException {
55 checkAfterExec(true);
56 };
58 protected static boolean throwAssertionOnError = true;
60 protected static boolean printAll = false;
62 protected static StringWriter errSWriter = new StringWriter();
63 protected static PrintWriter errWriter = new PrintWriter(errSWriter);
65 protected static StringWriter outSWriter = new StringWriter();
66 protected static PrintWriter outWriter = new PrintWriter(outSWriter);
68 protected static void checkAfterExec(boolean printCheckCount)
69 throws InterruptedException {
70 pool.shutdown();
71 pool.awaitTermination(15, TimeUnit.MINUTES);
72 if (errCount.get() > 0) {
73 if (throwAssertionOnError) {
74 closePrinters();
75 System.err.println(errSWriter.toString());
76 throw new AssertionError(
77 String.format("%d errors found", errCount.get()));
78 } else {
79 System.err.println(
80 String.format("%d errors found", errCount.get()));
81 }
82 } else if (printCheckCount) {
83 outWriter.println("Total check executed: " + checkCount.get());
84 }
85 closePrinters();
86 if (printAll) {
87 System.out.println(errSWriter.toString());
88 System.out.println(outSWriter.toString());
89 }
90 }
92 protected static void closePrinters() {
93 errWriter.close();
94 outWriter.close();
95 }
97 protected static void processException(Throwable t) {
98 errCount.incrementAndGet();
99 t.printStackTrace(errWriter);
100 pool.shutdown();
101 }
103 //number of checks executed
104 protected static AtomicInteger checkCount = new AtomicInteger();
106 //number of errors found while running combo tests
107 protected static AtomicInteger errCount = new AtomicInteger();
109 //create default shared JavaCompiler - reused across multiple compilations
110 protected static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
112 protected static ExecutorService pool = Executors.newFixedThreadPool(
113 getThreadPoolSize(), new ThreadFactory() {
114 @Override
115 public Thread newThread(Runnable r) {
116 Thread t = new Thread(r);
117 t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
118 @Override
119 public void uncaughtException(Thread t, Throwable e) {
120 pool.shutdown();
121 errCount.incrementAndGet();
122 e.printStackTrace(System.err);
123 }
124 });
125 return t;
126 }
127 });
129 /*
130 * File manager is not thread-safe so it cannot be re-used across multiple
131 * threads. However we cache per-thread FileManager to avoid excessive
132 * object creation
133 */
134 protected static final ThreadLocal<StandardJavaFileManager> fm =
135 new ThreadLocal<StandardJavaFileManager>() {
136 @Override protected StandardJavaFileManager initialValue() {
137 return comp.getStandardFileManager(null, null, null);
138 }
139 };
141 }