blob: 7178d6c89e59bbf366937660c95cc3f1af13b79f [file] [log] [blame]
Andreas Lundbladce4c4562014-08-13 14:44:59 +02001/*
Andreas Lundblad49850dd2016-02-29 13:24:01 +01002 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
Andreas Lundbladce4c4562014-08-13 14:44:59 +02003 * 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 Lundblad8d0ebb62015-08-26 09:02:02 +02007 * published by the Free Software Foundation.
Andreas Lundbladce4c4562014-08-13 14:44:59 +02008 *
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 Kulyakhtinb6262442015-05-21 11:41:04 -070028 * @modules jdk.compiler/com.sun.tools.sjavac.server
Andreas Lundbladce4c4562014-08-13 14:44:59 +020029 * @build Wrapper
30 * @run main Wrapper IdleShutdown
31 */
Andreas Lundbladab159bb2015-09-04 13:24:15 +020032import java.io.Writer;
Andreas Lundbladce4c4562014-08-13 14:44:59 +020033import java.util.concurrent.atomic.AtomicLong;
34
Andreas Lundbladce4c4562014-08-13 14:44:59 +020035import com.sun.tools.sjavac.server.IdleResetSjavac;
36import com.sun.tools.sjavac.server.Sjavac;
Andreas Lundbladce4c4562014-08-13 14:44:59 +020037import com.sun.tools.sjavac.server.Terminable;
38
39
40public 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 Lundblad3672dbc2015-08-25 15:14:41 +020065 // Use Sjavac object and wait less than TIMEOUT_MS in between calls
Andreas Lundbladce4c4562014-08-13 14:44:59 +020066 Thread.sleep(TIMEOUT_MS - 1000);
Andreas Lundblad3672dbc2015-08-25 15:14:41 +020067 log("Compiling");
Andreas Lundblad49850dd2016-02-29 13:24:01 +010068 service.compile(new String[0]);
Andreas Lundbladce4c4562014-08-13 14:44:59 +020069
70 Thread.sleep(TIMEOUT_MS - 1000);
71 log("Compiling");
Andreas Lundblad49850dd2016-02-29 13:24:01 +010072 service.compile(new String[0]);
Andreas Lundbladce4c4562014-08-13 14:44:59 +020073
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 Lundblad3672dbc2015-08-25 15:14:41 +0200103 public void shutdown() {
104 }
105 @Override
Andreas Lundblad49850dd2016-02-29 13:24:01 +0100106 public int compile(String[] args) {
Andreas Lundbladce4c4562014-08-13 14:44:59 +0200107 // Attempt to trigger idle timeout during a call by sleeping
108 try {
109 Thread.sleep(TIMEOUT_MS + 1000);
110 } catch (InterruptedException e) {
111 }
Andreas Lundbladab159bb2015-09-04 13:24:15 +0200112 return 0;
Andreas Lundbladce4c4562014-08-13 14:44:59 +0200113 }
Andreas Lundbladce4c4562014-08-13 14:44:59 +0200114 }
115}