test/langtools/tools/sjavac/IdleShutdown.java
author vlivanov
Tue, 26 Nov 2019 16:09:21 +0300
changeset 59276 94a84abb873b
parent 55731 ee23bd3192f7
permissions -rw-r--r--
8234403: C2: Enable CallSite.target updates in constructors Reviewed-by: psandoz, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     1
/*
55731
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     4
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32337
c9d3ab9f601c 8133671: langtools tests have bad license
alundblad
parents: 32335
diff changeset
     7
 * published by the Free Software Foundation.
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     8
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    13
 * accompanied this code).
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    14
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    18
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    21
 * questions.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    22
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    23
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    24
/*
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    25
 * @test
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    26
 * @bug 8044131
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    27
 * @summary Tests the hooks used for detecting idleness of the sjavac server.
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37643
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.main
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37643
diff changeset
    29
 *          jdk.compiler/com.sun.tools.sjavac.server
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    30
 * @build Wrapper
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    31
 * @run main Wrapper IdleShutdown
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    32
 */
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37643
diff changeset
    33
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicLong;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    35
37643
626e07816dce 8048146: sjavac uses unexpected exit code of -1
alundblad
parents: 36161
diff changeset
    36
import com.sun.tools.javac.main.Main.Result;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    37
import com.sun.tools.sjavac.server.IdleResetSjavac;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    38
import com.sun.tools.sjavac.server.Sjavac;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    39
import com.sun.tools.sjavac.server.Terminable;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    40
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    41
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    42
public class IdleShutdown {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    43
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    44
    final static long TEST_START = System.currentTimeMillis();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    45
    final static long TIMEOUT_MS = 3000;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    46
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    47
    public static void main(String[] args) throws InterruptedException {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    48
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    49
        final AtomicLong timeoutTimestamp = new AtomicLong(-1);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    50
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    51
        log("Starting IdleCallbackJavacService with timeout: " + TIMEOUT_MS);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    52
        Sjavac service = new IdleResetSjavac(
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    53
                new NoopJavacService(),
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    54
                new Terminable() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    55
                    public void shutdown(String msg) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    56
                        // Record the idle timeout time
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    57
                        log("Timeout detected");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    58
                        timeoutTimestamp.set(System.currentTimeMillis());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    59
                    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    60
                },
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    61
                TIMEOUT_MS);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    62
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    63
        // Make sure it didn't timeout immediately
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    64
        if (timeoutTimestamp.get() != -1)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    65
            throw new AssertionError("Premature timeout detected.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    66
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 30730
diff changeset
    67
        // Use Sjavac object and wait less than TIMEOUT_MS in between calls
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    68
        Thread.sleep(TIMEOUT_MS - 1000);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 30730
diff changeset
    69
        log("Compiling");
36161
a025c0619f25 8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents: 32542
diff changeset
    70
        service.compile(new String[0]);
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    71
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    72
        Thread.sleep(TIMEOUT_MS - 1000);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    73
        log("Compiling");
36161
a025c0619f25 8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents: 32542
diff changeset
    74
        service.compile(new String[0]);
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    75
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    76
        if (timeoutTimestamp.get() != -1)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    77
            throw new AssertionError("Premature timeout detected.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    78
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    79
        long expectedTimeout = System.currentTimeMillis() + TIMEOUT_MS;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    80
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    81
        // Wait for actual timeout
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    82
        log("Awaiting idle timeout");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    83
        Thread.sleep(TIMEOUT_MS + 1000);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    84
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    85
        // Check result
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    86
        if (timeoutTimestamp.get() == -1)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    87
            throw new AssertionError("Timeout never occurred");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    88
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    89
        long error = Math.abs(expectedTimeout - timeoutTimestamp.get());
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    90
        log("Timeout error: " + error + " ms");
55731
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    91
        String timeoutFactorText = System.getProperty("test.timeout.factor", "1.0");
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    92
        double timeoutFactor = Double.parseDouble(timeoutFactorText);
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    93
        double allowedError = TIMEOUT_MS * 0.1 * timeoutFactor;
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    94
        if (error > allowedError) {
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    95
            throw new AssertionError("Timeout error too large, error is " + error +
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    96
                                     " milliseconds, we allowed " + allowedError + " milliseconds");
ee23bd3192f7 8227247: tools/sjavac/IdleShutdown.java fails with AssertionError: Error too big on windows
mbaesken
parents: 47216
diff changeset
    97
        }
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    98
        log("Shutting down");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    99
        service.shutdown();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   100
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   101
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   102
    private static void log(String msg) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   103
        long logTime = System.currentTimeMillis() - TEST_START;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   104
        System.out.printf("After %5d ms: %s%n", logTime, msg);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   105
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   106
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   107
    private static class NoopJavacService implements Sjavac {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   108
        @Override
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 30730
diff changeset
   109
        public void shutdown() {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 30730
diff changeset
   110
        }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 30730
diff changeset
   111
        @Override
37643
626e07816dce 8048146: sjavac uses unexpected exit code of -1
alundblad
parents: 36161
diff changeset
   112
        public Result compile(String[] args) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   113
            // Attempt to trigger idle timeout during a call by sleeping
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   114
            try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   115
                Thread.sleep(TIMEOUT_MS + 1000);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   116
            } catch (InterruptedException e) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   117
            }
37643
626e07816dce 8048146: sjavac uses unexpected exit code of -1
alundblad
parents: 36161
diff changeset
   118
            return Result.OK;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   119
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   120
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   121
}