Change ThrowIfFaulted to WaitAndThrowIfFaulted

This commit is contained in:
Pranav K 2014-08-21 11:55:41 -07:00
parent 9c4df4606f
commit eccd25bedd
3 changed files with 11 additions and 11 deletions

View File

@ -11,13 +11,13 @@ namespace Microsoft.AspNet.Mvc.Internal
public static class TaskHelper
{
/// <summary>
/// Throws the first faulting exception for a task which is faulted. It preserves the original stack trace when
/// throwing the exception.
/// Waits for the task to complete and throws the first faulting exception if the task is faulted.
/// It preserves the original stack trace when throwing the exception.
/// </summary>
/// <remarks>
/// Invoking this method is equivalent to calling Wait() on the <paramref name="task" /> if it is not completed.
/// </remarks>
public static void ThrowIfFaulted(Task task)
public static void WaitAndThrowIfFaulted(Task task)
{
task.GetAwaiter().GetResult();
}

View File

@ -101,7 +101,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
var viewContext = new ViewContext(_viewContext, viewEngineResult.View, _viewData, writer);
var renderTask = viewEngineResult.View.RenderAsync(viewContext);
TaskHelper.ThrowIfFaulted(renderTask);
TaskHelper.WaitAndThrowIfFaulted(renderTask);
return writer.ToString();
}
}

View File

@ -10,40 +10,40 @@ namespace Microsoft.AspNet.Mvc.Internal
public class TaskHelperTest
{
[Fact]
public void ThrowIfFaulted_DoesNotThrowIfTaskIsNotFaulted()
public void WaitAndThrowIfFaulted_DoesNotThrowIfTaskIsNotFaulted()
{
// Arrange
var task = Task.FromResult(0);
// Act and Assert
Assert.DoesNotThrow(() => TaskHelper.ThrowIfFaulted(task));
Assert.DoesNotThrow(() => TaskHelper.WaitAndThrowIfFaulted(task));
}
[Fact]
public void ThrowIfFaulted_ThrowsIfTaskIsFaulted()
public void WaitAndThrowIfFaulted_ThrowsIfTaskIsFaulted()
{
// Arrange
var message = "Exception message";
var task = CreatingFailingTask(message);
// Act and Assert
var ex = Assert.Throws<Exception>(() => TaskHelper.ThrowIfFaulted(task));
var ex = Assert.Throws<Exception>(() => TaskHelper.WaitAndThrowIfFaulted(task));
Assert.Equal(message, ex.Message);
}
[Fact]
public void ThrowIfFaulted_ThrowsFirstExceptionWhenAggregateTaskFails()
public void WaitAndThrowIfFaulted_ThrowsFirstExceptionWhenAggregateTaskFails()
{
// Arrange
var message = "Exception message";
var task = Task.Run(async () =>
{
await Task.WhenAll(CreatingFailingTask(message),
await Task.WhenAll(CreatingFailingTask(message),
CreatingFailingTask("different message"));
});
// Act and Assert
var ex = Assert.Throws<Exception>(() => TaskHelper.ThrowIfFaulted(task));
var ex = Assert.Throws<Exception>(() => TaskHelper.WaitAndThrowIfFaulted(task));
Assert.Equal(message, ex.Message);
}