Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/MlvmOOMTest.java
41155 views
/*1* Copyright (c) 2010, 2018, 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*/2223package vm.mlvm.share;2425import java.util.LinkedList;26import java.util.List;2728/**29* The base class for Mlvm tests checking various OOM Errors.30* Subclasses are expected to implement {@link #eatMemory(List)}/31* {@link checkOOME(OutOfMemoryError)} methods consuming memory in various ways.32*/33public abstract class MlvmOOMTest extends MlvmTest {34private static Object garbage;3536/**37* A template method.38* Implements logic of the tests:39* consumes memory in loop until OOM is thrown, checks the OOM type.40*/41@Override42public final boolean run() {43Env.display("Test started.");44LinkedList<Object> objects = new LinkedList<Object>();45// to trick EA46garbage = objects;47try {48eatMemory(objects);49} catch (OutOfMemoryError oome) {50objects.clear();51Env.display("Caught OOME : " + oome.getMessage());52checkOOME(oome);53return true;54}55throw new RuntimeException("TEST FAIL : no OOME");56}5758/**59* Consumes memory.60* Subclasses implement their logic of memory consuming. Created objects are expected61* to be stored in the given parameter.62* The normal termination of the method is throwing OOM exception, which will be checked63* by the {@link #checkOOME(OutOfMemoryError)}64* Not throwing the OOM will be interpreted as test failure.65* @param garbage a list to store generated garbage66*/67protected abstract void eatMemory(List<Object> garbage);6869/**70* Checks the OOME type is expected.71* Method just exits if OOME is expected and throws an exeption if not.72* @param oome thrown by {@link #eatMemory(List)}73*/74protected abstract void checkOOME(OutOfMemoryError oome);75}767778