Path: blob/master/test/hotspot/jtreg/runtime/ErrorHandling/TestOnOutOfMemoryError.java
40942 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test TestOnOutOfMemoryError25* @summary Test using single and multiple -XX:OnOutOfMemoryError=<cmd>26* @modules java.base/jdk.internal.misc27* @library /test/lib28* @run driver TestOnOutOfMemoryError29* @bug 8078470 817752230*/3132import jdk.test.lib.process.ProcessTools;33import jdk.test.lib.process.OutputAnalyzer;3435public class TestOnOutOfMemoryError {3637public static void main(String[] args) throws Exception {38if (args.length == 1) {39// This should guarantee to throw:40// java.lang.OutOfMemoryError: Requested array size exceeds VM limit41Object[] oa = new Object[Integer.MAX_VALUE];42return;43}4445// else this is the main test46String msg1 = "Test1 Succeeded";47String msg2 = "Test2 Succeeded";48ProcessBuilder pb_single = ProcessTools.createJavaProcessBuilder(49"-XX:OnOutOfMemoryError=echo " + msg1,50TestOnOutOfMemoryError.class.getName(),51"throwOOME");5253ProcessBuilder pb_multiple = ProcessTools.createJavaProcessBuilder(54"-XX:OnOutOfMemoryError=echo " + msg1,55"-XX:OnOutOfMemoryError=echo " + msg2,56TestOnOutOfMemoryError.class.getName(),57"throwOOME");5859OutputAnalyzer output_single = new OutputAnalyzer(pb_single.start());6061OutputAnalyzer output_multiple = new OutputAnalyzer(pb_multiple.start());6263/* Actual output should look like this:64#65# java.lang.OutOfMemoryError: Requested array size exceeds VM limit66# -XX:OnOutOfMemoryError="echo Test Succeeded"67# Executing /bin/sh -c "echo Test Succeeded"...68Test Succeeded69Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit70at OOME.main(OOME.java:3)7172So we don't want to match on the "# Executing ..." line, and they73both get written to stdout.74*/75output_single.shouldContain("Requested array size exceeds VM limit");76output_single.stdoutShouldMatch("^" + msg1); // match start of line only7778output_multiple.shouldContain("Requested array size exceeds VM limit");79output_multiple.stdoutShouldMatch("^" + msg1); // match start of line only80output_multiple.stdoutShouldMatch("^" + msg2); // match start of line only8182System.out.println("PASSED");83}84}858687