Path: blob/master/test/langtools/tools/sjavac/IdleShutdown.java
41144 views
/*1* Copyright (c) 2014, 2019, 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 Tests the hooks used for detecting idleness of the sjavac server.27* @modules jdk.compiler/com.sun.tools.javac.main28* jdk.compiler/com.sun.tools.sjavac.server29* @build Wrapper30* @run main Wrapper IdleShutdown31*/3233import java.util.concurrent.atomic.AtomicLong;3435import com.sun.tools.javac.main.Main.Result;36import com.sun.tools.sjavac.server.IdleResetSjavac;37import com.sun.tools.sjavac.server.Sjavac;38import com.sun.tools.sjavac.server.Terminable;394041public class IdleShutdown {4243final static long TEST_START = System.currentTimeMillis();44final static long TIMEOUT_MS = 3000;4546public static void main(String[] args) throws InterruptedException {4748final AtomicLong timeoutTimestamp = new AtomicLong(-1);4950log("Starting IdleCallbackJavacService with timeout: " + TIMEOUT_MS);51Sjavac service = new IdleResetSjavac(52new NoopJavacService(),53new Terminable() {54public void shutdown(String msg) {55// Record the idle timeout time56log("Timeout detected");57timeoutTimestamp.set(System.currentTimeMillis());58}59},60TIMEOUT_MS);6162// Make sure it didn't timeout immediately63if (timeoutTimestamp.get() != -1)64throw new AssertionError("Premature timeout detected.");6566// Use Sjavac object and wait less than TIMEOUT_MS in between calls67Thread.sleep(TIMEOUT_MS - 1000);68log("Compiling");69service.compile(new String[0]);7071Thread.sleep(TIMEOUT_MS - 1000);72log("Compiling");73service.compile(new String[0]);7475if (timeoutTimestamp.get() != -1)76throw new AssertionError("Premature timeout detected.");7778long expectedTimeout = System.currentTimeMillis() + TIMEOUT_MS;7980// Wait for actual timeout81log("Awaiting idle timeout");82Thread.sleep(TIMEOUT_MS + 1000);8384// Check result85if (timeoutTimestamp.get() == -1)86throw new AssertionError("Timeout never occurred");8788long error = Math.abs(expectedTimeout - timeoutTimestamp.get());89log("Timeout error: " + error + " ms");90String timeoutFactorText = System.getProperty("test.timeout.factor", "1.0");91double timeoutFactor = Double.parseDouble(timeoutFactorText);92double allowedError = TIMEOUT_MS * 0.1 * timeoutFactor;93if (error > allowedError) {94throw new AssertionError("Timeout error too large, error is " + error +95" milliseconds, we allowed " + allowedError + " milliseconds");96}97log("Shutting down");98service.shutdown();99}100101private static void log(String msg) {102long logTime = System.currentTimeMillis() - TEST_START;103System.out.printf("After %5d ms: %s%n", logTime, msg);104}105106private static class NoopJavacService implements Sjavac {107@Override108public void shutdown() {109}110@Override111public Result compile(String[] args) {112// Attempt to trigger idle timeout during a call by sleeping113try {114Thread.sleep(TIMEOUT_MS + 1000);115} catch (InterruptedException e) {116}117return Result.OK;118}119}120}121122123