Java Process - 流程的生命周期太短。 (不到1秒......)

时间:2014-06-01 14:33:19

标签: java process lifetime

问题:

函数this.proc.IsAlive中的

make_test返回false ...我不知道为什么...... 通常等待用户命令...(在控制台的情况下)

外部计划: -> L10_Z1_external_test

目标: 过程应该是生命......

L10_Z1__Test__Core.java

package BST_Test;

import java.io.IOException;

public class L10_Z1__Test__Core {
    public static void main(String[] args) {
        // Build Proccess
        ProcessBuilder pr_1 = new ProcessBuilder("java", "-jar", "../BST.jar");
        try {
            Process proc = pr_1.start();
            L10_Z1__Test__C_Tester test_engine = new L10_Z1__Test__C_Tester(
                    proc);
            String[] cmd;
            String[] result;
            cmd = new String[] { "add", "remove" };
            result = new String[] { "Sucess (add) | ", "Sucess (remove) | ",
                    "Sucess (add) | ", "Sucess (remove) | ",
                    "Sucess (add) - Duplicate | ",
                    "Error (remove) - Not found | ",
                    "Error (Parse) - Invalid integer or Overflow | ",
                    "Error (remove) - Not found | ", "Sucess (add) | ",
                    "Sucess (remove) | ", "Sucess (add) | ",
                    "Sucess (remove) | ", "Sucess (add) | ",
                    "Sucess (remove) | ", "Sucess (add) | ",
                    "Sucess (remove) | ", "Sucess (add) | ",
                    "Sucess (remove) | ", "Sucess (add) | ",
                    "Sucess (remove) | ",
                    "Error (Parse) - Invalid integer or Overflow | ",
                    "Error (remove) - Not found | " };
            test_engine.make_test("ADD & search",
                    "This test check - how work add & remove", cmd, result,
                    new String[] { "-3", "4", "-3", "aa", "12", "5", "12", "7",
                            "-12", "-50", "-999999999" });

            // Stop test;
            test_engine.stop();
        } catch (IOException ex) {
            System.out.println("Error - Problem with init | java -jar "
                    + "../BST.jar");
        }
    }
}

L10_Z1__Test__C_Tester.java

package BST_Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class L10_Z1__Test__C_Tester{

    private boolean is_run; /** Information about Victim of Test ;) */
    private Process pr_proc; /** Victim process */

    private BufferedReader ProcessOut;
    private BufferedWriter ProcessIn;

    /**
     * Default construtor with invoke test subject.
     * @prog_name {String} name of test_subject (Program name: eg. closure.java)
     */
    L10_Z1__Test__C_Tester (Process in){
        this.is_run = false;
        this.pr_proc = in;
        if(this.pr_proc.isAlive()){
            this.ProcessOut = new BufferedReader(new InputStreamReader(this.pr_proc.getInputStream()));
            this.ProcessIn = new BufferedWriter(new OutputStreamWriter(this.pr_proc.getOutputStream()));
            this.is_run = true;
        }
    }


    /**
     * This method create test and invoke. While test is runing -> Program send cmd into process, get result and validate it. (For Multi arguments
     * @param name {String} Name of Test
     * @param desc {String} Description of Test
     * @param cmd {String[]} The commands that are sent into program.
     * @param c_res {String[]} The correct output.
     * @param data  {String[]} The test data.
     */
    public void make_test(String name, String desc, String[] cmd, String[] c_res, String[] data){
        if(this.is_run && this.pr_proc.isAlive()){
            System.out.println("/********************** [" + name + "] - #START **********************/");
            System.out.println("Desc: " + desc);
            int j;
            String out;
            for(int i = 1; i <= data.length; i++){
                System.out.println("\n\nLoop - " + i);
                for(j = 1; j <= cmd.length && this.pr_proc.isAlive(); j++){
                    try {
                        System.out.println("in: - " + cmd[(j - 1)] + " " + data[(i - 1)]);
                        this.ProcessIn.write(cmd[(j - 1)] + " " + data[(i - 1)]);
                        this.ProcessIn.flush();
                        out = this.ProcessOut.readLine();
                        System.out.println("out: - " + out +
                                       "----------------------------------" +
                                       "status: - " + this.check(out, c_res[(i - 1) * (j - 1)] + data[(i - 1)]));
                    } catch (IOException ex) {
                        System.out.println("Error - Problem with reader/writte buffer");
                    }
                }
            }
            System.out.println("/********************** [" + name + "] - #END **********************/");
        }
    }
    /**
     * This method create test and invoke. While test is runing -> Program send cmd into process, get result and validate it. (For one argument)
     * @param name {String} Name of Test
     * @param desc {String} Description of Test
     * @param cmd {String[]} The commands that are sent into program.
     * @param c_res {String[]} The correct output.
     */
    public void make_test(String name, String desc, String[] cmd, String[] c_res){
        if(this.is_run){
            System.out.println("/********************** [" + name + "] - #START **********************/");
            System.out.println("Desc: " + desc);
            int count = cmd.length;
            String out;
            for(int i = 0; i < count; i++){
                System.out.println("\n\nLoop - " + i);
                try {
                    System.out.println("in: - " + cmd[i]);
                    this.ProcessIn.write(cmd[i]);
                    out = this.ProcessOut.readLine();
                    System.out.println("out: - " + out +
                                   "----------------------------------" +
                                   "status: - " + this.check(out, c_res[i]));
                    this.ProcessIn.flush();
                } catch (IOException ex) {
                    System.out.println("Error - Problem with reader/writte buffer");
                }
            }
            System.out.println("/********************** [" + name + "] - #END **********************/");
        }
    }

    /**
     * This function validate a output for test subject
     * @param out {String} out to compare with cp
     * @param cp {String} Correct Template
     * @return {String} status of test.
     */
    public String check(String out, String cp){
        if(out.equals(cp))
            return "OK";
        else
            return "FAIL";
    }

    /**
     * Stop operation -> Stop Process, close buffer...
     */
    public void stop(){
        if(this.is_run && this.pr_proc.isAlive() ){
            try {
                this.ProcessIn.close();
                this.ProcessOut.close();
            } catch (IOException ex) {
                System.out.println("Error - Problem with closing I/O buffers");
            }
            this.pr_proc.destroy();
        }
    }
}

