forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify TemplateRenderer to use ThrowIfFaulted instead of Task.Wait
Fixes aspnet#782
- Loading branch information
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNet.Mvc.Internal | ||
{ | ||
/// <summary> | ||
/// Utility methods for dealing with <see cref="Task"/>. | ||
/// </summary> | ||
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. | ||
/// </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) | ||
{ | ||
task.GetAwaiter().GetResult(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/Microsoft.AspNet.Mvc.Core.Test/Internal/TaskHelperTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.Mvc.Internal | ||
{ | ||
public class TaskHelperTest | ||
{ | ||
[Fact] | ||
public void ThrowIfFaulted_DoesNotThrowIfTaskIsNotFaulted() | ||
{ | ||
// Arrange | ||
var task = Task.FromResult(0); | ||
|
||
// Act and Assert | ||
Assert.DoesNotThrow(() => TaskHelper.ThrowIfFaulted(task)); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfFaulted_ThrowsIfTaskIsFaulted() | ||
{ | ||
// Arrange | ||
var message = "Exception message"; | ||
var task = CreatingFailingTask(message); | ||
|
||
// Act and Assert | ||
var ex = Assert.Throws<Exception>(() => TaskHelper.ThrowIfFaulted(task)); | ||
Assert.Equal(message, ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfFaulted_ThrowsFirstExceptionWhenAggregateTaskFails() | ||
{ | ||
// Arrange | ||
var message = "Exception message"; | ||
var task = Task.Run(async () => | ||
{ | ||
await Task.WhenAll(CreatingFailingTask(message), | ||
CreatingFailingTask("different message")); | ||
}); | ||
|
||
// Act and Assert | ||
var ex = Assert.Throws<Exception>(() => TaskHelper.ThrowIfFaulted(task)); | ||
Assert.Equal(message, ex.Message); | ||
} | ||
|
||
private static Task CreatingFailingTask(string message) | ||
{ | ||
return Task.Run(() => | ||
{ | ||
throw new Exception(message); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters