1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 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 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /**
  25  * @test TestThreadFailure
  26  * @summary Test OOME in separate thread is recoverable
  27  * @library /test/lib
  28  * @run main TestThreadFailure
  29  */
  30 
  31 import java.util.*;
  32 import jdk.test.lib.process.OutputAnalyzer;
  33 import jdk.test.lib.process.ProcessTools;
  34 
  35 public class TestThreadFailure {
  36 
  37     static final int SIZE  = 1024;
  38     static final int COUNT = 16;
  39 
  40     static class NastyThread extends Thread {
  41         @Override
  42         public void run() {
  43             List<Object> root = new ArrayList<Object>();
  44             while (true) {
  45                 root.add(new Object[SIZE]);
  46             }
  47         }
  48     }
  49 
  50     public static void main(String[] args) throws Exception {
  51         if (args.length > 0) {
  52             for (int t = 0; t < COUNT; t++) {
  53                Thread thread = new NastyThread();
  54                thread.start();
  55                thread.join();
  56             }
  57             System.out.println("All good");
  58             return;
  59         }
  60 
  61         {
  62             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  63                                     "-Xmx16m",
  64                                     "-XX:+UnlockExperimentalVMOptions",
  65                                     "-XX:+UseShenandoahGC",
  66                                     TestThreadFailure.class.getName(),
  67                                     "test");
  68 
  69             OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  70             analyzer.shouldHaveExitValue(0);
  71             analyzer.shouldContain("java.lang.OutOfMemoryError");
  72             analyzer.shouldContain("All good");
  73         }
  74 
  75         {
  76             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  77                                     "-Xmx128m",
  78                                     "-XX:+UnlockExperimentalVMOptions",
  79                                     "-XX:+UseShenandoahGC",
  80                                     TestThreadFailure.class.getName(),
  81                                     "test");
  82 
  83             OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  84             analyzer.shouldHaveExitValue(0);
  85             analyzer.shouldContain("java.lang.OutOfMemoryError");
  86             analyzer.shouldContain("All good");
  87         }
  88     }
  89 }