L10_Z1_external_test.java

package BST;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class L10_Z1_external_test {


    private L10_Z1__BST BST;

    /**
     * Default constructor with L_10_Z1__BST init;
     */
    L10_Z1_external_test()
    {
        this.BST = new L10_Z1__BST();
    }

    /**
     * This class nitialize, a line reader and controler.
     */
    public void init()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = null;

        try {
            while((s = br.readLine()) != null && !s.equals("quit"))
            {
                this.switch_f(s.split(" "));
            }
        } catch (IOException ex) {
            System.out.println("Error - Unknow I/O problem.");
        }


        System.out.println("Bye");

    }

    /**
     * This method procces user request into BST - Tree functions or specyfied errors
     * @param in {String} - with request, String Format (<command> <number>)
     */
    public void switch_f(String[] in)
    {

        String text = null, error_text = "Error - Unknow command";


        if(in.length == 2)
        {

            int number = 0;
            boolean op_status = true;

            try
            {
                number = Integer.parseInt(in[1]);
            }
            catch (NumberFormatException e)
            {
                op_status = false;
                System.out.println("Error (Parse) - Invalid integer or Overflow | " + in[1]);
            }

            if(op_status)
                if( in[0].equals("add"))
                {
                    text = "Add";

                    try {
                        this.BST.add(number);
                        System.out.println("Sucess (" + text + ") | " + number);
                    } catch (L10_Z1__BST_Exception ex) {
                        System.out.println("Error (" + text + ") - " + ex.getMessage() + " | " + number);
                    }
                }
                else if( in[0].equals("remove"))
                {
                    text = "Remove";

                    try {
                        this.BST.remove(number);
                        System.out.println("Sucess (" + text + ") | " + number);
                    } catch (L10_Z1__BST_Exception ex) {
                        System.out.println("Error (" + text + ") - " + ex.getMessage() + " | " + number);
                    }
                }
                else if( in[0].equals("search"))
                {
                    text = "Search";

                    if(this.BST.search(number))
                        System.out.println("Sucess (" + text + ") | " + number);
                    else
                        System.out.println("Error (" + text + ") - | " + number);
                }
                else
                    System.out.println(error_text);

        }
        else if(in.length == 1)
            if( in[0].equals("list"))
            {
                text = "List";

                try {   
                    this.BST.print_list(this.BST.list_in_order());
                } catch (L10_Z1__BST_Exception ex) {
                    System.out.println("Error (" + text + ") - " + ex.getMessage());
                }
            }
            else
                System.out.println(error_text);
        else
            System.out.println(error_text);

    }    
}

1 个答案:

答案 0 :(得分:0)

您是否正在使用流程监控器检查流程是否已实际启动?我会通过一系列步骤来解决问题:

  1. 确保通过启动ProcessBuilder Process创建的简单命令实际上会在您的操作系统中生成一个进程。如果没有,可能存在与并发或命令语法相关的问题。
  2. 如果您能够启动流程,请启动一个您可以清楚地看到未在流程监视器中终止并在该流程上调用isAlive的流程。如果您可以获得True返回,那么您应该查看测试框架的体系结构,以确保一切正常运行。