1 /*
   2  * Copyright (c) 2012, 2015 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     7194254
  27  * @summary Creates several threads with different java priorities and checks
  28  *      whether jstack reports correct priorities for them.
  29  *
  30  * @library /testlibrary
  31  * @build com.oracle.java.testlibrary.*
  32  * @run main ThreadPriorities
  33  */
  34 
  35 import java.util.ArrayList;
  36 import java.util.concurrent.CyclicBarrier;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 
  40 import com.oracle.java.testlibrary.*;
  41 import static com.oracle.java.testlibrary.Asserts.*;
  42 
  43 public class ThreadPriorities {
  44 
  45     public static void main(String[] args) throws Throwable {
  46         final int NUMBER_OF_JAVA_PRIORITIES =
  47                 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
  48         final CyclicBarrier barrier =
  49                 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
  50 
  51         for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
  52             final int priority = p;
  53             new Thread("Priority=" + p) {
  54                 {
  55                     setPriority(priority);
  56                 }
  57                 public void run() {
  58                     try {
  59                         barrier.await(); // 1st
  60                         barrier.await(); // 2nd
  61                     } catch (Exception exc) {
  62                         // ignore
  63                     }
  64                 }
  65             }.start();
  66         }
  67         barrier.await(); // 1st
  68 
  69         int matches = 0;
  70         ArrayList<String> failed = new ArrayList<>();
  71         ProcessBuilder pb = new ProcessBuilder(
  72                 JDKToolFinder.getJDKTool("jstack"),
  73                 String.valueOf(ProcessTools.getProcessId()));
  74 
  75         String[] output = new OutputAnalyzer(pb.start()).getOutput().split("\\n+");
  76 
  77         Pattern pattern = Pattern.compile(
  78                 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
  79         for (String line : output) {
  80             Matcher matcher = pattern.matcher(line);
  81             if (matcher.matches()) {
  82                 matches += 1;
  83                 String expected = matcher.group(1);
  84                 String actual = matcher.group(2);
  85                 if (!expected.equals(actual)) {
  86                     failed.add(line);
  87                 }
  88             }
  89         }
  90         barrier.await(); // 2nd
  91         barrier.reset();
  92 
  93         assertEquals(matches, NUMBER_OF_JAVA_PRIORITIES);
  94         assertTrue(failed.isEmpty(), failed.size() + ":" + failed);
  95     }
  96 }
  97