Skip to content

Commit

Permalink
Merge pull request #5 from thomaslevesque/improve-tests
Browse files Browse the repository at this point in the history
Improve tests, fix exception propagation
  • Loading branch information
thomaslevesque authored Sep 24, 2018
2 parents ba9e34d + 094e6c8 commit 2d3eff4
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 281 deletions.
1 change: 1 addition & 0 deletions src/AspNetCore.AsyncInitialization/AsyncInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public async Task InitializeAsync()
catch(Exception ex)
{
_logger.LogError(ex, "Async initialization failed");
throw;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ public static IServiceCollection AddAsyncInitializer(this IServiceCollection ser
.AddTransient(implementationFactory);
}

/// <summary>
/// Adds an async initializer of the specified type
/// </summary>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> to add the service to.</param>
/// <param name="initializerType">The type of the async initializer to add.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddAsyncInitializer(this IServiceCollection services, Type initializerType)
{
return services
.AddAsyncInitialization()
.AddTransient(typeof(IAsyncInitializer), initializerType);
}

/// <summary>
/// Adds an async initializer whose implementation is the specified delegate.
/// </summary>
Expand Down
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>
<TargetFramework>netcoreapp2.1</TargetFramework>
Expand All @@ -7,10 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="4.8.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
126 changes: 126 additions & 0 deletions tests/AspNetCore.AsyncInitialization.Tests/AsyncInitializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Threading.Tasks;
using FakeItEasy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace AspNetCore.AsyncInitialization.Tests
{
public class AsyncInitializationTests
{
[Fact]
public async Task Single_initializer_is_called()
{
var initializer = A.Fake<IAsyncInitializer>();

var host = CreateHost(services => services.AddAsyncInitializer(initializer));

await host.InitAsync();

A.CallTo(() => initializer.InitializeAsync()).MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Delegate_initializer_is_called()
{
var initializer = A.Fake<Func<Task>>();

var host = CreateHost(services => services.AddAsyncInitializer(initializer));

await host.InitAsync();

A.CallTo(() => initializer()).MustHaveHappenedOnceExactly();
}

[Fact]
public async Task Multiple_initializers_are_called_in_order()
{
var initializer1 = A.Fake<IAsyncInitializer>();
var initializer2 = A.Fake<IAsyncInitializer>();
var initializer3 = A.Fake<IAsyncInitializer>();

var host = CreateHost(services =>
{
services.AddAsyncInitializer(initializer1);
services.AddAsyncInitializer(initializer2);
services.AddAsyncInitializer(initializer3);
});

await host.InitAsync();

A.CallTo(() => initializer1.InitializeAsync()).MustHaveHappenedOnceExactly()
.Then(A.CallTo(() => initializer2.InitializeAsync()).MustHaveHappenedOnceExactly())
.Then(A.CallTo(() => initializer3.InitializeAsync()).MustHaveHappenedOnceExactly());
}

[Fact]
public async Task Initializer_with_scoped_dependency_is_resolved()
{
var host = CreateHost(
services =>
{
services.AddScoped(sp => A.Fake<IDependency>());
services.AddAsyncInitializer<Initializer>();
},
true);

await host.InitAsync();
}

[Fact]
public async Task Failing_initializer_makes_initialization_fail()
{
var initializer1 = A.Fake<IAsyncInitializer>();
var initializer2 = A.Fake<IAsyncInitializer>();
var initializer3 = A.Fake<IAsyncInitializer>();

A.CallTo(() => initializer2.InitializeAsync()).ThrowsAsync(() => new Exception("oops"));

var host = CreateHost(services =>
{
services.AddAsyncInitializer(initializer1);
services.AddAsyncInitializer(initializer2);
services.AddAsyncInitializer(initializer3);
});

var exception = await Record.ExceptionAsync(() => host.InitAsync());
Assert.IsType<Exception>(exception);
Assert.Equal("oops", exception.Message);

A.CallTo(() => initializer1.InitializeAsync()).MustHaveHappenedOnceExactly();
A.CallTo(() => initializer3.InitializeAsync()).MustNotHaveHappened();
}

private static IWebHost CreateHost(Action<IServiceCollection> configureServices, bool validateScopes = false) =>
new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureServices(configureServices)
.UseDefaultServiceProvider(options => options.ValidateScopes = validateScopes)
.Build();

public class Startup
{
public void Configure(IApplicationBuilder app)
{
}
}

public interface IDependency
{
}

public class Initializer : IAsyncInitializer
{
// ReSharper disable once NotAccessedField.Local
private readonly IDependency _dependency;

public Initializer(IDependency dependency)
{
_dependency = dependency;
}
public Task InitializeAsync() => Task.CompletedTask;
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2d3eff4

Please sign in to comment.