test/runtime/7194254/Test7194254.java

Fri, 28 Sep 2012 13:39:41 -0700

author
amurillo
date
Fri, 28 Sep 2012 13:39:41 -0700
changeset 4118
9f008ad79470
parent 4077
a7509aff1b06
child 4146
48087f745a86
permissions
-rw-r--r--

Added tag hs25-b03 for changeset f2e12eb74117

dholmes@4077 1 /*
dholmes@4077 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
dholmes@4077 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dholmes@4077 4 *
dholmes@4077 5 * This code is free software; you can redistribute it and/or modify it
dholmes@4077 6 * under the terms of the GNU General Public License version 2 only, as
dholmes@4077 7 * published by the Free Software Foundation.
dholmes@4077 8 *
dholmes@4077 9 * This code is distributed in the hope that it will be useful, but WITHOUT
dholmes@4077 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dholmes@4077 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dholmes@4077 12 * version 2 for more details (a copy is included in the LICENSE file that
dholmes@4077 13 * accompanied this code).
dholmes@4077 14 *
dholmes@4077 15 * You should have received a copy of the GNU General Public License version
dholmes@4077 16 * 2 along with this work; if not, write to the Free Software Foundation,
dholmes@4077 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dholmes@4077 18 *
dholmes@4077 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dholmes@4077 20 * or visit www.oracle.com if you need additional information or have any
dholmes@4077 21 * questions.
dholmes@4077 22 */
dholmes@4077 23
dholmes@4077 24 /*
dholmes@4077 25 * @test
dholmes@4077 26 * @bug 7194254
dholmes@4077 27 * @summary Creates several threads with different java priorities and checks
dholmes@4077 28 * whether jstack reports correct priorities for them.
dholmes@4077 29 *
dholmes@4077 30 * @run main T7194254
dholmes@4077 31 */
dholmes@4077 32
dholmes@4077 33 import java.io.BufferedReader;
dholmes@4077 34 import java.io.InputStreamReader;
dholmes@4077 35 import java.lang.management.ManagementFactory;
dholmes@4077 36 import java.lang.management.RuntimeMXBean;
dholmes@4077 37 import java.util.ArrayList;
dholmes@4077 38 import java.util.List;
dholmes@4077 39 import java.util.concurrent.CyclicBarrier;
dholmes@4077 40 import java.util.regex.Matcher;
dholmes@4077 41 import java.util.regex.Pattern;
dholmes@4077 42
dholmes@4077 43 public class Test7194254 {
dholmes@4077 44
dholmes@4077 45 public static void main(String[] args) throws Exception {
dholmes@4077 46 final int NUMBER_OF_JAVA_PRIORITIES =
dholmes@4077 47 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
dholmes@4077 48 final CyclicBarrier barrier =
dholmes@4077 49 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
dholmes@4077 50
dholmes@4077 51 for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
dholmes@4077 52 final int priority = p;
dholmes@4077 53 new Thread("Priority=" + p) {
dholmes@4077 54 {
dholmes@4077 55 setPriority(priority);
dholmes@4077 56 }
dholmes@4077 57 public void run() {
dholmes@4077 58 try {
dholmes@4077 59 barrier.await(); // 1st
dholmes@4077 60 barrier.await(); // 2nd
dholmes@4077 61 } catch (Exception exc) {
dholmes@4077 62 // ignore
dholmes@4077 63 }
dholmes@4077 64 }
dholmes@4077 65 }.start();
dholmes@4077 66 }
dholmes@4077 67 barrier.await(); // 1st
dholmes@4077 68
dholmes@4077 69 int matches = 0;
dholmes@4077 70 List<String> failed = new ArrayList<>();
dholmes@4077 71 try {
dholmes@4077 72 String pid = getPid();
dholmes@4077 73 String jstack = System.getProperty("java.home") + "/../bin/jstack";
dholmes@4077 74 Process process = new ProcessBuilder(jstack, pid)
dholmes@4077 75 .redirectErrorStream(true).start();
dholmes@4077 76 Pattern pattern = Pattern.compile(
dholmes@4077 77 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
dholmes@4077 78 try (BufferedReader reader = new BufferedReader(
dholmes@4077 79 new InputStreamReader(process.getInputStream()))) {
dholmes@4077 80 String line;
dholmes@4077 81 while((line = reader.readLine()) != null) {
dholmes@4077 82 Matcher matcher = pattern.matcher(line);
dholmes@4077 83 if (matcher.matches()) {
dholmes@4077 84 matches += 1;
dholmes@4077 85 String expected = matcher.group(1);
dholmes@4077 86 String actual = matcher.group(2);
dholmes@4077 87 if (!expected.equals(actual)) {
dholmes@4077 88 failed.add(line);
dholmes@4077 89 }
dholmes@4077 90 }
dholmes@4077 91 }
dholmes@4077 92 }
dholmes@4077 93 barrier.await(); // 2nd
dholmes@4077 94 } finally {
dholmes@4077 95 barrier.reset();
dholmes@4077 96 }
dholmes@4077 97
dholmes@4077 98 if (matches != NUMBER_OF_JAVA_PRIORITIES) {
dholmes@4077 99 throw new AssertionError("matches: expected " +
dholmes@4077 100 NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
dholmes@4077 101 }
dholmes@4077 102 if (!failed.isEmpty()) {
dholmes@4077 103 throw new AssertionError(failed.size() + ":" + failed);
dholmes@4077 104 }
dholmes@4077 105 System.out.println("Test passes.");
dholmes@4077 106 }
dholmes@4077 107
dholmes@4077 108 static String getPid() {
dholmes@4077 109 RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();
dholmes@4077 110 String vmname = runtimebean.getName();
dholmes@4077 111 int i = vmname.indexOf('@');
dholmes@4077 112 if (i != -1) {
dholmes@4077 113 vmname = vmname.substring(0, i);
dholmes@4077 114 }
dholmes@4077 115 return vmname;
dholmes@4077 116 }
dholmes@4077 117
dholmes@4077 118 }
dholmes@4077 119

mercurial