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

Add IAsyncDispose support #51

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 1 addition & 1 deletion BoDi.NuGetPackages/BoDi.Embedded/BoDi.Embedded.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>BoDi.Embedded</id>
<version>1.5.0-alpha1</version>
<version>1.6.0-alpha1</version>
<title>BoDi - Simple IoC Container (embedded as source)</title>
<authors>Gaspar Nagy</authors>
<owners>Gaspar Nagy</owners>
Expand Down
2 changes: 1 addition & 1 deletion BoDi.NuGetPackages/BoDi/BoDi.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>BoDi</id>
<version>1.5.0-alpha1</version>
<version>1.6.0-alpha1</version>
<title>BoDi - Simple IoC Container</title>
<authors>Gaspar Nagy</authors>
<owners>Gaspar Nagy</owners>
Expand Down
202 changes: 202 additions & 0 deletions BoDi.Tests/AsyncDisposeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;

using Should;

namespace BoDi.Tests
{
#if ASYNC_DISPOSE
[TestFixture]
public class AsyncDisposeTests
{
[Test]
public void ContainerShouldBeAsyncDisposable()
{
var container = new ObjectContainer();

container.ShouldImplement<IAsyncDisposable>();
}

[Test/*ExpectedException(typeof(ObjectContainerException), ExpectedMessage = "disposed", MatchType = MessageMatch.Contains)*/]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment somehow relevant?

public async Task ContainerShouldThrowExceptionWhenDisposedAsynchronously()
{
var container = new ObjectContainer();
await container.DisposeAsync();

Assert.Throws<ObjectContainerException>(() => container.Resolve<IObjectContainer>(), "Object container disposed");
}

[Test]
public void ShouldDisposeAsyncCreatedObjects()
{
var container = new ObjectContainer();
container.RegisterTypeAs<AsyncDisposableClass1, IDisposableClass>();

var obj = container.Resolve<IDisposableClass>();

container.Dispose();

obj.WasDisposed.ShouldBeTrue();
}

[Test]
public async Task ShouldDisposeCreatedObjectsOnDisposeAsync()
{
var container = new ObjectContainer();
container.RegisterTypeAs<DisposableClass1, IDisposableClass>();

var obj = container.Resolve<IDisposableClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeTrue();
}

[Test]
public void ShouldPreferSyncOverAsyncDisposeAsyncCreatedObjectsOnSyncDispose()
{
var container = new ObjectContainer();
container.RegisterTypeAs<SyncAndAsyncDisposableClass1, IDisposableOptionallyAsyncClass>();

var obj = container.Resolve<IDisposableOptionallyAsyncClass>();

container.Dispose();

obj.WasDisposed.ShouldBeTrue();
obj.DisposalWasAsync.ShouldBeFalse();
}

[Test]
public async Task ShouldPreferAsyncOverSyncDisposeAsyncCreatedObjectsOnAsyncDispose()
{
var container = new ObjectContainer();
container.RegisterTypeAs<SyncAndAsyncDisposableClass1, IDisposableOptionallyAsyncClass>();

var obj = container.Resolve<IDisposableOptionallyAsyncClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeTrue();
obj.DisposalWasAsync.ShouldBeTrue();
}

[Test]
public async Task ShouldDisposeAsyncCreatedObjectsOnDisposeAsync()
{
var container = new ObjectContainer();
container.RegisterTypeAs<AsyncDisposableClass1, IDisposableClass>();

var obj = container.Resolve<IDisposableClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeTrue();
}

[Test]
public void ShouldAsyncDisposeInstanceRegistrations()
{
var container = new ObjectContainer();
var obj = new AsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableClass>(obj, dispose: true);

container.Resolve<IDisposableClass>();

container.Dispose();

obj.WasDisposed.ShouldBeTrue();
}

[Test]
public async Task ShouldAsyncDisposeInstanceRegistrationsOnDisposeAsync()
{
var container = new ObjectContainer();
var obj = new AsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableClass>(obj, dispose: true);

container.Resolve<IDisposableClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeTrue();
}

[Test]
public void ShouldPreferSyncDisposeOverAsyncForInstanceRegistrationsOnSyncDispose()
{
var container = new ObjectContainer();
var obj = new SyncAndAsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableOptionallyAsyncClass>(obj, dispose: true);

container.Resolve<IDisposableOptionallyAsyncClass>();

container.Dispose();

obj.WasDisposed.ShouldBeTrue();
obj.DisposalWasAsync.ShouldBeFalse();
}

[Test]
public async Task ShouldPreferAsyncDisposeOverSyncForInstanceRegistrationsOnAsyncDispose()
{
var container = new ObjectContainer();
var obj = new SyncAndAsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableOptionallyAsyncClass>(obj, dispose: true);

container.Resolve<IDisposableOptionallyAsyncClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeTrue();
obj.DisposalWasAsync.ShouldBeTrue();
}

[Test]
public void ShouldNotDisposeAsyncDisposableObjectsRegisteredAsInstance()
{
var container = new ObjectContainer();
var obj = new AsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableClass>(obj);

container.Resolve<IDisposableClass>();

container.Dispose();

obj.WasDisposed.ShouldBeFalse();
}

[Test]
public async Task ShouldNotDisposeObjectsRegisteredAsInstanceOnDisposeAsync()
{
var container = new ObjectContainer();
var obj = new DisposableClass1();
container.RegisterInstanceAs<IDisposableClass>(obj);

container.Resolve<IDisposableClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeFalse();
}

[Test]
public async Task ShouldNotDisposeAsyncDisposableObjectsRegisteredAsInstanceOnDisposeAsync()
{
var container = new ObjectContainer();
var obj = new AsyncDisposableClass1();
container.RegisterInstanceAs<IDisposableClass>(obj);

container.Resolve<IDisposableClass>();

await container.DisposeAsync();

obj.WasDisposed.ShouldBeFalse();
}
}
#endif
}
2 changes: 1 addition & 1 deletion BoDi.Tests/BoDi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<RootNamespace>BoDi.Tests</RootNamespace>
<AssemblyName>BoDi.Tests</AssemblyName>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net45;netcoreapp2.0;netcoreapp3.1</TargetFrameworks>

