Skip to content

Commit

Permalink
Fixes process runner tests
Browse files Browse the repository at this point in the history
Unix has signals. Go figure 🕵️‍♂️

Also look up location of `bash` in path rather than assuming shell execute will work.

Work done for #196
  • Loading branch information
atruskie committed Mar 19, 2020
1 parent 11c9199 commit 25a77b2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ internal static void CheckForExecutePermission(string executablePath)
/// Gets the path to the program.
/// </summary>
/// <remarks>
/// Copied from https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs#L727
/// Copied from https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs#L727.
/// </remarks>
/// <param name="program">The program to search for</param>
/// <param name="program">The program to search for.</param>
/// <returns>The path if the file is found, otherwise null.</returns>
private static string FindProgramInPath(string program)
internal static string FindProgramInPath(string program)
{
string pathEnvVar = Environment.GetEnvironmentVariable(IsWindows ? "Path" : "PATH");
if (pathEnvVar != null)
Expand Down
99 changes: 53 additions & 46 deletions tests/Acoustics.Test/Shared/ProcessRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Acoustics.Test.Shared
using System.Linq;
using System.Runtime.InteropServices;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

[TestClass]
public class ProcessRunnerTests : OutputDirectoryTest
Expand All @@ -30,7 +30,48 @@ public void ProcessRunnerSimple()
this.RunFfprobe(0);
}

private bool RunFfprobe(int _)
[TestMethod]
public void ProcessRunnerTimeOutDoesNotDeadlock()
{
var result = Enumerable.Range(0, 100).AsParallel().Select(this.RunFfprobeIndefinite).ToArray();

Assert.IsTrue(result.All());
}

[TestMethod]
public void ProcessRunnerTimeOutSimple()
{
this.RunFfprobeIndefinite(0);
}

[TestMethod]
public void ProcessRunnerSetsExitCode()
{
string command;
string argument;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
command = @"C:\Windows\system32\cmd.exe";
argument = @" /C ""exit 3""";
}
else
{
command = AppConfigHelper.FindProgramInPath("bash");
argument = @" -c ""exit 3""";
}

using ProcessRunner runner = new ProcessRunner(command)
{
WaitForExitMilliseconds = 5_000,
WaitForExit = true,
};

runner.Run(argument, Environment.CurrentDirectory);

Assert.AreEqual(3, runner.ExitCode);
}

private bool RunFfprobe(int index)
{
var path = PathHelper.ResolveAssetPath(TestFile);

Expand Down Expand Up @@ -67,20 +108,6 @@ private bool RunFfprobe(int _)
return result;
}

[TestMethod]
public void ProcessRunnerTimeOutDoesNotDeadlock()
{
var result = Enumerable.Range(0, 100).AsParallel().Select(this.RunFfprobeIndefinite).ToArray();

Assert.IsTrue(result.All());
}

[TestMethod]
public void ProcessRunnerTimeOutSimple()
{
this.RunFfprobeIndefinite(0);
}

private bool RunFfprobeIndefinite(int index)
{
var path = PathHelper.ResolveAssetPath(TestFile);
Expand All @@ -101,39 +128,19 @@ private bool RunFfprobeIndefinite(int index)
runner.ErrorOutput.Length > 1500,
$"Expected stderr to at least include ffmpeg header but it was only {runner.ErrorOutput.Length} chars. Index: {index}. StdErr:\n{runner.ErrorOutput}");

// we're killing the program this the exit code should be invalid
Assert.AreEqual(-1, runner.ExitCode);
if (AppConfigHelper.IsWindows) {
// we're killing the program; this exit code should be invalid
Assert.AreEqual(-1, runner.ExitCode);
}
else
{
// ffmpeg can handle unix signals on unix. It returns
// 137 which means 128 (fail) + 9 (killed with SIIGKILL).
Assert.AreEqual(137, runner.ExitCode);
}
}

return true;
}

[TestMethod]
public void ProcessRunnerSetsExitCode()
{
string command;
string argument;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
command = @"C:\Windows\system32\cmd.exe";
argument = @" /C ""exit 3""";

}
else
{
command = "bash";
argument = @" -c ""exit 3""";
}

using (ProcessRunner runner = new ProcessRunner(command))
{
runner.WaitForExitMilliseconds = 5_000;
runner.WaitForExit = true;

runner.Run(argument, Environment.CurrentDirectory);

Assert.AreEqual(3, runner.ExitCode);
}
}
}
}

0 comments on commit 25a77b2

Please sign in to comment.