< prev index next >

test/compiler/compilercontrol/share/actions/BaseAction.java

Print this page
rev 9429 : imported patch StressJCMD
rev 9149 : 8066153: JEP-JDK-8046155: Test task: cover existing
Summary: Tests for CompilerCommand and CompilerControl's directives
Reviewed-by: kvn


  38 import java.net.Socket;
  39 import java.util.Arrays;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.ListIterator;
  43 import java.util.Map;
  44 import java.util.concurrent.Callable;
  45 import java.util.stream.Collectors;
  46 
  47 public class BaseAction {
  48     private static final List<Pair<Executable, Callable<?>>> METHODS;
  49     private static final Map<String, Executable> METHODS_NAMES;
  50 
  51     static {
  52         METHODS = new PoolHelper().getAllMethods();
  53         METHODS_NAMES = METHODS.stream().collect(Collectors.toMap(
  54                 pair -> pair.first.toGenericString(),
  55                 pair -> pair.first));
  56     }
  57 





  58     public static void main(String[] args) {
  59         if (args.length < 1) {
  60             throw new Error("TESTBUG: requires port as parameter: "
  61                     + Arrays.toString(args));
  62         }








  63         int pid;
  64         try {
  65             pid = ProcessTools.getProcessId();
  66         } catch (Exception e) {
  67             throw new Error("Could not determine own pid", e);
  68         }
  69         int port = Integer.parseInt(args[0]);
  70         System.out.println("INFO: Client connection port = " + port);
  71         List<String> lines;
  72         try (
  73                 Socket socket = new Socket(InetAddress.getLocalHost(), port);
  74                 BufferedReader in = new BufferedReader(
  75                         new InputStreamReader(socket.getInputStream()));
  76                 PrintWriter out = new PrintWriter(
  77                         new OutputStreamWriter(socket.getOutputStream()))) {
  78             // send own pid to execute jcmd if needed
  79             out.println(String.valueOf(pid));
  80             out.flush();

  81             lines = in.lines().collect(Collectors.toList());




  82         } catch (IOException e) {
  83             throw new Error("Error on performing network operation", e);
  84         }
  85         check(decodeMap(lines));
  86     }
  87 
  88     private static Map<Executable, State> decodeMap(List<String> lines) {
  89         if (lines == null || lines.size() == 0) {
  90             throw new Error("TESTBUG: unexpected lines list");
  91         }
  92         Map<Executable, State> stateMap = new HashMap<>();
  93         int startIndex = 0;
  94         ListIterator<String> iterator = lines.listIterator();
  95         while (iterator.hasNext()) {
  96             int index = iterator.nextIndex();
  97             String next = iterator.next();
  98             switch (next) {
  99                 case "{" :
 100                     startIndex = index;
 101                     break;
 102                 case "}" :
 103                     // method name goes after {
 104                     Executable executable = METHODS_NAMES.get(lines.get(
 105                             ++startIndex));


  38 import java.net.Socket;
  39 import java.util.Arrays;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.ListIterator;
  43 import java.util.Map;
  44 import java.util.concurrent.Callable;
  45 import java.util.stream.Collectors;
  46 
  47 public class BaseAction {
  48     private static final List<Pair<Executable, Callable<?>>> METHODS;
  49     private static final Map<String, Executable> METHODS_NAMES;
  50 
  51     static {
  52         METHODS = new PoolHelper().getAllMethods();
  53         METHODS_NAMES = METHODS.stream().collect(Collectors.toMap(
  54                 pair -> pair.first.toGenericString(),
  55                 pair -> pair.first));
  56     }
  57 
  58     /*
  59      * args[0] is a port to connect
  60      * args[1] is an optional parameter that shows that the state map should be
  61      *         passed
  62      */
  63     public static void main(String[] args) {
  64         if (args.length < 1) {
  65             throw new Error("TESTBUG: requires port as parameter: "
  66                     + Arrays.toString(args));
  67         }
  68         boolean getStates = false;
  69         if (args.length == 2) {
  70             if ("states".equals(args[1])) {
  71                 getStates = true;
  72             } else {
  73                 throw new Error("TESTBUG: incorrect argument: "+ args[1]);
  74             }
  75         }
  76         int pid;
  77         try {
  78             pid = ProcessTools.getProcessId();
  79         } catch (Exception e) {
  80             throw new Error("Could not determine own pid", e);
  81         }
  82         int port = Integer.parseInt(args[0]);
  83         System.out.println("INFO: Client connection port = " + port);
  84         List<String> lines;
  85         try (
  86                 Socket socket = new Socket(InetAddress.getLocalHost(), port);
  87                 BufferedReader in = new BufferedReader(
  88                         new InputStreamReader(socket.getInputStream()));
  89                 PrintWriter out = new PrintWriter(
  90                         new OutputStreamWriter(socket.getOutputStream()))) {
  91             // send own pid to execute jcmd if needed
  92             out.println(String.valueOf(pid));
  93             out.flush();
  94             if (getStates) {
  95                 lines = in.lines().collect(Collectors.toList());
  96                 check(decodeMap(lines));
  97             } else {
  98                 in.readLine();
  99             }
 100         } catch (IOException e) {
 101             throw new Error("Error on performing network operation", e);
 102         }

 103     }
 104 
 105     private static Map<Executable, State> decodeMap(List<String> lines) {
 106         if (lines == null || lines.size() == 0) {
 107             throw new Error("TESTBUG: unexpected lines list");
 108         }
 109         Map<Executable, State> stateMap = new HashMap<>();
 110         int startIndex = 0;
 111         ListIterator<String> iterator = lines.listIterator();
 112         while (iterator.hasNext()) {
 113             int index = iterator.nextIndex();
 114             String next = iterator.next();
 115             switch (next) {
 116                 case "{" :
 117                     startIndex = index;
 118                     break;
 119                 case "}" :
 120                     // method name goes after {
 121                     Executable executable = METHODS_NAMES.get(lines.get(
 122                             ++startIndex));
< prev index next >