Skip to content

Commit

Permalink
ProcessWatchdogTest: improve diagnostics, migrate to ping instead of …
Browse files Browse the repository at this point in the history
…timeout

timeout was behaving weirdly in cmd with IO redirection.
  • Loading branch information
ForNeVeR committed Dec 2, 2023
1 parent 5d61b6e commit d2107f0
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions rd-net/Test.Lifetimes/Diagnostics/ProcessWatchdogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ private static Task DoTest(Func<Process> processCreator, bool assertAlive) => Li
if (assertAlive)
{
await Task.Delay(timeForReliableDetection, lt);
Assert.IsFalse(task.IsCompleted);
Assert.IsFalse(process.HasExited, "Process should not be exited.");
Assert.IsFalse(task.IsCompleted, "Watchdog should not be triggered.");
}

if (!process.HasExited) process.Kill();
Expand All @@ -62,15 +63,21 @@ private static Task DoTest(Func<Process> processCreator, bool assertAlive) => Li

private Process StartSleepingProcess()
{
if (RuntimeInfo.IsRunningUnderWindows)
{
return Process.Start(new ProcessStartInfo("cmd.exe", "/c timeout 30")
{
WindowStyle = ProcessWindowStyle.Hidden
});
}
var startInfo = RuntimeInfo.IsRunningUnderWindows
? new ProcessStartInfo("cmd.exe", "/c ping 127.0.0.1 -n 30")
: new ProcessStartInfo("sleep", "30");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

var logger = Log.GetLog<ProcessWatchdogTest>();
var process = Process.Start(startInfo)!;
process.ErrorDataReceived += (_, args) => logger.Warn($"[{process.Id}] {args.Data}");
process.OutputDataReceived += (_, args) => logger.Info($"[{process.Id}] {args.Data}");
process.Exited += (_, _) => logger.Info($"[{process.Id}] Exited with code: {process.ExitCode}");

return Process.Start("sleep", "30");
return process;
}

private Process GetTerminatedProcess(int exitCode)
Expand Down

0 comments on commit d2107f0

Please sign in to comment.