test/jdk/java/lang/StackWalker/AcrossThreads.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 34372 jdk/test/java/lang/StackWalker/AcrossThreads.java@ccdd9223ab7a
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * accompanied this code).
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * questions.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
 * @test
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
 * @bug 8143214
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    27
 * @summary Verify that StackWalker works well when one instance of StackWalker
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
 *          is used by several threads sequentially or concurrently.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
 * @run testng AcrossThreads
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
import java.util.ArrayList;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.util.EnumSet;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
import java.util.HashMap;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
import java.util.List;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
import java.util.Map;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
import java.lang.StackWalker.StackFrame;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
import static java.lang.StackWalker.Option.*;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
import org.testng.annotations.*;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
import static org.testng.Assert.*;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
public class AcrossThreads {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
    static final StackWalker WALKERS[] = new StackWalker[] {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
            StackWalker.getInstance(RETAIN_CLASS_REFERENCE),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
            StackWalker.getInstance(EnumSet.of(SHOW_REFLECT_FRAMES, RETAIN_CLASS_REFERENCE)),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
            StackWalker.getInstance(EnumSet.of(SHOW_HIDDEN_FRAMES, RETAIN_CLASS_REFERENCE))
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    49
    };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    50
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
    @DataProvider
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    52
    public StackWalker[][] walkerProvider() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
        return new StackWalker[][] {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
                new StackWalker[] { WALKERS[0] },
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
                new StackWalker[] { WALKERS[1] },
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
                new StackWalker[] { WALKERS[2] }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    57
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    58
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    59
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    60
    @Test(dataProvider = "walkerProvider")
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
    public void test(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
        Thread t1 = new T1(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
        // call methods of one instance of StackWalker sequentially in T1, T2, T3.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
        t1.start();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    65
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    66
            t1.join();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    67
        } catch (InterruptedException e) { }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    68
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
        List<Thread> threads = new ArrayList<Thread>();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
        for (int i = 0; i < 100; i++) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    71
            threads.add(new T1(walker));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    72
            threads.add(new T2(walker));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    73
            threads.add(new T3(walker));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
        // call methods of one instance of StackWalker concurrently in several threads.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    76
        threads.parallelStream().forEach(t -> {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
            t.setDaemon(true);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
            t.start();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
        });
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    80
        threads.parallelStream().forEach(t -> {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    81
            try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    82
                t.join();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    83
            } catch (InterruptedException e) { }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    84
        });
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    85
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    86
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    87
    interface Consumer {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    88
        final int LOOPS = 5;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
        public void consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    91
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    92
        default public void assertWalker(StackWalker walker, int n) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    93
            if (--n == 0) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
                Map<String, Integer> methods = new HashMap<String, Integer>();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    95
                walker.forEach(f -> {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    96
                    Integer i = methods.putIfAbsent(f.getMethodName(), 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
                    if (i != null) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
                        methods.put(f.getMethodName(), i + 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
                    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
                });
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
                // verify that walker.forEach(...) reaches the specified methods.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   103
                assertTrue(methods.get("consume") == 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
                assertTrue(methods.get("run") == 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   105
                assertTrue(methods.get("assertWalker") == LOOPS);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   106
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   107
                // verify that walker.walk(...) reaches the specified methods.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   108
                assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   109
                                             .filter(mn -> mn.equals("consume"))
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   110
                                             .count()) == 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   111
                assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   112
                                             .filter(mn -> mn.equals("run"))
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   113
                                             .count()) == 1);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   114
                assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   115
                                             .filter(mn -> mn.equals("assertWalker"))
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   116
                                             .count()) == LOOPS);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   117
            } else {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   118
                assertWalker(walker, n);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   119
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   120
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   121
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   123
    class T1 extends Thread implements Consumer {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   124
        final StackWalker walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   126
        public T1(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   127
            this.walker = walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   129
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   130
        public void run() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
            consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   133
            Thread t2 = new T2(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
            t2.start();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
            try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   136
                t2.join();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
            } catch (InterruptedException e) { }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
            consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   140
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   141
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   142
        public void consume() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
            assertWalker(walker, LOOPS);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   144
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   145
            // verify walker.walk() reaches T1 class through methods run() and consume().
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   146
            assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   147
                                         .count()) == 2);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   148
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   149
            assertCallerClass(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
            assertEquals(T1.class, walker.getCallerClass());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
    class T2 extends Thread implements Consumer {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
        final StackWalker walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
        public T2(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
            this.walker = walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   161
        public void run() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
            consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   164
            Thread t3 = new T3(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   165
            t3.start();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   166
            try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   167
                t3.join();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
            } catch (InterruptedException e) { }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   169
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   170
            consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   171
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   172
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   173
        public void consume() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   174
            assertWalker(walker, LOOPS);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
            // verify walker.walk() reaches T2 class through methods run() and consume().
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   177
            assertTrue(walker.walk(s -> s.filter(f -> T2.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
                                         .count()) == 2);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   179
            // verify T1 is not reached, even if call is invoked
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   180
            // from test()->T1.start()->T1.run()->T2
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   181
            assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   182
                                         .count()) == 0);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   183
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   184
            assertCallerClass(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
            assertEquals(T2.class, walker.getCallerClass());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   187
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   188
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
    class T3 extends Thread implements Consumer {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
        final StackWalker walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   191
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   192
        public T3(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   193
            this.walker = walker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   194
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   195
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   196
        public void run() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   197
            consume();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   198
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   199
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   200
        public void consume() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   201
            assertWalker(walker, LOOPS);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   202
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   203
            // verify walker.walk() reaches T1 class through methods run() and consume().
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   204
            assertTrue(walker.walk(s -> s.filter(f -> T3.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   205
                                         .count()) == 2);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   206
            // verify T1, T2 is not reached, even if call is invoked
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   207
            // from test() -> T1.start() -> T1.run() -> T2.start() -> T2.run() -> T3
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   208
            assertTrue(walker.walk(s -> s.filter(f -> T2.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   209
                                         .count()) == 0);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   210
            assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   211
                                         .count()) == 0);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   212
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   213
            assertCallerClass(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   214
            assertEquals(T3.class, walker.getCallerClass());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   215
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   216
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   217
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   218
    static void assertCallerClass(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   219
        // verify walker.getCallerClass() get the expected class.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   220
        call(walker);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   221
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   222
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   223
    static void call(StackWalker walker) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   224
        Class<?> c = walker.getCallerClass();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   225
        assertEquals(c, AcrossThreads.class);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   226
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   227
}