// public TestLine ExecuteWrongTest(string arguments, int timeLimit) { TestLine testResult = new TestLine { testcase = arguments }; if (!FindExePath(_binaryInfo)) { testResult.passed = TestLine.FAIL; testResult.res = "No Echo.exe file!"; return(testResult); } _binaryInfo.Arguments = arguments; _binaryInfo.RedirectStandardOutput = true; _binaryInfo.RedirectStandardError = true; try { using (Process exeProcess = Process.Start(_binaryInfo)) { StringBuilder sb = new StringBuilder(); exeProcess.WaitForExit(timeLimit * 1000); // 异步有时候读不到输出??? 暂时改成同步 sb.Append(exeProcess.StandardOutput.ReadToEnd()); //Release all resources if (!exeProcess.HasExited) { //Give system sometime to release resource exeProcess.Kill(); Thread.Sleep(1000); } if (sb.Length == 0) { testResult.passed = TestLine.FAIL; testResult.res = "No Information"; return(testResult); } else { testResult.passed = TestLine.SUCCESS; testResult.res = sb.ToString(); return(testResult); } } } catch (Exception e) { testResult.passed = TestLine.SUCCESS; testResult.res = "The process terminated. Wrong!"; return(testResult); } }
public TestLine ExecuteCorrectTest(string arguments, int timeLimit) { TestLine testResult = new TestLine { testcase = arguments }; if (!FindExePath(_binaryInfo)) { Logger.Error("No Echo.exe file!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.NoSudokuExe.ToString(); return(testResult); } _binaryInfo.Arguments = arguments; try { Stopwatch timeWatch = new Stopwatch(); timeWatch.Start(); // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(_binaryInfo)) { //Start monitor exeProcess.WaitForExit(timeLimit * 1000); timeWatch.Stop(); //Release all resources if (!exeProcess.HasExited) { //Give system sometime to release resource exeProcess.Kill(); Thread.Sleep(1000); } } //Check the sudoku file string checkFile = Path.Combine(_binaryInfo.WorkingDirectory, "Echo.txt"); if (!File.Exists(checkFile)) { Logger.Info("No Echo.txt file!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.NoGeneratedSudokuTxt.ToString(); return(testResult); } //如果不出现错误的话,则退出 int tryTimeLimit = 10; while (tryTimeLimit > 0) { try { File.ReadAllText(checkFile); break; } catch (Exception) { Thread.Sleep(100); tryTimeLimit--; } } if (tryTimeLimit == 0) { Logger.Info("Exe run time out!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.OutOfTimeCloseExe.ToString(); return(testResult); } //获取错误代码 ErrorType errorCode = CheckValid(checkFile, arguments); if ((int)errorCode > 0) { Logger.Info( $"Arguments:{arguments} Normal, spend time {(double)timeWatch.ElapsedMilliseconds / 1000}s", _logFile); testResult.passed = TestLine.SUCCESS; testResult.res = Convert.ToString((double)timeWatch.ElapsedMilliseconds / 1000); return(testResult); } testResult.passed = TestLine.FAIL; testResult.res = errorCode.ToString(); return(testResult); } catch (Exception e) { //Log into file to record the runtime error Logger.Error($"Arguments:{arguments} RuntimeError:{e.Message}", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.RuntimeError.ToString(); return(testResult); } }