Skip to content

Commit

Permalink
1.0.2: fixed bug where timeouts would suppress throwOnError errors
Browse files Browse the repository at this point in the history
  • Loading branch information
madelson committed Feb 22, 2015
1 parent 93fd889 commit 9aa730e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
19 changes: 19 additions & 0 deletions MedallionShell.Tests/GeneralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -121,6 +122,15 @@ public void TestExitCode()
shell.Run("SampleCommand", "exit", 0).Task.Wait();
}

[TestMethod]
public void TestThrowOnErrorWithTimeout()
{
var command = Command.Run("SampleCommand", new object[] { "exit", 1 }, o => o.ThrowOnError().Timeout(TimeSpan.FromDays(1)));
var ex = UnitTestHelpers.AssertThrows<AggregateException>(() => command.Task.Wait());
ex.InnerExceptions.Select(e => e.GetType()).SequenceEqual(new[] { typeof(ErrorExitCodeException) })
.ShouldEqual(true);
}

[TestMethod]
public void TestTimeout()
{
Expand Down Expand Up @@ -219,6 +229,15 @@ public void TestNestedKill()
UnitTestHelpers.AssertThrows<ArgumentOutOfRangeException>(() => lines[0].ShouldEqual("a line"));
}

[TestMethod]
public void TestVersioning()
{
var version = typeof(Command).Assembly.GetName().Version.ToString();
var informationalVersion = (AssemblyInformationalVersionAttribute)typeof(Command).Assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute));
Assert.IsNotNull(informationalVersion);
version.ShouldEqual(informationalVersion.InformationalVersion + ".0");
}

private IEnumerable<string> ErrorLines()
{
yield return "1";
Expand Down
2 changes: 1 addition & 1 deletion MedallionShell/MedallionShell.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
A lightweight library that simplifies working with processes in .NET
</description>
<releaseNotes>
Allowed for argument ommission in Command.Run(), other minor fixes
Fixed bug where timeout would suppress errors from ThrowOnError option
</releaseNotes>
<copyright>Copyright 2014</copyright>
<tags>process async</tags>
Expand Down
11 changes: 9 additions & 2 deletions MedallionShell/ProcessCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,16 @@ private static Task CreateProcessTask(Process process, bool throwOnError, TimeSp
}

private static async Task AddTimeout(Task task, Process process, TimeSpan timeout)
{
{
// wait for either the given task or the timeout to complete
// http://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-complete-with-timeout
if (await SystemTask.WhenAny(task, SystemTask.Delay(timeout)).ConfigureAwait(false) != task)
var completed = await SystemTask.WhenAny(task, SystemTask.Delay(timeout)).ConfigureAwait(false);

// Task.WhenAny() swallows errors: wait for the completed task to propagate any errors that occurred
await completed.ConfigureAwait(false);

// if we timed out, kill the process
if (completed != task)
{
Log.WriteLine("Process timed out");
TryKillProcess(process);
Expand Down
4 changes: 3 additions & 1 deletion MedallionShell/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

// 1.0.2: fixed bug where timeout would suppress errors from ThrowOnError option
// 1.0.1.0: allowed for argument ommission in Command.Run(), other minor fixes
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("MedallionShell.Tests")]

0 comments on commit 9aa730e

Please sign in to comment.