public void ProcessRunnerTest_TerminateProcess_Success() { Logger.LogMessage("Testing Process Runner -> Terminate Process."); string exePath = Path.Combine(this.testDataDirectory, "UtilsTestExe", "UtilsTestExe.exe"); using (DesktopProcessRunner processRunner = new DesktopProcessRunner()) { processRunner.ExePath = exePath; ThreadStart threadStart = new ThreadStart(() => processRunner.Run()); // This will run when we call TerminateProcessIfRunning (after the thread is finished execution) threadStart += () => { Logger.LogMessage($"Process exit code: {processRunner.ExitCode}"); string standardOutput = string.Join(string.Empty, processRunner.StandardOutput); Assert.IsFalse(standardOutput.Contains("Process Ending"), "Verifying process didn't print end message"); }; Thread thread = new Thread(threadStart); thread.Start(); // Give some time for the process to start and validate output Thread.Sleep(2000); string stdOut = string.Join(string.Empty, processRunner.StandardOutput); Assert.IsTrue(stdOut.Contains("Process Starting"), "Verifying process start message"); processRunner.TerminateProcessIfRunning(); thread.Join(); } }
public void ProcessRunnerTest_ValidateExitCode_Success() { Logger.LogMessage("Testing ProcessRunner ValidateExitCode"); string exePath = Path.Combine(this.testDataDirectory, "UtilsTestExe", "UtilsTestExe.exe"); using (DesktopProcessRunner processRunner = new DesktopProcessRunner()) { processRunner.ExePath = exePath; ThreadStart threadStart = new ThreadStart(() => processRunner.Run()); // This will run when we call TerminateProcessIfRunning (after the thread is finished execution) threadStart += () => { Logger.LogMessage($"Process exit code: {processRunner.ExitCode}"); // Process should have exited with code -1. bool invalid = false; try { processRunner.ValidateExitCode(); } catch { invalid = true; } Assert.IsTrue(invalid, "Verifying exit code is invalid when we expect 0."); // Make -1 a valid exit code. invalid = false; try { processRunner.ValidateExitCode(new int[] { -1 }); } catch { invalid = true; } Assert.IsFalse(invalid, "Verifying exit code is valid when we specify -1 as a valid code."); // Make -1 an invalid exit code with a list of valid codes. invalid = false; try { processRunner.ValidateExitCode(new int[] { 0 }); } catch { invalid = true; } Assert.IsTrue(invalid, "Verifying exit code is invalid when we specify 0 as a valid code."); }; Thread thread = new Thread(threadStart); thread.Start(); // Give some time for the process to start and validate output Thread.Sleep(2000); string stdOut = string.Join(string.Empty, processRunner.StandardOutput); Assert.IsTrue(stdOut.Contains("Process Starting"), "Verifying process start message"); processRunner.TerminateProcessIfRunning(); thread.Join(); } }