Browse Source

Added tests for AsyncHelper.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
a7cfc5318d
  1. 3
      src/Volo.Abp/Volo/Abp/Threading/AsyncHelper.cs
  2. 37
      test/Volo.Abp.Tests/Volo/Abp/Threading/AsyncHelper_Tests.cs

3
src/Volo.Abp/Volo/Abp/Threading/AsyncHelper.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using Nito.AsyncEx; using Nito.AsyncEx;
namespace Volo.Abp.Threading namespace Volo.Abp.Threading
@ -14,7 +15,7 @@ namespace Volo.Abp.Threading
/// Checks if given method is an async method. /// Checks if given method is an async method.
/// </summary> /// </summary>
/// <param name="method">A method to check</param> /// <param name="method">A method to check</param>
public static bool IsAsyncMethod(MethodInfo method) public static bool IsAsyncMethod([NotNull] this MethodInfo method)
{ {
return method.ReturnType == typeof(Task) || return method.ReturnType == typeof(Task) ||
(method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)); (method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));

37
test/Volo.Abp.Tests/Volo/Abp/Threading/AsyncHelper_Tests.cs

@ -1,4 +1,6 @@
using System.Threading.Tasks; using System.Reflection;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -9,24 +11,49 @@ namespace Volo.Abp.Threading
[Fact] [Fact]
public void RunSync_Test_Without_Return_Value() public void RunSync_Test_Without_Return_Value()
{ {
AsyncHelper.RunSync(MyTaskWithoutReturnValue); AsyncHelper.RunSync(MyTaskWithoutReturnValueAsync);
} }
[Fact] [Fact]
public void RunSync_Test_With_Return_Value() public void RunSync_Test_With_Return_Value()
{ {
AsyncHelper.RunSync(() => MyTaskWithReturnValue(42)).ShouldBe(42); AsyncHelper.RunSync(() => MyTaskWithReturnValueAsync(42)).ShouldBe(42);
} }
private static async Task MyTaskWithoutReturnValue() [Fact]
public void IsAsync_Should_Work()
{
GetType().GetMethod(
"MyTaskWithoutReturnValueAsync",
BindingFlags.NonPublic | BindingFlags.Static
).IsAsyncMethod().ShouldBe(true);
GetType().GetMethod(
"MyTaskWithReturnValueAsync",
BindingFlags.NonPublic | BindingFlags.Static
).IsAsyncMethod().ShouldBe(true);
GetType().GetMethod(
"MyTaskWithReturnValue2",
BindingFlags.NonPublic | BindingFlags.Instance
).IsAsyncMethod().ShouldBe(false);
}
private static async Task MyTaskWithoutReturnValueAsync()
{ {
await Task.Delay(1); await Task.Delay(1);
} }
private static async Task<int> MyTaskWithReturnValue(int aNumber) private static async Task<int> MyTaskWithReturnValueAsync(int aNumber)
{ {
await Task.Delay(1); await Task.Delay(1);
return aNumber; return aNumber;
} }
[UsedImplicitly]
private int MyTaskWithReturnValue2(int aNumber)
{
return aNumber;
}
} }
} }

Loading…
Cancel
Save