IdleShutdown.java revision 3022:5ba1a29a0eb0
1/*
2 * Copyright (c) 2014, 2015, 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 */
23
24/*
25 * @test
26 * @bug 8044131
27 * @summary Tests the hooks used for detecting idleness of the sjavac server.
28 * @modules jdk.compiler/com.sun.tools.sjavac.server
29 * @build Wrapper
30 * @run main Wrapper IdleShutdown
31 */
32import java.io.Writer;
33import java.util.concurrent.atomic.AtomicLong;
34
35import com.sun.tools.sjavac.server.IdleResetSjavac;
36import com.sun.tools.sjavac.server.Sjavac;
37import 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
65        // Use Sjavac object and wait less than TIMEOUT_MS in between calls
66        Thread.sleep(TIMEOUT_MS - 1000);
67        log("Compiling");
68        service.compile(new String[0], null, null);
69
70        Thread.sleep(TIMEOUT_MS - 1000);
71        log("Compiling");
72        service.compile(new String[0], null, null);
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
103        public void shutdown() {
104        }
105        @Override
106        public int compile(String[] args, Writer out, Writer err) {
107            // Attempt to trigger idle timeout during a call by sleeping
108            try {
109                Thread.sleep(TIMEOUT_MS + 1000);
110            } catch (InterruptedException e) {
111            }
112            return 0;
113        }
114    }
115}
116