Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IAsyncDisposable to TestServiceProvider #292

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ List of new features.

By [@egil](https://github.com/egil) in [#288](https://github.com/egil/bUnit/pull/288).

- Added support for registering services in bUnits `Services` collection that implements `IAsyncDisposable`. Suggested by [@jmaillet](https://github.com/jmaillet) in [#249](https://github.com/egil/bUnit/issues/249).

### Changed
List of changes in existing functionality.

Expand Down
11 changes: 9 additions & 2 deletions src/bunit.core/TestServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class TestServiceProvider : IServiceProvider, IServiceCollection,
{
private readonly IServiceCollection _serviceCollection;
private ServiceProvider? _serviceProvider;

/// <summary>
/// Gets whether this <see cref="TestServiceProvider"/> has been initialized, and
/// no longer will accept calls to the <c>AddService</c>'s methods.
Expand Down Expand Up @@ -78,7 +78,14 @@ public object GetService(Type serviceType)
/// <inheritdoc/>
public void Dispose()
{
_serviceProvider?.Dispose();
if (_serviceProvider is null) return;

var disposedTask = _serviceProvider.DisposeAsync().AsTask();

if (!disposedTask.IsCompleted)
disposedTask.GetAwaiter().GetResult();

_serviceProvider.Dispose();
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion tests/bunit.core.tests/TestServiceProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Bunit
{
public class TestServiceProviderTest
public partial class TestServiceProviderTest
{
class DummyService { }
class AnotherDummyService { }
Expand Down
28 changes: 28 additions & 0 deletions tests/bunit.core.tests/TestServiceProviderTest.net5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if NET5_0
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;

namespace Bunit
{
public partial class TestServiceProviderTest
{
[Fact(DisplayName = "Can correctly dispose of async disposable service")]
public void Net5Test001()
{
var sut = new TestServiceProvider();
sut.AddScoped<AsyncDisposableService>();
sut.GetService<AsyncDisposableService>();

Should.NotThrow(() => sut.Dispose());
}

class AsyncDisposableService : IAsyncDisposable
{
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
}
}
#endif