Path: blob/master/test/jdk/java/lang/Thread/GenerifyStackTraces.java
41149 views
/*1* Copyright (c) 2004, 2013, 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* @test25* @bug 4919105 800417726* @summary Generified basic unit test of Thread.getAllStackTraces()27* @author Mandy Chung28*/2930import java.util.*;3132public class GenerifyStackTraces {3334private static Object go = new Object();35private static String[] methodNames = {"run", "A", "B", "C", "Done"};36private static int DONE_DEPTH = 5;37private static boolean testFailed = false;3839private static Thread one;40private static boolean trace = false;41public static void main(String[] args) throws Exception {42if (args.length > 0 && args[0].equals("trace")) {43trace = true;44}4546one = new ThreadOne();47one.start();4849DumpThread dt = new DumpThread();50dt.start();5152try {53one.join();54} finally {55dt.shutdown();56}5758if (testFailed) {59throw new RuntimeException("Test Failed.");60}61}6263static class DumpThread extends Thread {64private volatile boolean finished = false;6566public void run() {67int depth = 2;68while (!finished) {69// At each iterator, wait until ThreadOne blocks70// to wait for thread dump.71// Then dump stack trace and notify ThreadOne to continue.72try {73sleep(2000);74dumpStacks(depth);75depth++;76finishDump();77} catch (Exception e) {78e.printStackTrace();79testFailed = true;80}81}82}8384public void shutdown() throws InterruptedException {85finished = true;86this.join();87}88}8990static class ThreadOne extends Thread {91public void run() {92A();93}94private void A() {95waitForDump();96B();97}98private void B() {99waitForDump();100C();101}102private void C() {103waitForDump();104Done();105}106private void Done() {107waitForDump();108109// Get stack trace of current thread110StackTraceElement[] stack = getStackTrace();111try {112checkStack(this, stack, DONE_DEPTH);113} catch (Exception e) {114e.printStackTrace();115testFailed = true;116}117}118119}120121122private static void waitForDump() {123synchronized(go) {124try {125go.wait();126} catch (Exception e) {127throw new RuntimeException("Unexpected exception" + e);128}129}130}131132private static void finishDump() {133synchronized(go) {134try {135go.notifyAll();136} catch (Exception e) {137throw new RuntimeException("Unexpected exception" + e);138}139}140}141142public static void dumpStacks(int depth) throws Exception {143// Get stack trace of another thread144StackTraceElement[] stack = one.getStackTrace();145checkStack(one, stack, depth);146147148// Get stack traces of all Threads149for (Map.Entry<Thread, StackTraceElement[]> entry :150Thread.getAllStackTraces().entrySet()) {151Thread t = entry.getKey();152stack = entry.getValue();153if (t == null || stack == null) {154throw new RuntimeException("Null thread or stacktrace returned");155}156if (t == one) {157checkStack(t, stack, depth);158}159}160}161162private static void checkStack(Thread t, StackTraceElement[] stack,163int depth) throws Exception {164if (trace) {165printStack(t, stack);166}167int frame = stack.length - 1;168for (int i = 0; i < depth && frame >= 0; i++) {169if (! stack[frame].getMethodName().equals(methodNames[i])) {170throw new RuntimeException("Expected " + methodNames[i] +171" in frame " + frame + " but got " +172stack[frame].getMethodName());173}174frame--;175}176}177178private static void printStack(Thread t, StackTraceElement[] stack) {179System.out.println(t +180" stack: (length = " + stack.length + ")");181if (t != null) {182for (int j = 0; j < stack.length; j++) {183System.out.println(stack[j]);184}185System.out.println();186}187}188}189190191