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