<Copyright>"Copyright (C) 2010-2018, Gaspar Nagy (http://gasparnagy.com)"</Copyright>
<Company>gasparnagy.com</Company>
Expand Down
2 changes: 2 additions & 0 deletions BoDi.Tests/DisposeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;
using Should;

Expand Down
2 changes: 1 addition & 1 deletion BoDi.Tests/RegisterFromConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BoDi.Tests
[TestFixture]
public class RegisterFromConfigTests
{
#if !NETCOREAPP2_0
#if !NETCOREAPP
//Disable this tests, because of problem with dotnet test and app.configs - https://github.com/dotnet/corefx/issues/22101#partial-timeline-marker

[Test]
Expand Down
39 changes: 38 additions & 1 deletion BoDi.Tests/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;

namespace BoDi.Tests
{
public interface IInterface1
Expand Down Expand Up @@ -196,8 +197,44 @@ public void Dispose()
{
WasDisposed = true;
}
}

#if ASYNC_DISPOSE
public class AsyncDisposableClass1 : IDisposableClass, IAsyncDisposable
{
public bool WasDisposed { get; private set; }

public ValueTask DisposeAsync()
{
WasDisposed = true;
return new ValueTask(Task.CompletedTask);
}
}

public interface IDisposableOptionallyAsyncClass : IDisposableClass
{
bool DisposalWasAsync { get; }
}

public class SyncAndAsyncDisposableClass1 : IDisposableOptionallyAsyncClass, IDisposable, IAsyncDisposable
{
public bool WasDisposed { get; private set; }
public bool DisposalWasAsync { get; private set; }

public void Dispose()
{
WasDisposed = true;
}

public ValueTask DisposeAsync()
{
WasDisposed = true;
DisposalWasAsync = true;
return new ValueTask(Task.CompletedTask);
}
}
#endif

public enum MyEnumKey
{
One,
Expand Down
Loading