Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 1 | /* |
Andreas Lundblad | 49850dd | 2016-02-29 13:24:01 +0100 | [diff] [blame] | 2 | * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 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 |
Andreas Lundblad | 8d0ebb6 | 2015-08-26 09:02:02 +0200 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 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 8044131 |
| 27 | * @summary Tests the hooks used for detecting idleness of the sjavac server. |
Alexander Kulyakhtin | b626244 | 2015-05-21 11:41:04 -0700 | [diff] [blame] | 28 | * @modules jdk.compiler/com.sun.tools.sjavac.server |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 29 | * @build Wrapper |
| 30 | * @run main Wrapper IdleShutdown |
| 31 | */ |
Andreas Lundblad | ab159bb | 2015-09-04 13:24:15 +0200 | [diff] [blame] | 32 | import java.io.Writer; |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 33 | import java.util.concurrent.atomic.AtomicLong; |
| 34 | |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 35 | import com.sun.tools.sjavac.server.IdleResetSjavac; |
| 36 | import com.sun.tools.sjavac.server.Sjavac; |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 37 | import com.sun.tools.sjavac.server.Terminable; |
| 38 | |
| 39 | |
| 40 | public class IdleShutdown { |
| 41 | |
| 42 | final static long TEST_START = System.currentTimeMillis(); |
| 43 | final static long TIMEOUT_MS = 3000; |
| 44 | |
| 45 | public static void main(String[] args) throws InterruptedException { |
| 46 | |
| 47 | final AtomicLong timeoutTimestamp = new AtomicLong(-1); |
| 48 | |
| 49 | log("Starting IdleCallbackJavacService with timeout: " + TIMEOUT_MS); |
| 50 | Sjavac service = new IdleResetSjavac( |
| 51 | new NoopJavacService(), |
| 52 | new Terminable() { |
| 53 | public void shutdown(String msg) { |
| 54 | // Record the idle timeout time |
| 55 | log("Timeout detected"); |
| 56 | timeoutTimestamp.set(System.currentTimeMillis()); |
| 57 | } |
| 58 | }, |
| 59 | TIMEOUT_MS); |
| 60 | |
| 61 | // Make sure it didn't timeout immediately |
| 62 | if (timeoutTimestamp.get() != -1) |
| 63 | throw new AssertionError("Premature timeout detected."); |
| 64 | |
Andreas Lundblad | 3672dbc | 2015-08-25 15:14:41 +0200 | [diff] [blame] | 65 | // Use Sjavac object and wait less than TIMEOUT_MS in between calls |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 66 | Thread.sleep(TIMEOUT_MS - 1000); |
Andreas Lundblad | 3672dbc | 2015-08-25 15:14:41 +0200 | [diff] [blame] | 67 | log("Compiling"); |
Andreas Lundblad | 49850dd | 2016-02-29 13:24:01 +0100 | [diff] [blame] | 68 | service.compile(new String[0]); |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 69 | |
| 70 | Thread.sleep(TIMEOUT_MS - 1000); |
| 71 | log("Compiling"); |
Andreas Lundblad | 49850dd | 2016-02-29 13:24:01 +0100 | [diff] [blame] | 72 | service.compile(new String[0]); |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 73 | |
| 74 | if (timeoutTimestamp.get() != -1) |
| 75 | throw new AssertionError("Premature timeout detected."); |
| 76 | |
| 77 | long expectedTimeout = System.currentTimeMillis() + TIMEOUT_MS; |
| 78 | |
| 79 | // Wait for actual timeout |
| 80 | log("Awaiting idle timeout"); |
| 81 | Thread.sleep(TIMEOUT_MS + 1000); |
| 82 | |
| 83 | // Check result |
| 84 | if (timeoutTimestamp.get() == -1) |
| 85 | throw new AssertionError("Timeout never occurred"); |
| 86 | |
| 87 | long error = Math.abs(expectedTimeout - timeoutTimestamp.get()); |
| 88 | log("Timeout error: " + error + " ms"); |
| 89 | if (error > TIMEOUT_MS * .1) |
| 90 | throw new AssertionError("Error too big"); |
| 91 | |
| 92 | log("Shutting down"); |
| 93 | service.shutdown(); |
| 94 | } |
| 95 | |
| 96 | private static void log(String msg) { |
| 97 | long logTime = System.currentTimeMillis() - TEST_START; |
| 98 | System.out.printf("After %5d ms: %s%n", logTime, msg); |
| 99 | } |
| 100 | |
| 101 | private static class NoopJavacService implements Sjavac { |
| 102 | @Override |
Andreas Lundblad | 3672dbc | 2015-08-25 15:14:41 +0200 | [diff] [blame] | 103 | public void shutdown() { |
| 104 | } |
| 105 | @Override |
Andreas Lundblad | 49850dd | 2016-02-29 13:24:01 +0100 | [diff] [blame] | 106 | public int compile(String[] args) { |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 107 | // Attempt to trigger idle timeout during a call by sleeping |
| 108 | try { |
| 109 | Thread.sleep(TIMEOUT_MS + 1000); |
| 110 | } catch (InterruptedException e) { |
| 111 | } |
Andreas Lundblad | ab159bb | 2015-09-04 13:24:15 +0200 | [diff] [blame] | 112 | return 0; |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 113 | } |
Andreas Lundblad | ce4c456 | 2014-08-13 14:44:59 +0200 | [diff] [blame] | 114 | } |
| 115 | } |