Path: blob/master/test/hotspot/jtreg/runtime/ErrorHandling/TimeoutInErrorHandlingTest.java
40942 views
/*1* Copyright (c) 2017, 2020, 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*/2223import java.io.BufferedReader;24import java.io.File;25import java.io.FileInputStream;26import java.io.InputStreamReader;27import java.util.regex.Pattern;2829import jdk.test.lib.process.OutputAnalyzer;30import jdk.test.lib.Platform;31import jdk.test.lib.process.ProcessTools;3233/*34* @test35* @bug 816694436* @summary Hanging Error Reporting steps may lead to torn error logs37* @modules java.base/jdk.internal.misc38* @library /test/lib39* @requires (vm.debug == true) & (os.family != "windows")40* @run driver TimeoutInErrorHandlingTest41* @author Thomas Stuefe (SAP)42*/4344public class TimeoutInErrorHandlingTest {4546public static final boolean verbose = System.getProperty("verbose") != null;47// 16 seconds for hs_err generation timeout = 4 seconds per step timeout48public static final int ERROR_LOG_TIMEOUT = 16;4950public static void main(String[] args) throws Exception {51/* Start the VM and let it crash. Specify TestUnresponsiveErrorHandler which will52* let five subsequent error reporting steps hang. The Timeout handling triggered53* by the WatcherThread should kick in and interrupt those steps. In theory, the54* text "timeout occurred during error reporting in step .." (the little timeouts)55* should occur in the error log up to four times, followed by the final big timeout56* "------ Timeout during error reporting after xx s. ------"57*58* Note that there are a number of uncertainties which make writing a 100% foolproof59* test challenging. The time the error reporting thread takes to react to the60* timeout triggers is unknown. So it is difficult to predict how many little timeouts61* will be visible before the big timeout kicks in. Also, once the big timeout hits,62* error reporting thread and Watcherthread will race. The former writes his last63* message to the error logs and flushes, the latter waits 200ms and then exits the64* process without further synchronization with the error reporting thread.65*66* Because of all this and the desire to write a bullet proof test which does67* not fail sporadically, we will not test for the final timeout message nor for all68* of the optimally expected little timeout messages. We just test for two of the69* little timeout messages to see that repeated timeout handling is basically working.70*/7172ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(73"-XX:+UnlockDiagnosticVMOptions",74"-Xmx100M",75"-XX:ErrorHandlerTest=14",76"-XX:+TestUnresponsiveErrorHandler",77"-XX:ErrorLogTimeout=" + ERROR_LOG_TIMEOUT,78"-XX:-CreateCoredumpOnCrash",79"-version");8081OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());8283if (verbose) {84System.err.println("<begin cmd output>");85System.err.println(output_detail.getOutput());86System.err.println("<end cmd output>");87}8889// we should have crashed with a SIGSEGV90output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");91output_detail.shouldMatch("# +(?:SIGSEGV|SIGBUS|EXCEPTION_ACCESS_VIOLATION).*");9293// VM should have been aborted by WatcherThread94output_detail.shouldMatch(".*timer expired, abort.*");9596// extract hs-err file97String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);98if (hs_err_file == null) {99if (!verbose) {100System.err.println("<begin cmd output>");101System.err.println(output_detail.getOutput());102System.err.println("<end cmd output>");103}104throw new RuntimeException("Did not find hs-err file in output.\n");105}106107File f = new File(hs_err_file);108if (!f.exists()) {109if (!verbose) {110System.err.println("<begin cmd output>");111System.err.println(output_detail.getOutput());112System.err.println("<end cmd output>");113}114throw new RuntimeException("hs-err file missing at "115+ f.getAbsolutePath() + ".\n");116}117118System.out.println("Found hs_err file. Scanning...");119120FileInputStream fis = new FileInputStream(f);121BufferedReader br = new BufferedReader(new InputStreamReader(fis));122String line = null;123124125Pattern [] pattern = new Pattern[] {126Pattern.compile(".*timeout occurred during error reporting in step.*"),127Pattern.compile(".*timeout occurred during error reporting in step.*")128};129int currentPattern = 0;130131String lastLine = null;132StringBuilder saved_hs_err = new StringBuilder();133while ((line = br.readLine()) != null) {134saved_hs_err.append(line + System.lineSeparator());135if (currentPattern < pattern.length) {136if (pattern[currentPattern].matcher(line).matches()) {137System.out.println("Found: " + line + ".");138currentPattern ++;139}140}141lastLine = line;142}143br.close();144145if (verbose) {146System.err.println("<begin hs_err contents>");147System.err.print(saved_hs_err);148System.err.println("<end hs_err contents>");149}150151if (currentPattern < pattern.length) {152if (!verbose) {153System.err.println("<begin hs_err contents>");154System.err.print(saved_hs_err);155System.err.println("<end hs_err contents>");156}157throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPattern + ")");158}159160System.out.println("OK.");161162}163164}165166167