Skip to content

Commit

Permalink
Add new unit test: Resolving_On_Same_Scope_Should_Be_Thread_Safe
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Apr 20, 2017
1 parent 50cacd6 commit edd5818
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Castle.Windsor.MsDependencyInjection.Tests.TestClasses
{
public interface INavigationListener
{

}

public interface IEntityStateListener
{

}

public interface INavigationFixer : IEntityStateListener, INavigationListener
{

}

public class NavigationFixer : INavigationFixer
{
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Castle.MicroKernel.Registration;
using Castle.Windsor.MsDependencyInjection.Tests.TestClasses;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection.Specification;
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
using Shouldly;
Expand Down Expand Up @@ -320,7 +323,6 @@ public void ShouldReleaseScopedOnScopeDisposeButNotBefore()
var collection = new ServiceCollection();

collection.AddScoped<MyTestClass3>();

var serviceProvider = CreateServiceProvider(collection);
var windsorContainer = serviceProvider.GetService<IWindsorContainer>();

Expand All @@ -338,6 +340,28 @@ public void ShouldReleaseScopedOnScopeDisposeButNotBefore()
obj.IsDisposed.ShouldBeTrue();
}

[Fact]
public void Resolving_On_Same_Scope_Should_Be_Thread_Safe()
{
var serviceCollection = new ServiceCollection();
serviceCollection.TryAddEnumerable(new ServiceCollection()
.AddScoped<IEntityStateListener, INavigationFixer>(p => p.GetService<INavigationFixer>())
.AddScoped<INavigationListener, INavigationFixer>(p => p.GetService<INavigationFixer>())
);

serviceCollection.TryAdd(new ServiceCollection()
.AddScoped<INavigationFixer, NavigationFixer>()
);

var serviceProvider = CreateServiceProvider(serviceCollection);

Parallel.For(1, 100, (i) =>
{
var listener = serviceProvider.GetRequiredService<INavigationListener>();
(listener is NavigationFixer).ShouldBeTrue();
});
}

//TODO: TEST - Globally resolved objects should be disposed when the container is disposed?

public void Dispose()
Expand Down

0 comments on commit edd5818

Please sign in to comment.