Skip to content

Commit

Permalink
#526 Preparing unit tests for generic async method issue
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed May 20, 2023
1 parent 4bb81b7 commit 8a1c5ac
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 131 deletions.
21 changes: 21 additions & 0 deletions AssemblyWithoutInterceptor/ClassWithAsyncMethod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MethodTimer;
#pragma warning disable 414
Expand Down Expand Up @@ -102,3 +103,23 @@ public async Task<bool> ComplexMethodWithAwaitAsync(int instructionsToHandle)
public async Task MethodWithExceptionAsync() =>
await Task.Factory.StartNew(() => throw new ArgumentOutOfRangeException());
}

public class ClassWithGenericResultAsyncMethodBase
{
public virtual async Task<List<T>> DoSomethingAsync<T>()
{
await Task.Delay(50);

return new List<T>();
}
}

public class ClassWithGenericResultAsyncMethod : ClassWithGenericResultAsyncMethodBase
{
[Time("some message")]
public override async Task<List<T>> DoSomethingAsync<T>()
{
var result = await base.DoSomethingAsync<T>();
return result;
}
}
151 changes: 151 additions & 0 deletions Tests/AssemblyTesterSets/WithoutInterceptorTests.async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

public partial class WithoutInterceptorTests
{
[Fact]
public void MethodWithEmptyAsync()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithEmptyAsync();
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithEmptyAsync ", message.First());
}

[Fact]
public void ClassWithAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithAwaitAsync();
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithAsyncMethodThatThrowsException()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
try
{
var task = (Task)instance.MethodWithAwaitAndExceptionAsync();
task.Wait();
}
catch (Exception)
{
// Expected
}
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithAwaitAndExceptionAsync ", message.First());
}

[Fact]
public void ClassWithInheritedGenericTaskAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithGenericResultAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.DoSomethingAsync<int>();
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithGenericResultAsyncMethod.DoSomethingAsync", message.First());
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ClassWithAsyncMethodWithFastPath(bool recurse)
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithFastPathAsync(recurse);
task.Wait();
});

Assert.Equal(recurse ? 2 : 1, message.Count);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithFastPathAsync ", message.First());
}

[Fact]
public void ClassWithExceptionAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(-1);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithFastComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(0);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithMediumComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(2);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithSlowComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(100);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}
}
132 changes: 1 addition & 131 deletions Tests/AssemblyTesterSets/WithoutInterceptorTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Fody;
using Xunit;

public class WithoutInterceptorTests
public partial class WithoutInterceptorTests
{
static TestResult testResult;

Expand Down Expand Up @@ -53,135 +52,6 @@ public void ClassWithYieldMethod()
//Assert.True(message.First().StartsWith("ClassWithYieldMethod.YieldMethod "));
}

[Fact]
public void MethodWithEmptyAsync()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithEmptyAsync();
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithEmptyAsync ", message.First());
}

[Fact]
public void ClassWithAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithAwaitAsync();
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithAsyncMethodThatThrowsException()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
try
{
var task = (Task)instance.MethodWithAwaitAndExceptionAsync();
task.Wait();
}
catch (Exception)
{
// Expected
}
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithAwaitAndExceptionAsync ", message.First());
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ClassWithAsyncMethodWithFastPath(bool recurse)
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.MethodWithFastPathAsync(recurse);
task.Wait();
});

Assert.Equal(recurse ? 2 : 1, message.Count);
Assert.StartsWith("ClassWithAsyncMethod.MethodWithFastPathAsync ", message.First());
}

[Fact]
public void ClassWithExceptionAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(-1);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithFastComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(0);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithMediumComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(2);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithSlowComplexAsyncMethod()
{
var type = testResult.Assembly.GetType("ClassWithAsyncMethod");
var instance = (dynamic)Activator.CreateInstance(type);
var message = TraceRunner.Capture(() =>
{
var task = (Task)instance.ComplexMethodWithAwaitAsync(100);
task.Wait();
});

Assert.Single(message);
Assert.StartsWith("ClassWithAsyncMethod.ComplexMethodWithAwaitAsync ", message.First());
}

[Fact]
public void ClassWithConstructor()
{
Expand Down

0 comments on commit 8a1c5ac

Please sign in to comment.