TestThreadStackTrace.sh 4.4 KB
Newer Older
Y
yunyao.zxl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/usr/bin/env bash

#
# Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Alibaba designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#

#
# @test
# @summary test Thread.getStackTrace() in wisp transparentAsync model
# @requires os.family == "linux"
# @run shell TestThreadStackTrace.sh
#

if [ "${TESTSRC}" = "" ]
then
    TESTSRC=${PWD}
    echo "TESTSRC not set.  Using "${TESTSRC}" as default"
fi
echo "TESTSRC=${TESTSRC}"
# determine platform dependant variables
OS=`uname -s`
case ${OS} in
  Linux)
    FS=/
    ;;
  *)
    exit 1
    ;;
esac

JAVA=${TESTJAVA}${FS}bin${FS}java
JAVAC=${TESTJAVA}${FS}bin${FS}javac
TEST_CLASS=TmpThreadStackTrace
TEST_SOURCE=${TEST_CLASS}.java

###################################################################################


cat > ${TESTCLASSES}${FS}$TEST_SOURCE << EOF
import com.alibaba.wisp.engine.WispEngine;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import sun.misc.SharedSecrets;
import java.util.concurrent.Executors;
import java.util.concurrent.*;

public class TmpThreadStackTrace {

    public static Thread runningCoroutine;

    public static void foo() throws Exception {
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute(() -> {
            try {
                runningCoroutine = Thread.currentThread();
                Thread.sleep(2_000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        AtomicBoolean result = new AtomicBoolean(true);
        CountDownLatch done = new CountDownLatch(1);
        Thread mainThread = Thread.currentThread();
        Thread[] ts = new Thread[1];
        ts[0] = new Thread(() -> {
            System.out.println("in managed!");
            try {
                if (ts[0].getStackTrace().length == 0 || mainThread.getStackTrace().length == 0
                        || Thread.currentThread().getStackTrace().length == 0) {
                    result.set(false);
                }
                runningCoroutine.getStackTrace();
            } catch (Exception e) {
91
              result.set(false);
Y
yunyao.zxl 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            } finally {
                done.countDown();
            }
        }, "test-thread");
        ts[0].start();
        done.await();
        System.out.println("in main");
        executor.shutdown();
        if (!result.get()) {
            throw new Error("test failure");
        }
    }

    public static void main(String[] args) throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        WispEngine.current().execute(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println(i);
                Thread.currentThread().getStackTrace();
            }
            latch.countDown();
        });
        latch.await();
        foo();
        System.out.println("done");
        System.exit(0);
    }
}
EOF

# Do compilation
${JAVAC} -cp ${TESTCLASSES} -d ${TESTCLASSES} ${TESTCLASSES}${FS}$TEST_SOURCE >> /dev/null 2>&1
if [ $? != '0' ]
then
	printf "Failed to compile ${TESTCLASSES}${FS}${TEST_SOURCE}"
	exit 1
fi

#run
131
${JAVA} -XX:+PrintSafepointStatistics  -XX:PrintSafepointStatisticsCount=1 -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dcom.alibaba.wisp.carrierEngines=1 -cp ${TESTCLASSES} ${TEST_CLASS} > output.txt  2>&1
Y
yunyao.zxl 已提交
132 133 134 135 136 137
cat output.txt

function assert()
{
    line=`cat output.txt | grep ThreadDump | wc -l`
    echo $line
138
    if [[ $line -eq "3" ]]; then
Y
yunyao.zxl 已提交
139 140 141 142 143 144 145 146
        echo "success"
    else
        echo "failure"
        exit -1
    fi
}

assert