Path: blob/master/test/langtools/tools/sjavac/PooledExecution.java
41144 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804413126* @summary Makes sure sjavac poolsize option is honored.27* @modules jdk.compiler/com.sun.tools.javac.main28* jdk.compiler/com.sun.tools.sjavac.comp29* jdk.compiler/com.sun.tools.sjavac.server30* @build Wrapper31* @run main Wrapper PooledExecution32*/3334import java.util.concurrent.CountDownLatch;35import java.util.concurrent.atomic.AtomicInteger;3637import com.sun.tools.javac.main.Main.Result;38import com.sun.tools.sjavac.comp.PooledSjavac;39import com.sun.tools.sjavac.server.Sjavac;404142public class PooledExecution {4344public static void main(String[] args) throws InterruptedException {45new PooledExecutionTest().runTest();46}4748static class PooledExecutionTest {4950final int POOL_SIZE = 15;51final int NUM_REQUESTS = 100;5253// Number of tasks that has not yet started54CountDownLatch leftToStart = new CountDownLatch(NUM_REQUESTS);5556// Highest number of concurrently active request seen57int highWaterMark = 0;5859public void runTest() throws InterruptedException {60ConcurrencyLoggingService loggingService = new ConcurrencyLoggingService();61final Sjavac service = new PooledSjavac(loggingService, POOL_SIZE);6263// Keep track of the number of finished tasks so we can make sure all64// tasks finishes gracefully upon shutdown.65Thread[] tasks = new Thread[NUM_REQUESTS];66final AtomicInteger tasksFinished = new AtomicInteger(0);6768for (int i = 0; i < NUM_REQUESTS; i++) {69tasks[i] = new Thread() {70public void run() {71service.compile(new String[0]);72tasksFinished.incrementAndGet();73}74};75tasks[i].start();76}7778// Wait for all tasks to start (but not necessarily run to completion)79leftToStart.await();8081// Shutdown before all tasks are completed82System.out.println("Shutting down!");83service.shutdown();8485// Wait for all tasks to complete86for (Thread t : tasks)87t.join();8889if (tasksFinished.get() != NUM_REQUESTS) {90throw new AssertionError(tasksFinished.get() + " out of " +91NUM_REQUESTS + " finished. Broken shutdown?");92}9394if (highWaterMark > POOL_SIZE) {95throw new AssertionError("Pool size overused: " + highWaterMark +96" used out of " + POOL_SIZE + " allowed.");97}9899// Assuming more than POOL_SIZE requests can be processed within 1 sek:100if (highWaterMark < POOL_SIZE) {101throw new AssertionError("Pool size underused: " + highWaterMark +102" used out of " + POOL_SIZE + " allowed.");103}104}105106107private class ConcurrencyLoggingService implements Sjavac {108109// Keeps track of currently active requests110AtomicInteger activeRequests = new AtomicInteger(0);111112@Override113public Result compile(String[] args) {114leftToStart.countDown();115int numActiveRequests = activeRequests.incrementAndGet();116System.out.printf("Left to start: %2d / Currently active: %2d%n",117leftToStart.getCount(),118numActiveRequests);119highWaterMark = Math.max(highWaterMark, numActiveRequests);120try {121Thread.sleep(1000);122} catch (InterruptedException ie) {123throw new RuntimeException("Interrupted", ie);124}125activeRequests.decrementAndGet();126System.out.println("Task completed");127return Result.OK;128}129130@Override131public void shutdown() {132}133}134}135}136137138