当我在调试模式下运行代码时,测试完成,当我在运行测试中运行它时,ps exec 似乎挂起并且永远不会返回,我也将 kill 到位,但它从未完成。
为什么这在调试时完成但不在运行模式下。
这里有什么我想念的傻事吗?
public class RemoteExeInvokerTask :IQaTask
{
public string TargetHostName { get; set; }
public string TargetHostUserName { get; set; }
public string TargetPassword { get; set; }
public string TargetExecutableWithParams { get; set; }
public string DoWork()
{
string psExecUtility = Path.GetDirectoryName(Assembly
.GetExecutingAssembly()
.Location)
+ "\\PsTools\\psExec.exe";
string quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} ",
TargetHostName,
TargetHostUserName,
TargetPassword,
TargetExecutableWithParams);
ProcessStartInfo processInfo = new ProcessStartInfo(psExecUtility, quotedArgs);
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
Process process = Process.Start(processInfo);
process.WaitForExit();
string error = process.StandardError.ReadToEnd();
string output = process.StandardOutput.ReadToEnd();
int exitCode = process.ExitCode;
process.Close();
if (Process.GetProcessById(process.Id) != null)
{
process.Kill();
}
if (exitCode <= -1)
{
string errorMessage = string.Format("Process Exited with ErrorMessge {0} {1} "
+ "OutputMessage {2}",
error,
Environment.NewLine,
output);
throw new MyApplicationException(errorMessage);
}
return output;
}
测试代码。
[Test]
[ExpectedException(typeof(MyApplicationException))]
[Description("Expect a exception message form the Server.")]
public void RemoteInvokeOfJobWrapperFromCommandLine()
{
RemoteExeInvokerTask task = new RemoteExeInvokerTask {
TargetHostName = "Serverd02",
TargetHostUserName = @"corp\username",
TargetPassword = @"password",
TargetExecutableWithParams = @" E:\D01\Home.Framework.JobExecutionEngine\Home.Framework.JobexecutionEngine.exe 99999 1"};
string value = task.DoWork();
Assert.IsNotNull(value);
}