changeset 12:df2c4155dc25

CODETOOLS-7901254, CODETOOLS-7900991, CODETOOLS-7901040
author afedorch
date Fri, 16 Jan 2015 18:45:39 +0400
parents cd6626baaedd
children f73d660552bb
files src/classes/com/sun/tdk/jcov/Grabber.java src/classes/com/sun/tdk/jcov/GrabberManager.java src/classes/com/sun/tdk/jcov/report/html/CoverageReport.java src/classes/com/sun/tdk/jcov/runtime/NetworkSatelliteDecorator.java src/classes/jcov/JTObserver.java
diffstat 5 files changed, 310 insertions(+), 127 deletions(-) [+]
line wrap: on
line diff
--- a/src/classes/com/sun/tdk/jcov/Grabber.java	Thu Nov 20 18:24:34 2014 +0400
+++ b/src/classes/com/sun/tdk/jcov/Grabber.java	Fri Jan 16 18:45:39 2015 +0400
@@ -332,7 +332,7 @@
     private int totalConnections;           // connections occurred
     int aliveClients = 0;                   // increased on client.start(), decreased in client
     private LinkedList<String> tests;       // accepted testnames
-    private static int MAX_TIMEOUT = 90000; // maximum time to wait for Client working
+    static int MAX_TIMEOUT = 90000;         // maximum time to wait for Client working
     static boolean showMemoryChecks = false; // show memory checks
     final static Runtime rt = Runtime.getRuntime(); // runtime
     private long reservedMemory = 0;        // memory reserved by alive clients - not used yet
