Skip to content

Commit

Permalink
fix #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Qu authored and Victor Qu committed Nov 3, 2022
1 parent 27cb56e commit 48bfba2
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ProjectCommon.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<IsPackable>true</IsPackable>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>0.0.4.6</Version>
<Version>0.0.4.7</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
Expand Down
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,23 @@ Many implementations of Norns.urd refer to the source code of Castle and AspectC

``` ini

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1256 (1909/November2018Update/19H2)
Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.101
[Host] : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
DefaultJob : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT


```
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|----------------------------------------------- |----------:|---------:|---------:|-------:|------:|------:|----------:|
| TransientInstanceCallSyncMethodWhenNoAop | 61.77 ns | 0.311 ns | 0.291 ns | 0.0178 | - | - | 112 B |
| TransientInstanceCallSyncMethodWhenNornsUrd | 155.58 ns | 1.038 ns | 0.971 ns | 0.0548 | - | - | 344 B |
| TransientInstanceCallSyncMethodWhenCastle | 213.94 ns | 1.213 ns | 1.076 ns | 0.0815 | - | - | 512 B |
| TransientInstanceCallSyncMethodWhenAspectCore | 508.71 ns | 2.334 ns | 2.183 ns | 0.1030 | - | - | 648 B |
| TransientInstanceCallAsyncMethodWhenNoAop | 92.58 ns | 0.793 ns | 0.619 ns | 0.0408 | - | - | 256 B |
| TransientInstanceCallAsyncMethodWhenNornsUrd | 242.98 ns | 0.818 ns | 0.765 ns | 0.0892 | - | - | 560 B |
| TransientInstanceCallAsyncMethodWhenCastle | 225.98 ns | 0.521 ns | 0.487 ns | 0.1044 | - | - | 656 B |
| TransientInstanceCallAsyncMethodWhenAspectCore | 565.25 ns | 2.377 ns | 2.107 ns | 0.1373 | - | - | 864 B |
BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.1288/21H2/November2021Update)
Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.402
[Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2
DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2


| Method | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------------------- |----------:|---------:|---------:|-------:|----------:|
| TransientInstanceCallSyncMethodWhenNoAop | 49.63 ns | 0.249 ns | 0.233 ns | 0.0134 | 112 B |
| TransientInstanceCallSyncMethodWhenNornsUrd | 116.59 ns | 0.436 ns | 0.364 ns | 0.0411 | 344 B |
| TransientInstanceCallSyncMethodWhenCastle | 174.01 ns | 0.708 ns | 0.628 ns | 0.0610 | 512 B |
| TransientInstanceCallSyncMethodWhenAspectCore | 434.65 ns | 2.315 ns | 2.052 ns | 0.0772 | 648 B |
| TransientInstanceCallAsyncMethodWhenNoAop | 88.87 ns | 0.909 ns | 0.759 ns | 0.0305 | 256 B |
| TransientInstanceCallAsyncMethodWhenNornsUrd | 222.94 ns | 2.224 ns | 2.080 ns | 0.0668 | 560 B |
| TransientInstanceCallAsyncMethodWhenCastle | 193.51 ns | 1.803 ns | 1.408 ns | 0.0782 | 656 B |
| TransientInstanceCallAsyncMethodWhenAspectCore | 518.50 ns | 2.043 ns | 1.911 ns | 0.1030 | 864 B |

## License
[MIT](https://github.com/fs7744/Norns.Urd/blob/main/LICENSE)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../../ProjectCommon.targets" />
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
Expand All @@ -15,4 +15,9 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd\Norns.Urd.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd.Caching.Memory\Norns.Urd.Caching.Memory.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion src/Norns.Urd.Caching.Memory/Norns.Urd.Caching.Memory.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../../ProjectCommon.targets" />
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
Expand All @@ -14,5 +14,9 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd.Caching.Abstractions\Norns.Urd.Caching.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd\Norns.Urd.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../ProjectCommon.targets" />
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
Expand All @@ -10,5 +10,9 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd.HttpClient\Norns.Urd.HttpClient.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/Norns.Urd.HttpClient/Norns.Urd.HttpClient.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../ProjectCommon.targets" />
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
Expand All @@ -25,4 +25,8 @@
<ItemGroup>
<ProjectReference Include="..\Norns.Urd\Norns.Urd.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>
</Project>
15 changes: 3 additions & 12 deletions src/Norns.Urd/Interceptors/InterceptorCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,17 @@ public InterceptorCreator(IAspectConfiguration configuration)

public static async Task Await(Task task)
{
if (!task.IsCompleted)
{
await task;
}
await task;
}

public static async Task AwaitValueTask(ValueTask task)
{
if (!task.IsCompleted)
{
await task;
}
await task;
}

public static async Task AwaitValueTaskReturnValue<T>(ValueTask<T> task)
{
if (!task.IsCompleted)
{
await task;
}
await task;
}

public static DynamicMethod CreateCaller(MethodInfo method)
Expand Down
7 changes: 6 additions & 1 deletion src/Norns.Urd/Norns.Urd.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../ProjectCommon.targets" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -25,4 +25,9 @@
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageReference Update="SecurityCodeScan" Version="3.5.4" />
</ItemGroup>

</Project>
9 changes: 7 additions & 2 deletions test/Norns.Urd.Test/DependencyInjection/AopTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ namespace Test.Norns.Urd.DependencyInjection
{
public static class AopTestExtensions
{
public static IServiceProvider ConfigServiceCollectionWithAop(Func<IServiceCollection, IServiceCollection> config, bool isIgnoreError = false)
public static IServiceCollection ConfigCollectionWithAop(Func<IServiceCollection, IServiceCollection> config, bool isIgnoreError = false)
{
return config(new ServiceCollection())
.ConfigureAop(i =>
{
i.NonPredicates.Clean();
i.GlobalInterceptors.Add(new AddTenInterceptor());
i.FacdeProxyAllowPredicates.AddNamespace("*");
}, isIgnoreError)
}, isIgnoreError);
}

public static IServiceProvider ConfigServiceCollectionWithAop(Func<IServiceCollection, IServiceCollection> config, bool isIgnoreError = false)
{
return ConfigCollectionWithAop(config, isIgnoreError)
.BuildServiceProvider();
}
}
Expand Down
57 changes: 57 additions & 0 deletions test/Norns.Urd.Test/DependencyInjection/AutofacTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Norns.Urd;
using Norns.Urd.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using static Test.Norns.Urd.DependencyInjection.ExceptionTest;

namespace Test.Norns.Urd.DependencyInjection
{
[NonAspect]
public class AutofacTest
{
[Fact]
public void WhenUseAutofac()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(AopTestExtensions.ConfigCollectionWithAop(i => i.AddSingleton(i => new OneConstructorOneStringArgs("a"))));
var v = new AutofacServiceProvider(containerBuilder.Build())
.GetRequiredService<OneConstructorOneStringArgs>();
var pt = v.GetType();
Assert.True(pt.IsProxyType());
Assert.NotNull(pt.CreateInstanceGetter()(v));
Assert.NotNull(pt.CreateServiceProviderGetter()(v));
Assert.NotNull(v);
Assert.NotNull(v.A);
Assert.Equal("a", v.A);
}

public DoExceptionTest Mock()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(new ServiceCollection()
.AddTransient<DoExceptionTest>()
.ConfigureAop(i => i.GlobalInterceptors.Add(new NoThings())));
return new AutofacServiceProvider(containerBuilder.Build())
.GetRequiredService<DoExceptionTest>();
}

[Fact]
public async Task WhenExceptionFromMehtodUseAutofac()
{
var sut = Mock();
Assert.Throws<FieldAccessException>(() => sut.DoVoid(true));
Assert.Throws<FieldAccessException>(() => sut.Do(true));
await Assert.ThrowsAsync<FieldAccessException>(() => sut.DoAsync(true));
await Assert.ThrowsAsync<FieldAccessException>(async () => await sut.DoErrorValueTaskAsync(true));
await Assert.ThrowsAsync<FieldAccessException>(() => sut.DoValueTaskAsync(true).AsTask());
await Assert.ThrowsAsync<FieldAccessException>(() => sut.DoTaskAsync(true));
}
}
}
5 changes: 3 additions & 2 deletions test/Norns.Urd.Test/DependencyInjection/ExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ public virtual bool Do(bool error)
return error;
}

public virtual Task<bool> DoAsync(bool error)
public virtual async Task<bool> DoAsync(bool error)
{
if (error)
{
throw new FieldAccessException();
}
return Task.FromResult(error);
await Task.Yield();
return error;
}

public virtual Task DoTaskAsync(bool error)
Expand Down
4 changes: 3 additions & 1 deletion test/Norns.Urd.Test/Norns.Urd.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="6.4.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.15.1" />
<PackageReference Include="Polly.Caching.Memory" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
9 changes: 5 additions & 4 deletions test/SimpleBenchmark/SimpleBenchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -9,9 +9,10 @@

<ItemGroup>
<PackageReference Include="AspectCore.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Castle.Windsor" Version="5.1.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="Castle.Windsor" Version="5.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 48bfba2

Please sign in to comment.