@@ -1164,11 +1164,16 @@
                     switch (command) {
                         case MiscConstants.GRABBER_KILL_COMMAND:
                             Grabber.logger.log(Level.INFO, "Server received kill command.");
+                            BufferedReader inReader = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
+                            int killtimeout = Integer.valueOf(inReader.readLine());
+                            if (killtimeout > 0){
+                                server.setMaxTimeout(killtimeout*1000);
+                            }
                             server.kill(false);
 //                            server.saveData();
                             socket.getOutputStream().write(1);
                             socket.getOutputStream().close();
-                            in.close();
+                            inReader.close();
                             break outer;
                         case MiscConstants.GRABBER_FORCE_KILL_COMMAND:
                             socket.getOutputStream().write(1);
--- a/src/classes/com/sun/tdk/jcov/GrabberManager.java	Thu Nov 20 18:24:34 2014 +0400
+++ b/src/classes/com/sun/tdk/jcov/GrabberManager.java	Fri Jan 16 18:45:39 2015 +0400
@@ -35,6 +35,8 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.BufferedWriter;
+import java.io.OutputStreamWriter;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import java.nio.charset.Charset;
@@ -53,6 +55,7 @@
 
     private LinkedList<ServerCommand> commands;
     private int waittime = 5;
+    private int stoptimeout = 0;
 
     @Override
     protected int run() throws Exception {
@@ -118,6 +121,7 @@
                     DSC_PORT,
                     DSC_FILE,
                     DSC_WAITTIME,
+                    DSC_STOPTIMEOUT,
                     COMM_KILL,
                     COMM_KILL_FORCE,
                     COMM_SAVE,
@@ -166,6 +170,9 @@
         if (opts.isSet(DSC_WAITTIME)) {
             waittime = Utils.checkedToInt(opts.getValue(DSC_WAITTIME), "time to wait value");
         }
+        if (opts.isSet(DSC_STOPTIMEOUT)) {
+            stoptimeout = Utils.checkedToInt(opts.getValue(DSC_STOPTIMEOUT), "time to wait before stop");
+        }
 
         return SUCCESS_EXIT_CODE;
     }
@@ -189,6 +196,9 @@
     final static OptionDescr DSC_PORT =
             new OptionDescr("command_port", new String[]{"port"}, "", OptionDescr.VAL_SINGLE, "Specify servers command port.",
             Integer.toString(MiscConstants.JcovGrabberCommandPort));
+    final static OptionDescr DSC_STOPTIMEOUT =
+            new OptionDescr("stoptimeout", new String[]{"stopt", "killt"}, "", OptionDescr.VAL_SINGLE,
+                    "Max time in seconds for Grabber to save all alive connections before stop.", Integer.toString(Server.MAX_TIMEOUT / 1000));
     final static OptionDescr DSC_FILE =
             new OptionDescr("grabber.props", "", OptionDescr.VAL_SINGLE, "Read server properties from a file. Host should be specified explicitly.");
     final static OptionDescr DSC_WAITTIME =
@@ -240,11 +250,21 @@
             socket = new Socket(host, port);
             OutputStream out = socket.getOutputStream();
             out.write(code);
+            BufferedWriter outWriter = null;
+            if (code == COMM_KILL.getCommandCode()){
+                outWriter = new BufferedWriter(new OutputStreamWriter(out, Charset.defaultCharset()));
+                outWriter.write(String.valueOf(stoptimeout));
+                outWriter.newLine();
+                outWriter.flush();
+            }
             socket.getInputStream().read();
             socket.getInputStream().close();
+            if (outWriter != null) {
+                outWriter.close();
+            }
             out.close();
         } finally {
-            if (socket != null) {
+            if (socket != null && !socket.isClosed()) {
                 socket.close();
             }
         }
--- a/src/classes/com/sun/tdk/jcov/report/html/CoverageReport.java	Thu Nov 20 18:24:34 2014 +0400
+++ b/src/classes/com/sun/tdk/jcov/report/html/CoverageReport.java	Fri Jan 16 18:45:39 2015 +0400
@@ -716,7 +716,12 @@
         generateScriptsHeader(pw);
         pw.println("</head>");
         pw.println("<body>");
-        generateNavHeader(pw, theClass.getName() + ".html", "" + theClass.getPackageName().replaceAll("[a-zA-Z0-9]+", "..") + "/index.html?" + srcOutputFilename);
+        if (!theClass.getPackageName().isEmpty()) {
+            generateNavHeader(pw, theClass.getName() + ".html", "" + theClass.getPackageName().replaceAll("[a-zA-Z0-9]+", "..") + "/index.html?" + srcOutputFilename);
+        }
+        else{
+            generateNavHeader(pw, theClass.getName() + ".html", "" + theClass.getName()+"/../index.html?" + srcOutputFilename);
+        }
         //pw.println("<span class=\"title\">" + title + " "
         //  + coverage.getData(ColumnName.PRODUCT) + "</span>");
         if (isGenHitTests) {
--- a/src/classes/com/sun/tdk/jcov/runtime/NetworkSatelliteDecorator.java	Thu Nov 20 18:24:34 2014 +0400
+++ b/src/classes/com/sun/tdk/jcov/runtime/NetworkSatelliteDecorator.java	Fri Jan 16 18:45:39 2015 +0400
@@ -24,11 +24,8 @@
  */
 package com.sun.tdk.jcov.runtime;
 
-import java.io.DataInputStream;
-import java.io.IOException;
-import java.net.ServerSocket;
+import java.io.*;
 import java.net.Socket;
-import java.nio.charset.Charset;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Level;
@@ -40,110 +37,98 @@
  */
 public class NetworkSatelliteDecorator implements SaverDecorator {
 
-    private ServerSocket ss;
     private JCovSaver wrapped;
-    private int port1 = 3337;
+    private int port = 3337;
+    private static String host = "localhost";
     private static Lock lock = new ReentrantLock();
-
-    static {
-        Collect.saveAtShutdownEnabled = false;
-    }
-
-    public NetworkSatelliteDecorator() {
-        System.setProperty("jcov.autosave", "false");
-    }
+    private Thread socketClientThread = null;
+    private volatile String name = null;
 
     public void init(JCovSaver wrap) {
         this.wrapped = wrap;
+        listenObserver();
+    }
 
-        ThreadGroup tg;
-        if (Thread.currentThread().getThreadGroup() != null && Thread.currentThread().getThreadGroup().getParent() != null) {
-            tg = new ThreadGroup(Thread.currentThread().getThreadGroup().getParent(), "JCov");
-        } else {
-            tg = new ThreadGroup("JCov");
-        }
-        tg.setDaemon(true);
-
-        Thread t = new Thread(tg, "JCovSatellite") {
+    private void listenObserver(){
+        socketClientThread = new Thread(new Runnable() {
             @Override
             public void run() {
+                BufferedReader in;
+                PrintWriter out;
                 try {
-                    ss = new ServerSocket(port1);
-                    outer:
+                    Socket socket = new Socket(host, port);
+                    in = new BufferedReader(new InputStreamReader(
+                            socket.getInputStream()));
+                    out = new PrintWriter(socket.getOutputStream(), true);
+
                     while (true) {
-                        Socket sock = null;
+                        String line = null;
                         try {
-                            sock = ss.accept();
+                            line = in.readLine();
+                        } catch (Exception e) {
+                            lock.lock();
+                            try {
+                                wrapped.saveResults();
+                            }
+                            finally {
+                                lock.unlock();
+                            }
+                        }
+                        if (line != null) {
+                            if (line.startsWith("NAME")) {
+                                name = line.substring(4, line.length());
+                                System.setProperty("jcov.testname", name);
+                                out.println("named " + name);
+                                out.flush();
+                            } else if (line.startsWith("SAVE")) {
+                                name = line.substring(4, line.length());
+                                System.setProperty("jcov.testname", name);
 
-                            DataInputStream in = new DataInputStream(sock.getInputStream());
-                            byte buff[] = new byte[4];
-                            for (int i = 0; i < buff.length; ++i) {
-                                buff[i] = in.readByte();
-                            }
-
-                            if (!new String(buff, Charset.defaultCharset()).equals("JCOV")) {
-                                continue;
-                            }
-
-                            int code = in.read();
-                            switch (code) {
-                                case 0: // SAVE
-                                    String name = in.readUTF();
-                                    System.setProperty("jcov.testname", name);
-                                    lock.lock();
-                                    try {
-                                        wrapped.saveResults();
-                                    } finally {
-                                        lock.unlock();
-                                    }
-                                    sock.getOutputStream().write(0);
-                                    break;
-                                case 1: // NAME
-                                    name = in.readUTF();
-                                    System.setProperty("jcov.testname", name);
-                                    sock.getOutputStream().write(0);
-                                    break;
-                                case 2: // EXIT
-                                    sock.getOutputStream().write(0);
-                                    break outer;
-                                default:
-                                    sock.getOutputStream().write(0);
-                            }
-
-                        } catch (Exception e) {
-                            e.printStackTrace();
-                        } finally {
-                            try {
-                                if (sock != null) {
-                                    sock.close();
+                                lock.lock();
+                                try {
+                                    wrapped.saveResults();
                                 }
-                            } catch (Exception e) {
+                                finally {
+                                    lock.unlock();
+                                }
+                                out.println("saved " + name);
+                                out.flush();
+                                name = null;
                             }
                         }
                     }
-                } catch (IOException ex) {
-                    Logger.getLogger(NetworkSatelliteDecorator.class.getName()).log(Level.SEVERE, null, ex);
-                } finally {
-                    try {
-                        ss.close();
-                    } catch (Throwable e) {
-                    }
+
+                } catch (Exception e) {
+                    Logger.getLogger(NetworkSatelliteDecorator.class.getName()).log(Level.SEVERE, "SocketClient: ", e);
                 }
             }
-        };
-        t.setDaemon(true);
-        t.start();
+
+        });
+        socketClientThread.setDaemon(true);
+        socketClientThread.start();
+
     }
 
     public void saveResults() {
-        if (!lock.tryLock()) {
+
+        while (name == null){
+            try {
+                Thread.currentThread().sleep(100);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+
+        if (name != null) {
+            System.setProperty("jcov.testname", name);
             lock.lock();
+            try {
+                wrapped.saveResults();
+            } finally {
+                lock.unlock();
+            }
+            name = null;
         }
-        lock.unlock();
-        try {
-            ss.close();
-        } catch (Exception ex) {
-        }
-//        wrapped.saveResults();
+
     }
 }
--- a/src/classes/jcov/JTObserver.java	Thu Nov 20 18:24:34 2014 +0400
+++ b/src/classes/jcov/JTObserver.java	Fri Jan 16 18:45:39 2015 +0400
@@ -28,8 +28,10 @@
 import com.sun.javatest.Parameters;
 import com.sun.javatest.TestResult;
 import com.sun.javatest.TestResult.Section;
-import java.io.DataOutputStream;
+import java.io.*;
+import java.net.ServerSocket;
 import java.net.Socket;
+import java.util.HashSet;
 
 /**
  *
@@ -38,13 +40,33 @@
 public class JTObserver implements Harness.Observer {
 
     private static int port = 3337;
-    private static String host = "localhost";
     public static final int SAVE = 0;
     public static final int NAME = 1;
-    public static final int EXIT = 2;
-    public static final int WAIT = 3; // should be run in same thread (Thread.run())
+
+    private static final HashSet<ClientData> clientsData = new HashSet<ClientData>();
+    private static volatile String currentname = null;
+    private static volatile boolean saving = false;
+    private static volatile boolean naming = false;
 
     public JTObserver() {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    ServerSocket listener = new ServerSocket(port);
+                    try {
+                        while (true) {
+                            new Handler(listener.accept()).start();
+                        }
+                    } finally {
+                        listener.close();
+                    }
+                }
+                catch (Exception e){
+                    log("Socket server exception: "+e);
+                }
+            }
+        }).start();
     }
 
     public void startingTestRun(Parameters prmtrs) {
@@ -52,7 +74,7 @@
     }
 
     public void startingTest(TestResult tr) {
-        new Signal(tr.getTestName(), NAME).start();
+        send(tr.getTestName(), NAME);
         tr.addObserver(new TestResult.Observer() {
             public void createdSection(TestResult tr, Section sctn) {
             }
@@ -73,14 +95,13 @@
             }
 
             public void completed(TestResult tr) {
-                new Signal(tr.getTestName(), SAVE).start();
+                send(tr.getTestName(), SAVE);
             }
         });
         log("Starting test " + tr.getTestName());
     }
 
     public void finishedTest(TestResult tr) {
-        //new Signal(tr.getTestName(), SAVE).start();
         log("Finished test " + tr.getTestName());
     }
 
@@ -89,9 +110,6 @@
     }
 
     public void finishedTesting() {
-        Signal sig = new Signal(null, EXIT);
-        sig.setDaemon(true);
-        sig.start();
         log("Finished testing");
     }
 
@@ -103,43 +121,193 @@
         log("error");
     }
 
-    public static class Signal extends Thread {
+    private static void log(String str) {
+        //System.out.println(str);
+    }
 
-        private String message;
-        private int command;
+    private void send(String name, int command) {
 
-        public Signal(String name, int command) {
-            this.message = name;
-            this.command = command;
+        log("send name="+name + " command = "+command);
+        log("clientsData.size() ="+clientsData.size());
+
+        if (command == JTObserver.NAME) {
+            currentname = name;
+            naming = true;
+
+            while (!nameClientsData()){
+                try {
+                    Thread.currentThread().sleep(100);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            synchronized (clientsData){
+                for (ClientData clientData :clientsData){
+                    clientData.setNamed(false);
+                }
+            }
         }
 
-        @Override
+        if (command == JTObserver.SAVE) {
+            saving = true;
+            currentname = name;
+
+            while (!saveClientsData()){
+                try {
+                    Thread.currentThread().sleep(100);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            synchronized (clientsData){
+                for (ClientData clientData :clientsData){
+                    clientData.setSaved(false);
+                }
+            }
+        }
+        log("end send");
+    }
+
+    private boolean nameClientsData(){
+        synchronized (clientsData){
+
+            for (ClientData clientData : clientsData){
+                if (!clientData.isNamed())
+                    return false;
+            }
+
+            if (clientsData.size() != 0)
+                naming = false;
+
+            return true;
+        }
+    }
+
+    private boolean saveClientsData(){
+        synchronized (clientsData){
+
+            for (ClientData clientData :clientsData){
+                if (!clientData.isSaved())
+                    return false;
+            }
+
+            if (clientsData.size() != 0)
+                saving = false;
+
+            return true;
+        }
+    }
+
+    private class Handler extends Thread {
+        private Socket socket;
+        private BufferedReader in;
+        private PrintWriter out;
+
+
+        public Handler(Socket socket) {
+            this.socket = socket;
+        }
+
         public void run() {
-            for (int i = 0; i < 100; ++i) {
-                try {
-                    Socket s = new Socket(host, port);
-                    DataOutputStream out = new DataOutputStream(s.getOutputStream());
-                    out.writeBytes("JCOV");
-                    out.write(command);
-                    if (message != null) {
-                        out.writeUTF(message);
+            try {
+                in = new BufferedReader(new InputStreamReader(
+                        socket.getInputStream()));
+                out = new PrintWriter(socket.getOutputStream(), true);
+                ClientData clientData = null;
+
+                synchronized (clientsData) {
+                    clientData = new ClientData(out, in);
+                    clientsData.add(clientData);
+                }
+
+                while (true) {
+
+                    try {
+                        Thread.currentThread().sleep(100);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
                     }
-                    s.getInputStream().read();
-                    s.close();
-                    log("DONE! " + message + " " + command);
-                    break;
-                } catch (Exception ex) {
-                    try {
-                        log("waiting " + message + " " + command);
-                        Thread.sleep(100);
-                    } catch (InterruptedException ex1) {
+
+                    synchronized (clientData) {
+
+                        if (naming && !clientData.isNamed()){
+                            try {
+                                clientData.getWriter().println("NAME" + currentname);
+                                String answer = clientData.getReader().readLine();
+                                log("name answer = " + answer);
+                            }
+                            catch(Exception e){
+                                //we do not close clients connections
+                            }
+                            clientData.setNamed(true);
+                        }
+
+                        if (saving && !clientData.isSaved()){
+                            try {
+                                clientData.getWriter().println("SAVE" + currentname);
+                                String answer = clientData.getReader().readLine();
+                                log("save answer = " + answer);
+                            }
+                            catch(Exception e){
+                                //we do not close clients connections
+                            }
+                            clientData.setSaved(true);
+                        }
+
+                        if (currentname!=null && !naming && !saving && !clientData.isNamed()){
+                            try {
+                                clientData.getWriter().println("NAME" + currentname);
+                                String answer = clientData.getReader().readLine();
+                                log("name answer = " + answer);
+                            }
+                            catch(Exception e){
+                                //we do not close clients connections
+                            }
+                            clientData.setNamed(true);
+                        }
                     }
+
                 }
+            } catch (IOException e) {
+                log("JTObserver: " + e);
             }
         }
     }
 
-    private static void log(String str) {
-//        System.out.println(str);
+    private static class ClientData {
+        private PrintWriter writer;
+        private BufferedReader reader;
+        private boolean named = false;
+        private boolean saved = false;
+
+        public ClientData(PrintWriter writer, BufferedReader reader){
+            this.writer = writer;
+            this.reader = reader;
+        }
+
+        public PrintWriter getWriter(){
+            return writer;
+        }
+
+        public BufferedReader getReader(){
+            return reader;
+        }
+
+        public boolean isNamed() {
+            return named;
+        }
+
+        public void setNamed(boolean named) {
+            this.named = named;
+        }
+
+        public boolean isSaved() {
+            return saved;
+        }
+
+        public void setSaved(boolean saved) {
+            this.saved = saved;
+        }
     }
-}
+}
\ No newline at end of file